Jump to content

Menu Builder


kongondo

Recommended Posts

On 6/13/2019 at 4:31 PM, uiui said:

Has anyone ever had the issue that changes to a menu won't save anymore? After adding page number 89 moving items isn't possible anymore as the change won't be saved. This is the case independent of the last page I add or where I move that page.

We had this issue recently, it's because of the limit of Input variables in your PHP configuration. Adjusting the value of max_input_vars in your php.ini file to a larger number will increase the number of items you can save ?

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

21 minutes ago, Asimov said:

We had this issue recently, it's because of the limit of Input variables in your PHP configuration. Adjusting the value of max_input_vars in your php.ini file to a larger number will increase the number of items you can save ?

Thanks @Asimov ! Excellent response! Welcome to the forums.

Link to comment
Share on other sites

  • 3 weeks later...

Hello,

I need some help please. ? With "configuring the menu" and adding some additional stuff to my mobile menu, but not really sure how to do that.

I would like my Logo to show in my mobile menu, and I would also like the language switcher there, but cant figure it out. I tried all sorts of things but cant make it work.

My html looks like this: In html the logo is there, but when I try tu give it to menubuilder code it is gone. 

Spoiler

    <nav id="ro-main-nav" class="ro-main-nav-style-1">
      <div class="container">
        <div class="ro-brand"><a href="index.html"><img src="javascript:if(confirm('http://mysite.com/images/logo.svg\n\nThis file was not retrieved because it was filtered out by your project settings.\n\nWould you like to open it from the server?'))window.location='http://mysite.com/images/logo.svg'" alt="Logo"/><img src="javascript:if(confirm('http://mysite.com/images/logo.svg\n\nThis file was not retrieved because it was filtered out by your project settings.\n\nWould you like to open it from the server?'))window.location='http://mysite.com/images/logo.svg'" alt="Logo"/></a></div>
        <ul class="ro-nav-content text-center">
          <li class="visible-xs"><a href="index.html"><img src="javascript:if(confirm('http://mysite.com/images/logo.svg\n\nThis file was not retrieved because it was filtered out by your project settings.\n\nWould you like to open it from the server?'))window.location='http://mysite.com/images/logo.svg'" alt="Logo"/></a></li>
          <li><a href="index.html">HOME</a>
            <ul class="ro-sub-nav">
              <li><a href="basic.html">Link 1</a></li>
              <li><a href="fullwidth.html">Link 2</a></li>
              <li><a href="fullheight.html">Link 3</a></li>

            </ul>
          </li>
          <li><a href="about.html">PAGES</a>
            <ul class="ro-sub-nav">
              <li><a href="about.html">About Us</a></li>
              <li><a href="404.html">Page 2</a></li>
            </ul>
          </li>
          <li><a href="service.html">SERVICES</a></li>
          <li><a href="shop.html">SHOP</a>
            <ul class="ro-sub-nav">
              <li><a href="product.html">Product Page</a></li>
              <li><a href="cart.html">Page</a></li>
              <li><a href="checkout-1.html">Also page</a></li>
            </ul>
          </li>
          <li><a href="contact.html">CONTACT</a></li>
        </ul>
        <div id="ro-hamburger" class="ro-hamburger visible-xs pull-right"><span></span></div>
      </div>
    </nav>

I've tried with:

Spoiler

<nav id="ro-main-nav" class="ro-main-nav-style-1">
      <div class="container">
        <div class="ro-brand"><a href="<?=$config->urls->root;?>"><img src="<?=$pages->get("/Nastavitve/")->header_logo->url?>'" alt="logo"/></a></div>
          <?php 
                        $menu = $modules->get('MarkupMenuBuilder');
                        $options = array(

                            'wrapper_list_type' => 'ul',// ul, ol, nav, div, etc.
                            'list_type' => 'li',// li, a, span, etc.
                            'menu_css_id' => '',// a CSS ID for the menu
                            'menu_css_class' => 'ro-nav-content text-center',// a CSS Class for the menu
                            'submenu_css_class' => 'ro-sub-nav',// CSS Class for sub-menus
                            'has_children_class' => 'ro-sub-nav',// CSS Class for any menu item that has children
                            'first_class'=>'',// CSS Class for the first item in 
                            'last_class' => '',
                            'current_class' => 'active',
                            'default_title' => 0,// 0=show saved titles;1=show actual/current titles
                            'include_children' => 4,// show 'natural' MB non-native descendant items as part of navigation
                            'm_max_level' => 1,// how deep to fetch 'include_children'
                            'current_class_level' => 1,// how high up the ancestral tree to apply 'current_class'
                            'default_class' => '',// a CSS class to apply to all menu items
                        );
                        echo $menu->render(1163, $options);
                        ?>

        <div id="ro-hamburger" class="ro-hamburger visible-xs pull-right"><span></span></div>
      </div>
    </nav>

