Jump to content

Menu Builder


kongondo

Recommended Posts

3 hours ago, Mike Rockett said:

It does appear to work, but the root page is also assigned the current class when viewing any decsendant.

My bad! I forgot to do that check. We check by looking at $page->parents....Since home is everybody's parents, it passes the logic. I'll fix this in the next commit. 

3 hours ago, Mike Rockett said:

Is there no clean way to add an option to specific menu items that states "add current class here if an included / non-included descendant is current"?

RE 'specific menu items':

Unlike the backend per-menu-item setting Include natural children, you cannot cherry pick which items to apply current class to. That has to be done at a global level, aka $options.  For the benefit of others reading this, in this regard, 'current_class' and 'current_class_level' work globally. For 'current_class_level' = 0 we target current $page that is not part of the MB menu items (aka non-included) in order to apply 'current_class' to its NATURAL (ProcessWire) ancestors in the MB menu. For 'current_class_level' > 1, we target current $page that is either part of the MB menu or that was made part of the MB menu via 'include_children' in order to apply 'current_class' to itself as well as its Menu Builder ancestors in the MB menu if applicable. 

Back to Mike's question, as stated in some previous posts, for such fine-grained/specific control, I recommend using the getMenuItems() method with your own custom function.  Please see this post for examples as well as custom functions + gists. Please note that currently, getMenuItems() does not support 'include_children' but it will soon.

Link to comment
Share on other sites

5 minutes ago, kongondo said:

My bad! I forgot to do that check. We check by looking at $page->parents....Since home is everybody's parents, it passes the logic. I'll fix this in the next commit. 

I thought that may be the case -- thanks!

And thanks for the explanation, I understand how it works now.

Link to comment
Share on other sites

Update: Menu Builder Version 0.2.1.

Changelog

  1. Fixed bug in 'current_class_level' where 'current_class' was being applied to 'Home' menu item when level was set to '0'.
  2. getMenuItems() now honours both locally and globally set 'include_children'.
  3. Added 'cached_menu' option to MarkupMenuBuilder to save and fetch menus from cache to speed up menu rendering if required.
  4. Added 'cached_menu_time' to set cached menu expiration time. For compatible times, see WireCache docs.
  5. Fixed bug in ProcessMenuBuilder where 'include_children' and 'm_max_level' inputs would be displayed when the page selected to add to menu was 'Home' (These options do not apply to Home).

Note

Cached Menu

  • The benefits of this feature will likely be small for most sites as menu rendering is already fast. However, for cases where you have lots of items in your menu (or in your site map if you are using Menu Builder for such), you will notice some difference. In addition, since cached menus save the full menu items array to the database (JSON of items only; not the markup!), it means you avoid the internal multiple pages->get() for getting menu items. You only hit the database once or twice (WireCache) and you got your menu! The feature also works with multi-lingual sites, in which case each language's menu is cached separately. Anyway, the feature is there for those that need it.
  • Please note that the feature does not work with getMenuItems() since in such cases you are retrieving menu items to do what you will with them (even cache them yourself if you wish).
  • Limitations: Currently, cached menus ONLY respect 'current_class_level' 0 and 1 (the default). This is because the 'current-ness' of a menu item should not be cached! That has to be checked live [EDIT].

 

News features in dev branch only for now. Please let me know how it goes, thanks.

Edited by kongondo
Oversights and then some!
  • Like 2
Link to comment
Share on other sites

37 minutes ago, Mike Rockett said:

@kongondo Thanks! Works like a charm now.

Good to know. Thanks for testing!

Please note I've just pushed a new commit (but did not change versions) to fix some oversight in the last commit. It should not affect you unless you are using the cached menu feature. I've also updated my previous post.

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Hi! kongondo, Menu Builder is vety easy to use and powerful, But now I'm going to add some css3 effects to the menu.

Can you add a custom tpl parameter to the list item.

E.g. I want to make it

<li> <a href="">item name</a></li>

into

<li> <a href=""><span data="item name">item name</span></a></li>

The final effect is like this webpage's top menu:

http://mo005-3785.mo5.line1.uemo.net/list/id/37389/

Link to comment
Share on other sites

  • 3 weeks later...

Hi @kongondo

