OliveStudio / olivemenus

A powerful menus plugin for Craft CMS built for the need of simplicity and flexibility
MIT License
40 stars 22 forks source link

Custom Nav Output #25

Closed nevsie closed 5 years ago

nevsie commented 5 years ago

Like previous requests, i needed this. I have knocked up some crude code to complete this. It would need polish and features - but it is very easy and it does work.

OlivemenusVariable.php: I added:

    public function getMenuCustom($handle, $config = array())
    {
        if ($handle != '') {
            return Olivemenus::$plugin->olivemenus->getMenuCustom($handle, $config);
        }
    }

OlivemenusService.php: i added

    // CUSTOM Frotend Methods
    // =========================================================================
    public function getMenuCustom($handle = false, $config ) {
        if ($handle) {

            $menu = $this->getMenuByHandle($handle);

            if ($menu !== NULL) {
                $menu_items = Olivemenus::$plugin->olivemenuItems->getMenuItems($menu->id);

                foreach ( $menu_items as $key => $value ) {
                    $menu_items[$key] = $this->getMenuItemCustom($value);
                }

                return $menu_items;

            } else {
                return false;
            }

        }
    }

    private function getMenuItemCustom($menu_item) {

        if ($menu_item['custom_url'] != '') {
            $menu_item['url'] = $this->replaceEnvironmentVariables($menu_item['custom_url']);
        } else {
            $entry = Entry::find()
                ->id($menu_item['entry_id'])
                ->one();

            if (!empty($entry) ) 
            {
                $menu_item['url'] = $entry->url;
            }
            else
            {
                $entry = Category::find()
                ->id($menu_item['entry_id'])
                ->one();

                if (!empty($entry) ) 
                {
                    $menu_item['url'] = $entry->url;
                }
            }
        }

        if (isset($menu_item['children'])) {
            foreach ( $menu_item['children'] as $key => $value )
            {
                $menu_item['children'][$key] = $this->getMenuItemCustom($value);
            }
        }

        return $menu_item;
    }

And template code in twig, i used:

{% set navigation = craft.olivemenus.getMenuCustom('mainMenu') %}

{% macro buildNavMacro(node, index) %}
    <li class="{{ node.class_parent }}">
        <a href="{{ node.url }}" class="{{ node.class }}">
            {{ node.name | raw }}
        </a>
        {% if node.children is defined %}
            <ul>
                {% import _self as recur %}
                {% for child in node.children %}
                    {{ recur.buildNavMacro(child, loop.index) }}
                {% endfor %}
            </ul>
        {% endif %}
    </li>
{% endmacro %}

<nav class="navmain">
    <ul class="nav">
        {% import _self as tree %}
        {% for item in navigation %}
            {{ tree.buildNavMacro(item, loop.index) }}
        {% endfor %}
    </ul>
</nav>
alinistrate commented 5 years ago

Hi @nevsie Thanks for the suggestion but we don't have the resources now to implement this. You are always welcome to contribute by forking this repository, making the changes and create a pull request.

Thanks, Alin