Jump to content

Menu Builder


kongondo

Recommended Posts

44 minutes ago, Roych said:

not realy working. The children are not native children of a page, they are made with MenuBuilder drag&drop option. I want those in my header main menu, but in my footer I only want to show the first row.

 

That's exactly what I said wouldn't work 🙂. You cannot control non-native children. If the children are MB (drag and drop) children, they are always shown (unless you disable them in the backend using the disable menu item option).

I don't understand what you mean by 'not really working'. A little more detail would help.

Edited by kongondo
Link to comment
Share on other sites

  • 3 months later...

I'have a strange problem, my drag and drop is not working anymore not sure why. All the settings are correct and should be working. It's quite annoying, anybody else have this problem? any ideas how to fix this? Menu is published, unlocked. ...

menu.gif.3a7b50e1cbbf0873fc1596a123bf6c37.gif

Thank you

R

Link to comment
Share on other sites

  • 2 months later...

Hello, @kongondo!

I use the module in this way: I'm creating a site with possibility of creating blank menu without items.

On these lines I'm trying to get menu items:
 

$menuBuilder = $this->modules->get('MarkupMenuBuilder');
$this->menuItems = $menuBuilder->getMenuItems($id);

Then I get an error: "No menu items found! Confirm that such a menu exists and that it has menu items." if items are not present. This is caused by the line 1366:

https://github.com/kongondo/MenuBuilder/blob/main/MarkupMenuBuilder.module#L1366

I tried to fix it manually using the fact that the menu is a page and menu items are being stored in menu_items variable:

$menuBuilder = $this->modules->get('MarkupMenuBuilder');
$menu = $this->pages->get($id);
if($menu->menu_items) {
    $this->menuItems = $menuBuilder->getMenuItems($id);
} else {
    return;
}

However, it seems to be only a workaround. Am I using module correctly? If no, how can I use it to avoid such an error without adding items?

If it is not possible, I suggest the following fix:

Here https://github.com/kongondo/MenuBuilder/blob/main/MarkupMenuBuilder.module#L1366 return can be replaced from $this->throwError() to [] or array().

I created a new issue here: https://github.com/kongondo/MenuBuilder/issues/50. Can you please see this issue?

Link to comment
Share on other sites

Maybe something like this?

$menuBuilder = $this->modules->get('MarkupMenuBuilder');
$menu = $this->pages->get($id);

if ($menu->menu_items->count()) {
    $this->menuItems = $menuBuilder->getMenuItems($id);
} else {
    // Handle the case where there are no menu items, for example:
    $this->menuItems = []; // or any other appropriate action
}

Haven't tested!

This code (should) first checks if there are any menu items by using the count() method on the menu_items field. If there are menu items, it proceeds to fetch and assign them using the getMenuItems method from the MenuBuilder module. If there are no items, you can handle it as needed. In the example above, an empty array is assigned to $this->menuItems, but you can customize it to match your specific requirements.

This approach allows you to use the MenuBuilder module to create menus with or without items, without triggering the error you mentioned.

Cheers,

R

  • Like 1
Link to comment
Share on other sites

On 10/27/2023 at 12:55 PM, Roych said:

Maybe something like this?

$menuBuilder = $this->modules->get('MarkupMenuBuilder');
$menu = $this->pages->get($id);

if ($menu->menu_items->count()) {
    $this->menuItems = $menuBuilder->getMenuItems($id);
} else {
    // Handle the case where there are no menu items, for example:
    $this->menuItems = []; // or any other appropriate action
}

Haven't tested!

This code (should) first checks if there are any menu items by using the count() method on the menu_items field. If there are menu items, it proceeds to fetch and assign them using the getMenuItems method from the MenuBuilder module. If there are no items, you can handle it as needed. In the example above, an empty array is assigned to $this->menuItems, but you can customize it to match your specific requirements.

This approach allows you to use the MenuBuilder module to create menus with or without items, without triggering the error you mentioned.

Cheers,

R

@Roych, as I can see from his post @Clarity is already doing almost the same as you suggested. But he is looking for a cleaner way to handle it that already exist (or asks @kongondoto update the module to provide it).

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...
  • 2 months later...

how to return an array of the menu items? So far I was not successful.

$menu = $modules->get('MarkupMenuBuilder');// $menu is an example
echo $menu->getMenuItems('topmenu'); // topmenu is the name of my menu

But it only returns Menu|Menu|Menu

How can that be?
 

Link to comment
Share on other sites

You need to iterate over the menu items object.

<?php
$mainMenuItems = $mb->getMenuItems('Main navigation', 2, $options);

foreach($mainMenuItems as $menu_item) {
	// You know have access to $menu_item->title
  	// You know have access to $menu_item->url
  	// See the doc for available $menu_item properties
        $has_children = '';
        $has_children_dropdown = '';
        $dropdown = '';
        $tag_class = $menu_item->cssClass!=''?$menu_item->cssClass . ' ' . $has_children:$has_children;

        if($menu_item->isParent) {
            $has_children = 'has-submenu';
            $has_children_dropdown = '<span uk-icon="chevron-down"></span>';
            $children = $wire_pages->get($menu_item->pagesID)->children();

            $dropdown = '<div uk-dropdown="animation: uk-animation-slide-top-small; duration: 300; animate-out: true; offset: 0; pos: bottom-left"><ul class="uk-nav uk-navbar-dropdown-nav">';

            foreach($children as $child) {
                 $dropdown .= '<li><a href="' . $child->url .'">'. $child->title . '</a></li>';
            }
            $dropdown .= '</ul></div>';

        }

        if($menu_item->pagesID == $current_page->parent->id || $menu_item->pagesID == $current_page->id) {
          $item =  '<li class="current-item ' . $tag_class . '"><a href="' . $menu_item->url .'">'. $menu_item->title . $has_children_dropdown . '</a>' . $dropdown . '</li>';
        } else {
          $item = '<li class="'. $tag_class .'"><a href="' . $menu_item->url .'">'. $menu_item->title . $has_children_dropdown . '</a>' . $dropdown . '</li>';
        }
        if($menu_item->parentID == 0) {
            echo $item;
        }
    }

 

  • Like 2
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...