Thank you for so great module!

I use 0.1.7 version of module and get 

PHP Notice: Array to string conversion in ...\ProcessMenuBuilder\MarkupMenuBuilder.module:922

Here is how I use it

$menu_page = pages(1023);
if($lang_name == "ukrainian") {
    $menu_page = pages(1024);
}

$menu = $menuModule->getMenuItems($menu_page, 1, [
    "current_class_level" => 2
]);

Changing this line

$menuItems = '';

to 

$menuItems = array();

fixes it. 

Am I missing something? 

Link to comment
Share on other sites

Hi @Zeka

Yeah, PHP 7 is a little more stricter. PHP 5 forgave such indiscretions :).  If you could open a bug report for me so that I don't forget to fix it, I'll appreciate it (along with other issues that have been recently reported). Thanks.

  • Like 1
Link to comment
Share on other sites

On 7/20/2017 at 12:38 PM, kongondo said:

Update: Menu Builder Version 0.1.9.

Changelog

  1. Added support for multi-lingual menu items (both PW pages and custom ones)! Thanks @Michael van Laar for 'reminder'.

I'm busy with a multi-lingual site for a client, and this feature doesn't appear to work on my end - I'm getting the error "No menu items found" when switching to the only non-default language.

My menu, called footerNavLeft, is set to use French, and the language tabs are being added to the menu builder (and inheriting default values as expected). When opening the site in English (default language), all is fine. The moment I switch to French, it goves the above-mentioned error.

I'm rendering the menu to my view like this:

$menus = wire('modules')->MarkupMenuBuilder;
$view->menuFooterNavLeft = $menus->render('footerNavLeft', [
    'menu_css_class' => 'menu',
]);

infinitygroupcm_pw.lserv-2017-10-01-17-28-15.screengrab.jpeg.25b227270af4ae500a3c764e21ca2508.jpeg

 

 

---

Edit: Apologies - missed the instructions (which aren't clear in the post quoted above). Using a Page ID works. Though, I hve to ask: why isn't it possible to use a menu name/title in a multi-lingual environment?

Edited by Mike Rockett
Oops!
Link to comment
Share on other sites

1 hour ago, Mike Rockett said:

Apologies - missed the instructions (which aren't clear in the post quoted above). Using a Page ID works. Though, I hve to ask: why isn't it possible to use a menu name/title in a multi-lingual environment?

Ahem...really Mike? :'(....the docs, Mike, the docs:P

Quote

The first argument is not optional and can be a Page object, a title, name or id of a menu or an array of menu items returned from a menu's menu_items field. Note that for multilingual environments, you cannot pass the method a title or a name; only the other three choices will work. The second argument is an optional array and will fall back to defaults if no user configurations are passed to the method.

Glad you got it sorted :):).

  • Like 1
Link to comment
Share on other sites

15 minutes ago, kongondo said:

Ahem...really Mike? :'(....the docs, Mike, the docs:P

Glad you got it sorted :):).

Works flawlessly by the way. :-) I haven't perused the source yet, but would it be possible for you to make reference by name/title work, considering that it doesn't change on a per-language basis? 

Link to comment
Share on other sites

1 minute ago, Mike Rockett said:

would it be possible for you to make reference by name/title work, considering that it doesn't change on a per-language basis? 

I suppose so. I struggle with the ML API to be honest and could do with some help. Do I tell PW to get the page (menu) with the title of the default language? Thanks for any pointers/code, ta.

Link to comment
Share on other sites

26 minutes ago, kongondo said:

I suppose so. I struggle with the ML API to be honest and could do with some help. Do I tell PW to get the page (menu) with the title of the default language? Thanks for any pointers/code, ta.

I'm no expert either, but I feel the calls made to search for menu pages in MarkupMenuBuilder.module:142-144 are a bit much... On my side, changing it to the following fixes the issue, but I honestly don't know if it breaks anything else...

$menuName = $this->wire('sanitizer')->pageName($menu);
$menu = $this->wire('pages')->get("name=$menuName, template=menus, parent.name=menu-builder, check_access=0");

 

Edited by Mike Rockett
Refer to specific commit for when code changes...
Link to comment
Share on other sites

Wrapping inner <ul> into div?

thanks for your great module!

