Jump to content

Menu Builder


kongondo

Recommended Posts

17 hours ago, MateThemes said:

How can I close the <div class="uk-navbar-dropdown">? Everything I've tried seems not to work!

Try this one:

Please note that the \t and \n need cleaning up.

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 .= "\n<ul class='uk-navbar-nav'>";
							$first = 1;
						}
						else {
							$out .=
								"\n\t<div class='uk-navbar-dropdown'>" .
									"\n\t\t<ul class='uk-nav uk-navbar-dropdown-nav'>";
						}
				}
				// active/current menu item
				$class = $m->isCurrent ? ' class="uk-active"' : '';

				// a menu item
				$out .= "\n\t<li$class><a href='{$m->url}{$newtab}'>{$m->title}</a>";

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

		}// end if parent

	}// end foreach

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

	return $out;

}

// example usage
                              
$mb = $modules->get('MarkupMenuBuilder');
$menuItemsAsObject = $mb->getMenuItems(1041, 2);
$menu = "\n<div class='uk-navbar-center uk-visible@m'>";
$menu .= buildMenuFromObject(0, $menuItemsAsObject);
$menu .= "</div>\n";

echo $menu;

 

  • Like 1
Link to comment
Share on other sites

7 hours ago, kongondo said:

Try this one:

Please note that the \t and \n need cleaning up.


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 .= "\n<ul class='uk-navbar-nav'>";
							$first = 1;
						}
						else {
							$out .=
								"\n\t<div class='uk-navbar-dropdown'>" .
									"\n\t\t<ul class='uk-nav uk-navbar-dropdown-nav'>";
						}
				}
				// active/current menu item
				$class = $m->isCurrent ? ' class="uk-active"' : '';

				// a menu item
				$out .= "\n\t<li$class><a href='{$m->url}{$newtab}'>{$m->title}</a>";

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

		}// end if parent

	}// end foreach

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

	return $out;

}

// example usage
                              
$mb = $modules->get('MarkupMenuBuilder');
$menuItemsAsObject = $mb->getMenuItems(1041, 2);
$menu = "\n<div class='uk-navbar-center uk-visible@m'>";
$menu .= buildMenuFromObject(0, $menuItemsAsObject);
$menu .= "</div>\n";

echo $menu;

 

Thank you very much!!! This works great!!!

Link to comment
Share on other sites

Good day @kongondo and everyone here!

I am trying to get my menus with MenuBuilder to work, but having some strange issue.

This is my code:

<?php namespace ProcessWire;

$menuBuilder = $modules->get('MarkupMenuBuilder');
$mainMenuLeft = $menuBuilder->getMenuItems('main-menu-left');
bd($mainMenuLeft, '$mainMenuLeft');

And this is a screen of MenuBuilder admin page:

MB.png.fe6d855303423b126109462901e0199e.png

The problem is I get 0 items. And only if I add the homepage I get 1 (and this is the homepage).

Either I am doing something wrong or something got broken) Please help me!

Link to comment
Share on other sites

1 hour ago, Ivan Gretsky said:

Either I am doing something wrong or something got broken) Please help me!

If the pages are not viewable they won't appear in the menu/won't be returned as menu items. Make sure the templates they use have a template file and there aren't any other restrictions.

  • Like 3
Link to comment
Share on other sites

Update: Menu Builder Version 0.2.5.

Just a quick maintenance update.

Changelog

  1. Fixed a minor bug where we had to check if a variable was an array first before "counting" it.
  2. Removed a leftover debugging statement from the code (an echo of all things! Not even a bd()!!) 😁.
  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Hi,

I'm wondering how unpublished (or hidden) pages could "disappear" from the menu, without having to remove it from the menu?