but there is no logo inside. I also tried with both upper codes and added images to them but not working.

And second question, I would also like to put language switcher to it, not sure how to combine this ...

Spoiler

<ul class="ro-nav-content text-center">
<?php
$savedLanguage = $user->language;

foreach($languages as $language) {
  if($language->id == $savedLanguage->id) continue;
  if(!$page->viewable($language)) continue;
  $user->language = $language;
  echo "<li><a href='$page->url'>$language->title</a></li>";
}
$user->language = $savedLanguage;

?>
</ul>

I hope u understand what Im trying to do. Any help would be appreciated. ?

Thank you

R

Link to comment
Share on other sites

  • 1 month later...

HI there, 

Very nice module thank you.

I would like to put a class in each of the ul of the sub menus I have in my menu. It seems that through the admin interface or the $option of the render() function I can't achieve this. Is this correct ?

Then would the only  way to do this is the getMenuItems() function and coding the menu's markup ?

Thanks 

Link to comment
Share on other sites

  • 3 months later...

Hey guys, I've been using the MenuBuilder for quite a few projects (thanks @kongondo for the great module and support!), however this time it is my first when I need to use a menu in the sidebar if some pages are added to it. It all works as soon as I add a few items to the menu, however there is one little issue I am having - not knowing how to show the block title ONLY if the menu contains some items added. I've been looking through the module functions etc. to figure it out, however for some reason I am always getting the items count as 1 even though I've added 9.

<?php
        //Building the array of options. The ones that are not used can be removed
        //They are left here for the learning process only.
        $options = array(
         'wrapper_list_type' => 'ul',// ul, ol, nav, div, etc.
         'list_type' => 'li',// li, a, span, etc.
         'menu_css_id' => '',// a CSS ID for the menu
         'menu_css_class' => 'category-list',// a CSS Class for the menu
         'current_class' => 'active',
         'default_title' => 0,// 0=show saved titles;1=show actual/current titles
         'include_children' => 4,// show 'natural' MB non-native descendant items as part of navigation
         'm_max_level' => 1,// how deep to fetch 'include_children'
         'current_class_level' => 1,// how high up the ancestral tree to apply 'current_class'
        );

        // Render the menu by title
        $cat_menu = $menu->render(1108, $options);
        echo "This menu has: " . count($cat_menu);
        echo $cat_menu;

       ?>	

The big question here is, how can I add a check if my menu with a specific ID/title would contain some items or is just empty in order to show or not the block title etc.?

Link to comment
Share on other sites

On 12/4/2019 at 3:02 AM, MilenKo said:

Hey guys, I've been using the MenuBuilder for quite a few projects (thanks @kongondo for the great module and support!),

Hi @MilenKo. Apologies for the late response.

I am not sure what you mean by block title.

However, this count() will not work:

echo "This menu has: " . count($cat_menu);

as $menu->render() returns a string. PHP will return 1 (a boolean) but if you had Tracy Debugger it would throw an error about using count() on a non-iterable variable.

If you need to know the number of items you can use the method getMenuItems() introduced here and documented here. You can use the method to either return menu items as a an normal PHP array or as a WireArray. In the examples outlined in the first link above, you can use getMenuItems() with a custom recursive function to render your menu however you wish. In your case though, you can just use it to check number of items. For example:

$mb = $modules->get('MarkupMenuBuilder');
// menu items as PHP Array
$menuItemsAsArray = $mb->getMenuItems(1108, 1, $options);// param 2 = 1: return array
echo 'Number of Menu Items (count($menuItemsAsArray)): ' .  count($menuItemsAsArray);
// menu items as WireArray Object
$menuItemsAsObject = $mb->getMenuItems(1108, 2, $options);// param 2 = 2: return WireArray (the default)
echo 'Number of Menu Items ($menuItemsAsObject->count): ' .  $menuItemsAsObject->count;

In case you just want to know if a menu item is not empty, you can simply check if the return value of $cat_menu is a string. For example:

 // Render the menu by title
 $cat_menu = $menu->render(1108, $options);
 if(is_string( $cat_menu)) {
	echo $cat_menu;
 }

This, however, will not give you the count as explained above. You'd need getMenuItems() for that.