With the uikit nav + dropdown i need to wrap an inner <ul> into a <div>. Thats fairly easy with MarkupSimpleNavigation "inner_tpl" but how to do it in this case?

Uikit example:

<a href"#">Menuitem</a>
<div uk-dropdown>
  <ul class="uk-nav uk-dropdown-nav">...</ul>
</div>

thanks!

Link to comment
Share on other sites

1 hour ago, kater said:

Wrapping inner <ul> into div?

thanks for your great module!

With the uikit nav + dropdown i need to wrap an inner <ul> into a <div>. Thats fairly easy with MarkupSimpleNavigation "inner_tpl" but how to do it in this case?

Menu Builder doesn't support that out of the box, and so you'll need to use getMenuItems(), which allows you to build your menu the way you want it. Here are some gists that could guide you for your menu.

  • Like 1
Link to comment
Share on other sites

Hi @kongondo

I have this setup: 

  • MenuItem1
    • Submenuitem_01( allowed children)
    • SubmenuItem_02 ( allowed children)
  • MenuItem2
    • Submenuitem_03 ( allowed children)
    • SubmenuItem_02 ( allowed children)

I'm using getMenuItems for building my menu, but the issue is that I get children of Submenuitem_02 as children only of the last Submenuitem_02 ( parent_id  =  id of the last Submenuitem_02 ), but I expected that I will get duplications of children pages with right parent_id for every Submenuitem_02

Of course, I can get children in my custom function but I think that in this case, getMenuItems doesn't work as expected and could be improved.

What do you think about it? 

Thanks, Eugene. 

Link to comment
Share on other sites

  • 1 month later...

I am currently getting a fatal error on a multilanguage site. On the german version, there is no problem, the menu is rendering just as expected, but when I try to open the english or dutch version I am getting the following message:

Error: Exception: No menu items found! Confirm that such a menu exists and that it has menu items. (in /Applications/MAMP/htdocs/cleaninstall/site/modules/ProcessMenuBuilder/MarkupMenuBuilder.module line 959)

#0 /Applications/MAMP/htdocs/cleaninstall/site/modules/ProcessMenuBuilder/MarkupMenuBuilder.module(87): MarkupMenuBuilder->throwError()
#1 /Applications/MAMP/htdocs/cleaninstall/site/templates/partials/header.inc(110): MarkupMenuBuilder->render('main', Object(MenuOptions))
#2 /Applications/MAMP/htdocs/cleaninstall/wire/core/TemplateFile.php(259): require('/Applications/M...')
#3 /Applications/MAMP/htdocs/cleaninstall/wire/core/Wire.php(380): ProcessWire\TemplateFile->___render()
#4 /Applications/MAMP/htdocs/cleaninstall/wire/core/WireHooks.php(698): ProcessWire\Wire->_callMethod('___render', Array)
#5 /Applications/MAMP/htdocs/cleaninstall/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\TemplateFile), 'render', Array)
#6 

 

The mentioned line 110 of header.inc looks like this: 

<?php
$options = array(
  'menu_css_class' => 'menu-lvl-1',
  'submenu_css_class' => 'menu-lvl-2',
  'current_class' => 'active',
  'has_children_class' => 'has_children',
  'current_class_level' => 3,
  'include_children' => 1
);
?>
<?php echo $menu->render('main', $options); ?>

The Processwire version is 3.0.62 and yes, I already checked if there were menu items in there and I also created another menu to confirm that the error persists.

Link to comment
Share on other sites

48 minutes ago, thmsnhl said:

I am currently getting a fatal error on a multilanguage site. On the german version, there is no problem, the menu is rendering just as expected, but when I try to open the english or dutch version I am getting the following message:

 

48 minutes ago, thmsnhl said:

<?php echo $menu->render('main', $options); ?>

https://github.com/kongondo/MenuBuilder#api

render()

This method renders a menu/navigation list of a specified menu. The method accepts two arguments/parameters:

render($menu, $options);

The first argument is not optional and can be a Page object, a title, name or id of a menu or an array of menu items returned from a menu's menu_items field. Note that for multilingual environments, you cannot pass the method a title or a name; only the other three choices will work. The second argument is an optional array and will fall back to defaults if no user configurations are passed to the method.