The context : I have some pages in menu. My user need to unpublished a page not necessary for a temporary period. But it's not automatically removed from the menu. I now deleted from the menu, but I'm quite sure than she will want to re-published it, she will call me because it will not be present in the menu. (I gave her permissions on menu but if I could avoided to touch too much those settings, I will be happy... Anyway, in theory she shouldn't have to modify anything on that module on regular basis).

Thanks

Mel

 

 

Link to comment
Share on other sites

48 minutes ago, mel47 said:

I'm wondering how unpublished (or hidden) pages could "disappear" from the menu, without having to remove it from the menu?

Hi,

Have a look at your menus settings, specifically the 'Use enable/disable menu items feature'. This requires the permission 'menu-builder-disable-items'. When enabled, you will see under each menu item's settings panel, a checkbox 'Disabled'. Tick that for the menu items you wish to disable and voila; no need to delete them. However, if you do not wish the user to be going to these settings, enabling and disabling menu items, you can instead use getMenuItems() and filter out unpublished and hidden pages. If you can open an issue in the project repo, I can consider to add these as options i.e.  'skip_unpublished'  and 'skip_hidden'. I can't remember of the top of my head how ProcessWire page->viewable() relates to hidden and unpublished pages. We already check for viewable() in menu builder but I'd have to investigate this further.

 

  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...

Hi, 

I have some issues since the last update of the module.
If a menu item has children it doesn't get the class provided in the options array (in my case 'menu-item-has-children').
The a tag is not even in a li?!?!

Here is my code:

$options = array(
  'wrapper_list_type' => 'ul',
  'list_type' => 'li',
  'menu_css_id' => '',
  'menu_css_class' => 'menu',
  'submenu_css_class' => 'sub-menu',
  'has_children_class' => 'menu-item-has-children',
  'first_class'=>'',
  'last_class' => '',
  'current_class' => 'current-menu-item',
  'default_title' => 1  ,
  'include_children' => 4,
  'm_max_level' => 2,
  'current_class_level' => 1,
  'default_class' => 'menu-item',
);
  $menu = $modules->get('MarkupMenuBuilder');
  echo $menu->render('hauptmenu', $options);

And this is what comes out (look at Fotografie for example😞
 

<ul class="menu">
  <li class="menu-item">
    <a href="/" class="pm_text_menu_item">Start</a>
  </li>
  	<a href="/fotografie/" class="pm_text_menu_item">Fotografie</a>
  <ul class="sub-menu">
    <a href="/fotografie/landschaften/" class="pm_text_menu_item">Landschaften</a>
    <ul class="sub-menu">
      <li class="menu-item">
        <a href="/fotografie/landschaften/wald/" class="pm_text_menu_item">Wald</a>
      </li>
      <li class="menu-item">
        <a href="/fotografie/landschaften/strand-kuste/" class="pm_text_menu_item">Strand &amp; Küste</a>
      </li>
    </ul>
    <a href="/fotografie/tiere/" class="pm_text_menu_item">Tiere</a>
    <ul class="sub-menu">
      <li class="menu-item">
        <a href="/fotografie/tiere/amphibien-reptilien/" class="pm_text_menu_item">Amphibien &amp; Reptilien</a>
      </li>
      <li class="menu-item">
        <a href="/fotografie/tiere/arthropoden/" class="pm_text_menu_item">Arthropoden</a>
      </li>
      <li class="menu-item">
        <a href="/fotografie/tiere/saugetiere/" class="pm_text_menu_item">Säugetiere</a>
      </li>
      <li class="menu-item">
        <a href="/fotografie/tiere/vogel/" class="pm_text_menu_item">Vögel</a>
      </li>
    </ul>
    <li class="menu-item">
      <a href="/fotografie/stimmungen/" class="pm_text_menu_item">Stimmungen</a>
    </li>
  </ul>
  <li class="menu-item">
    <a href="/video/" class="pm_text_menu_item">Video</a>
  </li>
  <li class="menu-item current-menu-item">
    <a href="/daniel-holte/" class="pm_text_menu_item">Daniel Holte</a>
  </li>
  <li class="menu-item">
    <a href="/kontakt/" class="pm_text_menu_item">Kontakt</a>
  </li>
  <li class="menu-item">
    <a href="/impressum/" class="pm_text_menu_item">Impressum</a>
  </li>
</ul>

So as you can see the li tags for menu items with children are completly missing. I have no idea why and as I said above, I changed nothing except the upadte of the module.

Thanks in advance

Link to comment
Share on other sites

1. Try rendering the menu without $options.

$menu = $modules->get('MarkupMenuBuilder');
echo $menu->render('hauptmenu');

If the markup is fine then something within your $options doesn't work or

2. Look into your menu settings within the builder. There could be some settings too.

 

I just tried it with version 0.2.5 (which seems to be the latest version) and my menu markup is fine.

Link to comment
Share on other sites

1 hour ago, neonwired said:

Found a bit of a bug. If you add javascript:void(0) in the link field in a custom link it deletes the item.

I wouldn't call it a bug. That's the sanitizer at work. For custom things like that, I suggest you use getMenuItems() method to create your menus. See the examples in that link.

Link to comment
Share on other sites

  • 2 weeks later...
On 4/30/2019 at 4:31 PM, neonwired said:

I disagree, it's perfectly legitimate to use it. I don't do code snobbery.

"Anti-snobbery" resulted in my boss recently sending me an email with a link to javascript:void(0);

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

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.

Link to comment
Share on other sites

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

After adding page number 89 moving items isn't possible anymore as the change won't be saved.

Hi @uiui. I am afraid I don't follow. Could you please explain what page number 89 is?

Also, your ProcessWire version please.

Link to comment
Share on other sites

Just now, kongondo said:

Hi @uiui. I am afraid I don't follow. Could you please explain what page number 89 is?

Also, your ProcessWire version please.

Hi @kongondo,

I'm working with version 3.0.132.

The menu that produces this error contains 88 pages and a couple of custom links. As soon as I add a page that is counted as number 89, the menu won't save anymore. The last page added doesn't seem to be the origin of the problem as I tried it with a number of different pages.

Link to comment
Share on other sites

1 hour ago, uiui said:

The menu that produces this error contains 88 pages and a couple of custom links. As soon as I add a page that is counted as number 89, the menu won't save anymore. The last page added doesn't seem to be the origin of the problem as I tried it with a number of different pages.

I've just tested and it works fine. I have created a menu with 93 items. I started with 87 then added more.

  1. How are you adding the menu items? Asm select / Auto complete / Page List / Selector?
  2. Are the menus nested?
Link to comment
Share on other sites

7 hours ago, kongondo said:

I've just tested and it works fine. I have created a menu with 93 items. I started with 87 then added more.

  1. How are you adding the menu items? Asm select / Auto complete / Page List / Selector?
  2. Are the menus nested?

1. ASM

2. Yes, two levels.

Link to comment
Share on other sites

1 hour ago, uiui said:

1. ASM

2. Yes, two levels.

I have tested again, this time adding items one at a time using ASM select, creating a two-level deep menu with 93 items. Some of the items were repeated though.  I also added a number of custom menu items. 

Are you able to show me screenshot, or, preferably, a video of the offending menu? The video would need to show the bit about the 89th item please. Ideally, have Tracy Debugger on to catch any errors as well. 

  • Is this a local or remote site?
  • Single or Multilingual? 
  • Third-party modules in use?
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By MarkE
      This fieldtype and inputfield bundle was built for storing measurement values within a field, rendering them in a variety of formats and converting them to other units or otherwise modifying them via the API.
      The API consists of a number of predefined functions, some of which include...
      render() for rendering the measurement object, valueAs() for converting the value to another unit value, convertTo() for converting the whole measurement object to different units, and add() and subtract() for creating a new measurement object from the sum or difference between two other objects. In the admin the inputfield includes a checkbox (which can be optionally disabled) for converting values on page save. For an example if a value was typed in as centimeters, the unit was changed to metres, and the page saved with this checkbox selected, said value would be automatically converted so that e.g. 170 cm becomes 1.7 m.
      A simple length field using Fieldtype Measurement and Inputfield Measurement.
      Combination units (e.g. feet and inches) are also supported.
      Please note that this module is 'proof of concept' at the moment - there are limited units available and quite a lot of code tidying to do. More units will be added shortly.
      See the GitHub at https://github.com/MetaTunes/FieldtypeMeasurement for full details and updates.
    • By tcnet
      File Manager for ProcessWire is a module to manager files and folders from the CMS backend. It supports creating, deleting, renaming, packing, unpacking, uploading, downloading and editing of files and folders. The integrated code editor ACE supports highlighting of all common programming languages.
      https://github.com/techcnet/ProcessFileManager

      Warning
      This module is probably the most powerful module. You might destroy your processwire installation if you don't exactly know what you doing. Be careful and use it at your own risk!
      ACE code editor
      This module uses ACE code editor available from: https://github.com/ajaxorg/ace

      Dragscroll
      This module uses the JavaScript dragscroll available from: http://github.com/asvd/dragscroll. Dragscroll adds the ability to drag the table horizontally with the mouse pointer.
      PHP File Manager
      This module uses a modified version of PHP File Manager available from: https://github.com/alexantr/filemanager
       
    • By tcnet
      This module implements the website live chat service from tawk.to. Actually the module doesn't have to do much. It just need to inserted a few lines of JavaScript just before the closing body tag </body> on each side. However, the module offers additional options to display the widget only on certain pages.
      Create an account
      Visit https://www.tawk.to and create an account. It's free! At some point you will reach a page where you can copy the required JavaScript-code.

      Open the module settings and paste the JavaScript-code into the field as shown below. Click "Submit" and that's all.

      Open the module settings
      The settings for this module are located int the menu Modules=>Configure=>LiveChatTawkTo.

       
    • By tcnet
      Session Viewer is a module for ProcessWire to list session files and display session data. This module is helpful to display the session data of a specific session or to kick out a logged in user by simply delete his session file. After installation the module is available in the Setup menu.

      The following conditions must be met for the module to work properly:
      Session files
      Session data must be stored in session files, which is the default way in ProcessWire. Sessions stored in the database are not supported by this module. The path to the directory where the session files are stored must be declared in the ProcessWire configuration which is by default: site/assets/sessions.
      Serialize handler
      In order to transform session data easier back to a PHP array, the session data is stored serialized. PHP offers a way to declare a custom serialize handler. This module supports only the default serialize handlers: php, php_binary and php_serialize. WDDX was dropped in PHP 7.4.0 and is therefore not supported by this module as well as any other custom serialize handler. Which serialize handler is actually used you can find out in the module configuration which is available under Modules=>Configure=>SessionViewer.

      Session data
      The session data can be displayed in two different ways. PHP's default output for arrays print_r() or by default for this module nice_r() offered on github: https://github.com/uuf6429/nice_r. There is a setting in the module configuration if someone prefers print_r(). Apart from the better handling and overview of the folded session data the output of nice_r() looks indeed nicer.

      Links
      ProcessWire module directory
      github.com
    • By Robin S
      Repeater Easy Sort
      Adds a compact "easy-sort" mode to Repeater and Repeater Matrix, making those fields easier to sort when there are a large number of items.
      The module also enhances Repeater Matrix by allowing a colour to be set for each matrix type. This colour is used in the item headers and in the "add new" links, to help visually distinguish different matrix types in the inputfield.
      Screencasts
      A Repeater field

      A Repeater Matrix field with custom header colours

      Easy-sort mode
      Each Repeater/Matrix item gets an double-arrow icon in the item header. Click this icon to enter easy-sort mode.
      While in easy-sort mode:
      The items will reduce in width so that more items can be shown on the screen at once. The minimum width is configurable in the field settings. Any items that were in an open state are collapsed, but when you exit easy-sort mode the previously open items will be reopened. You can drag an item left/right/up/down to sort it within the items. The item that you clicked the icon for is shown with a black background. This makes it easier to find the item you want to move in easy-sort mode. You can click an item header to open the item. An "Exit easy-sort mode" button appears at the bottom of the inputfield. Configuration
      In the field settings for Repeater and Repeater Matrix fields you can define a minimum width in pixels for items in easy-sort mode. While in easy-sort mode the items will be sized to neatly fill the available width on any screen size but will never be narrower than the width you set here.
      In the field settings for Repeater Matrix you can define a custom header colour for each matrix type using an HTML "color" type input. The default colour for this type of input is black, so when black is selected in the input it means that no custom colour will be applied to the header.
      Exclusions
      The easy-sort mode is only possible on Repeater/Matrix fields that do not use the "item depth" option.
       
      https://github.com/Toutouwai/RepeaterEasySort
      https://processwire.com/modules/repeater-easy-sort/
×
×
  • Create New...