In case you want to know if a menu item is a parent/has children, etc, you can use the WireArray option of getMenuItems() as shown above and use ProcessWire WireArray methods to filter items. For instance:

$someChildItems = $menuItemsAsObject->find("parentID=2");
// OR
$someParents = $menuItemsAsObject->find("isParent=1");

I hope this helps.

Link to comment
Share on other sites

/site/modules/ProcessMenuBuilder/ProcessMenuBuilder.css

178 line

div.handle i.fa-trash {
    position: relative;
    border: none;
    padding: 0.6em;
    /*margin-right: -2.125em;*/
    top: 1px;
}

fix: add comment margin

 

Знімок екрану.png

  • Like 2
Link to comment
Share on other sites

Hi guys, hi @kongondo,

I have a menu which I built using your code from some pages back:

Quote

function buildMenuFromObject($parent = 0, $menu, $first = 0) {
    if(!is_object($menu)) return;

    $out = '';
    $has_child = false;

    foreach ($menu as $m) {
        $newtab = $m->newtab ? " target='_blank'" : '';
        // if this menu item is a parent; create the sub-items/child-menu-items
        if ($m->parentID == $parent) {// if this menu item is a parent; create the inner-items/child-menu-items
            // if this is the first child
            if ($has_child === false) {
                $has_child = true;// This is a parent
                if ($first == 0){
                    $out .= "<ul class='mainMenu js-mainMenu clearfix'>\n";
                    $first = 1;
                }
                else $out .= "\n<ul class='subMenu js-subMenu'>\n";
            }

            $class = $m->isCurrent ? ' class="current"' : '';

            // a menu item
            $out .= '<li' . $class . '><a href="' . $m->url . '"' . $newtab . '>' . $m->title;
            // if menu item has children
            if ($m->isParent) {
                $out .= '</a><span class="icon submenu__expand js-submenu__expand icon-cheveron-down"></span>';
            }

            else $out .= '</a>';

            // call function again to generate nested list for sub-menu items belonging to this menu item.
            $out .= buildMenuFromObject($m->id, $menu, $first);
            $out .= "</li>\n";

        }// end if parent

    }// end foreach

    if ($has_child === true) $out .= "</ul>\n";

    return $out;

}

 

which works fine, although I have a question regarding the submenus. I have selected to include all native descendants of my page into the submenu. which generates something like this:

<ul>
  <li> Projects </li>
  <ul class="submenu">
  	<li class="submenuItem">
      Project 1
    </li>
    <li class="submenuItem">
      Project 2
    </li>
    ... -> render all remaining Project objects which are children of the Projects Page
  </ul>

This, however, bloats my submenu, if I have a lot of projects. Is there some option to limit the amount of projects that are rendered, to e.g. 10 and then provide a link as the last project item which leads to a project list view which displays all projects?

I just don't quite get the code; e.g. where the code fetches the native descendants of the ``projects`` page.

Any help would be appreciated.

Greetings, derelektrischemoench

Link to comment
Share on other sites

Update: Menu Builder 0.2.6

Changelog

  1. Added the properties numChildren, totalChildren and showMoreText for use with getMenuItems()
  2. Added option maximum_children_per_parent to limit the maximum number of (included) children a menu item can return. Thanks @derelektrischemoench for inspiration/request.
  3. Fixed CSS issue that affected menu items' trash cans in the backend. Thanks @duncan.
  4. Refactored code to improve efficiency.

In dev branch only for testing.

I have also updated the docs, especially around getMenuItems().

@derelektrischemoench,

See this gist for an example recursive menu builder function that uses maximum_children_per_parent and showMore.

OK, breaktime over, back to Padloper! :-).

  • Like 2
Link to comment
Share on other sites

  • 3 weeks later...
On 12/17/2019 at 1:44 PM, kongondo said:

Update: Menu Builder 0.2.6

Changelog

  1. Added the properties numChildren, totalChildren and showMoreText for use with getMenuItems()
  2. Added option maximum_children_per_parent to limit the maximum number of (included) children a menu item can return. Thanks @derelektrischemoench for inspiration/request.
  3. Fixed CSS issue that affected menu items' trash cans in the backend. Thanks @duncan.
  4. Refactored code to improve efficiency.

In dev branch only for testing.

I have also updated the docs, especially around getMenuItems().

@derelektrischemoench,

See this gist for an example recursive menu builder function that uses maximum_children_per_parent and showMore.

OK, breaktime over, back to Padloper! :-).

Hi Kongondo,