@thmsnhl

That should resolve your issue :).

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

@kongondo First: thank you for your response and sorry for my delayed reply. I had some other projects crashing my work on that one.

With your help I was able to render the menu also on localized sites. The code now looks like this:

<?php
$options = array(
'menu_css_class' => 'menu-lvl-1',
'submenu_css_class' => 'menu-lvl-2',
'current_class' => 'active',
'has_children_class' => 'has_children',
'current_class_level' => 3,
'include_children' => 1
);
?>
<?php echo $menu->render($pages->get("id=1930"), $options); ?>

But: The menu does not show the localized page titles, those are still German. Am I missing anything else?

 

Edit: I solved it, I just realised that I was missing the following option to translate the titles.

'default_title' => 1

 

  • Like 1
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 monollonom
      (once again I was surprised to see a work of mine pop up in the newsletter, this time without even listing the module on PW modules website 😅. Thx @teppo !)
      FieldtypeQRCode
      Github: https://github.com/romaincazier/FieldtypeQRCode
      Modules directory: https://processwire.com/modules/fieldtype-qrcode/
      A simple fieldtype generating a QR Code from the public URL of the page, and more.
      Using the PHP library QR Code Generator by Kazuhiko Arase.

      Options
      In the field’s Details tab you can change between .gif or .svg formats. If you select .svg you will have the option to directly output the markup instead of a base64 image. SVG is the default.
      You can also change what is used to generate the QR code and even have several sources. The accepted sources (separated by a comma) are: httpUrl, editUrl, or the name of any text/URL/file/image field.
      If LanguageSupport is installed the compatible sources (httpUrl, text field, ...) will return as many QR codes as there are languages. Note however that when outputting on the front-end, only the languages visible to the user will be generated.
      Formatting
      Unformatted value
      When using $page->getUnformatted("qrcode_field") it returns an array with the following structure:
      [ [ "label" => string, // label used in the admin "qr" => string, // the qrcode image "source" => string, // the source, as defined in the configuration "text" => string // and the text used to generate the qrcode ], ... ] Formatted value
      The formatted value is an <img>/<svg> (or several right next to each other). There is no other markup.
      Should you need the same markup as in the admin you could use:
      $field = $fields->get("qrcode_field"); $field->type->markupValue($page, $field, $page->getUnformatted("qrcode_field")); But it’s a bit cumbersome, plus you need to import the FieldtypeQRCode's css/js. Best is to make your own markup using the unformatted value.
      Static QR code generator
      You can call FieldtypeQRCode::generateQRCode to generate any QR code you want. Its arguments are:
      string $text bool $svg Generate the QR code as svg instead of gif ? (default=true) bool $markup If svg, output its markup instead of a base64 ? (default=false) Hooks
      Please have a look at the source code for more details about the hookable functions.
      Examples
      $wire->addHookAfter("FieldtypeQRCode::getQRText", function($event) { $page = $event->arguments("page"); $event->return = $page->title; // or could be: $event->return = "Your custom text"; }) $wire->addHookAfter("FieldtypeQRCode::generateQRCodes", function($event) { $qrcodes = $event->return; // keep everything except the QR codes generated from editUrl foreach($qrcodes as $key => &$qrcode) { if($qrcode["source"] === "editUrl") { unset($qrcodes[$key]); } } unset($qrcode); $event->return = $qrcodes; })
    • By Sebi
      AppApiFile adds the /file endpoint to the AppApi routes definition. Makes it possible to query files via the api. 
      This module relies on the base module AppApi, which must be installed before AppApiFile can do its work.
      Features
      You can access all files that are uploaded at any ProcessWire page. Call api/file/route/in/pagetree?file=test.jpg to access a page via its route in the page tree. Alternatively you can call api/file/4242?file=test.jpg (e.g.,) to access a page by its id. The module will make sure that the page is accessible by the active user.
      The GET-param "file" defines the basename of the file which you want to get.
      The following GET-params (optional) can be used to manipulate an image:
      width height maxwidth maxheight cropX cropY Use GET-Param format=base64 to receive the file in base64 format.
    • 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 for modifying the stored value by the value (converted as required) in another measurement. 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.

       
×
×
  • Create New...