thanks for including my input and change requests :) very cool! I'm gonna give it a try, asap.

Greetings

derelektrischemoench

Link to comment
Share on other sites

Hi kongondo and hi all guys!

Is there a way to put a new field (ex: description) on each menu entries? I like to have something like:


Title: Home
Description: My beautiful home page
URL: /my-beautiful-url/

I wait your feedback.

Thanks and have a nice day!

Link to comment
Share on other sites

  • 2 weeks later...
On 1/8/2020 at 5:52 PM, e0f said:

Is there a way to put a new field (ex: description) on each menu entries? I like to have something like:


Title: Home
Description: My beautiful home page
URL: /my-beautiful-url/

I wait your feedback.

Thanks and have a nice day!

Hi @e0f,

Currently, this is not doable out of the box. However, using the method getMenuItems() (see a few posts up for examples), you can use the property pagesID (or index pages_id if getting back array of menu items) to get ($pages->get($m->pageID), for example) the respective ProcessWire pages of the menu items and return the custom fields you want, e.g. description. Having said that, I would not recommend this. It seems like an overkill and may not be very performant based on the number of menu items and other factors.

Perhaps if you could explain your use case, I would see if I can add an option to retrieve the values of simple fields (text, integers, dates, etc) for use with getMenuItems(). MenuBuilder is primarily for building navigation menus. From your example, it seems you are using it for a different purpose? Maybe provide a fuller example so that I can get my head around this.

  • Like 1
Link to comment
Share on other sites

12 hours ago, kongondo said:

Hi @e0f,

Currently, this is not doable out of the box. However, using the method getMenuItems() (see a few posts up for examples), you can use the property pagesID (or index pages_id if getting back array of menu items) to get ($pages->get($m->pageID), for example) the respective ProcessWire pages of the menu items and return the custom fields you want, e.g. description. Having said that, I would not recommend this. It seems like an overkill and may not be very performant based on the number of menu items and other factors.

Perhaps if you could explain your use case, I would see if I can add an option to retrieve the values of simple fields (text, integers, dates, etc) for use with getMenuItems(). MenuBuilder is primarily for building navigation menus. From your example, it seems you are using it for a different purpose? Maybe provide a fuller example so that I can get my head around this.

Thanks for answer kongondo.

I need to have a little description appear under my mega menu.

In frontend menu I'll have something like "Home" and under it a little description of the menu entry (ex: my beautiful home page).

So each menu voices will have a custom description on desktop version.

I hope is all clear now. Let me know if you need more infos!

Have a nice day,

Link to comment
Share on other sites

Update: Menu Builder 0.2.7

Changelog

  1. Added the property extra_fields for use with getMenuItems() ONLY. This enables returning the page field values for some selected Fieldtypes. For example, the description field in the page corresponding to the menu item. Thanks @e0f for the inspiration. Full documentation of this new feature is available here.
  2. Menus in multi-lingual sites can now be called using their multi-lingual titles or names irrespective of the current user's language. For example, a multi-lingual site with English, German and Finnish languages could have a menu with the respective titles My Awesome Menu / Mein Tolles Menü / Oma Mahtava Valikko. Any of these titles can be used in the code to call the menu (using render() or getMenuItems() methods). [PS: blame you-know-who for these language translations :-)]
  3. Changed the way new menus are added. Only one menu can be added at a time. The title field/input is multi-lingual ready. See screens below.
  4. Improved the backend GUI when viewing locked menus. Only the menu items and and exit button are shown in such cases.
  5. Fixed bug that allowed access to unpublished menus for frontend rendering.
  6. Updated the documentation
  7. Refactored code to improve efficiency.

In dev branch only for testing.

Screenshots

a. Add Menu (Multi-lingual)

mb-version-27-add-menu-multilingual-screen.thumb.png.1c5654f2eae1e95c5fcee2b4baf5f1d1.png

 

b. Edit Menu (Multi-lingual)

mb-version-27-edit-menu-multilingual-screen.thumb.png.2c5d93ef748b5b9298f51d4ed67ee366.png

c. Locked Menu

mb-version-27-locked-menu-screen.thumb.png.991346ad1977d5d2216c2c872ed96e95.png

d. An example usage of extra_fields (I am no designer, so yeah, nothing much to see here...but you get the idea :-))

mb-version-27-extra-fields-screen.thumb.png.4f981af877a394d4d2175f0294b1181d.png

 

Thanks for testing!

  • Like 2
Link to comment
Share on other sites

i added new option in menu options because i needed css class in <a> tag, searched for a while and didn't find any example so had to do the following.

$NavOptions = array(
                                'wrapper_list_type' => 'ul', // ul, ol, nav, div, etc.
                                'list_type' => 'li', // li, a, span, etc.
                                'menu_css_id' => 'navbarSupportedContent', // a CSS ID for the menu
                                'menu_css_class' => 'navbar-nav ml-auto', // a CSS Class for the menu
                                'current_class' => 'active',
                                'default_title' => 0, // 0=show saved titles;1=show actual/current titles
                                'default_class' => 'nav-item', // a CSS class to apply to all menu items
                                'ahref_class'=>'nav-link'
                            );

 

added following line in MarkupMenuBuilder.module
$this->set('linkClass', isset($options['ahref_class']) ? $options['ahref_class'] : '');

// if $iTag is empty, apply css id and classes to <a> instead
if(!$iTag) $out .= "\n\t<a{$itemCSSID}{$class}{$newtab} href='{$m->url}'>{$m->title}</a>";
else $out .= "\n\t<{$iTag}{$itemCSSID}{$class}>\n\t\t<a{$newtab} href='{$m->url}' class='{$o->linkClass}'>{$m->title}</a>";

suggest me if there is any better way out!!

  • Like 2
Link to comment
Share on other sites

Welcome to the forums @Neeraz,

5 hours ago, Neeraz said:

because i needed css class in <a> tag,

It was decided early on to add classes to the <li> instead of the <a>, as it was the usual practice.

6 hours ago, Neeraz said:

suggest me if there is any better way out!!

Getting the <a> via CSS is quit easy, for instance...

#menu li a {
/* your styles here*/
}

You can be as specific as  you want with your CSS.

6 hours ago, Neeraz said:

added following line in MarkupMenuBuilder.module

Not a good idea as this will get overwritten next time you upgrade Menu Builder :-).

Link to comment
Share on other sites

8 hours ago, kongondo said:

Welcome to the forums @Neeraz,

It was decided early on to add classes to the <li> instead of the <a>, as it was the usual practice.

Getting the <a> via CSS is quit easy, for instance...


#menu li a {
/* your styles here*/
}

You can be as specific as  you want with your CSS.

Not a good idea as this will get overwritten next time you upgrade Menu Builder :-).

i know this but i have no permission to modify customer css file so had to do this, i suggest you to add the same option in future update so it will be more flexible ?

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

Hi there,

I'm with @Neeraz - it would be great to have the possibility to also add a class to the link element (for what reason ever).
Maybe @kongondo it is not a big deal for you to add that functionality to the module? That would be great ?.

Have a nice Friday ?

  • Like 3
Link to comment
Share on other sites

  • 1 month later...

Using this module for the first time, menu created and saved but does not render. Looks like by default it's saved as un-published.

I was expecting to find publish settings as part of the menu settings, however the only way to publish it is via the page tree: Admin > Setup > Menu Builder > *Menu*
Is this intentional? I think from a usability perspective being able to publish the menu as you create it would be helpful.

The only way I could get menu builder to render items was update line 218, MarkupMenuBuilder to:
For some unknown reason, PW doesnt skip language.

// skip inactive language page @note: PW will do this automatically for 'include_children'
                if(!$p->viewable($language&& $languagecontinue;
Link to comment
Share on other sites

On 2/5/2020 at 5:55 AM, Neeraz said:

i know this but i have no permission to modify customer css file so had to do this, i suggest you to add the same option in future update so it will be more flexible

 

On 2/21/2020 at 8:27 AM, Bacelo said:

Maybe @kongondo it is not a big deal for you to add that functionality to the module? That would be great ?.

OK. Added to my todo ?

Link to comment
Share on other sites

16 hours ago, Mackski said:

the only way to publish it is via the page tree: Admin > Setup > Menu Builder > *Menu*

Actually, this is not true ?.

I don't know what version you are using but there has always been a save as unpublished or published during menu creation and an unpublish/publish action that can be applied to existing menu items in the menus list/table. See the big green shiny buttons ?.

Create menu before version 027 

mm_add_new_menu_versions_less_than_027.thumb.png.abef7bcbff9f1c31e4add8ea310b46ff.png

Create menu version since version 027

mm_add_new_menu_versions_from_027.thumb.png.5215c34d9985bcd69fa85b42a36c0f81.png

Unpublish/Publish post menu creation

mm_unpublish_publish_existing_menu.thumb.png.8bdbddb85f2161f191d1144be8d7e145.png

Am I missing something?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...