Jump to content

MarkupSimpleNavigation


Soma

Recommended Posts

Any ideas why this works:

'selector_level2' => 'start=0, employee=""',

and this don't:

'selector_level2' => 'start=0, employee!=""', 

In the first example I am excluding people from an author list, showing only the authors that are NOT employees.

The second example works for the authors, but then the other level2 items no longer show (only "Employees") .

(My objective is to show only the authors that are also employees - in my backend I have a checkbox controlling this).

I have also tried with:

employee!=0 and employee=1

Up to now I have worked around this,

'selector_level2' => 'start=0, limit=20,

but employees come and go so this is not the ideal solution.

Any ideas?

-Peter

Link to comment
Share on other sites

I'm not sure and don't have time to test, but the behavior seems correct. All 2th level are affected of course.

I think you would want to define a selector for the parent only using "nav_selector" https://github.com/somatonic/MarkupSimpleNavigation#added-support-for-nav_selector-propertyfield-and-selector_leveln-new-in-121

So you could define the selector on the parent "Employees"

$pages->get("/employees/")->nav_selector = "employee=1";
Link to comment
Share on other sites

Thanks Soma,

That worked. And thanks for a great module!

Now my code looks like this:

$mainMenu = $modules->get("MarkupSimpleNavigation");
$pages->get('/employees/')->nav_selector = 'employee=1';

$options = array(
    'show_root' => true,
    'parent_class' => 'parent',
    'current_class' => 'on',
    'levels' => true,
    'firstlast' => true,
    'max_levels' => 3,
    'collapsed' => false,
    'outer_tpl' => '<ul>||</ul>',
    'inner_tpl' => '<ul>||</ul>',
    'list_tpl' => '<li%s>||</li>',
    'item_tpl' => '<a href="{url}">{title}</a>',
    'selector_level1' => 'start=0',
    'selector_level2' => 'template!=project, start=0',
    'selector_level3' => 'start=0',
    'selector_field' => 'nav_selector'
);

echo $mainMenu->render($options);
Link to comment
Share on other sites

Hi Soma,

I am wondering if I can attach two different hooks to alter the output (my understanding of hooks is very limited at this stage)? I am trying to achieve something similar to this one http://www.abriefhistoryof.info/, the navigation on the left is the same navigation with the one on footer (bottom of the content) except that the one on the bottom has thumbnail and tags. I am thinking to use the hook to resize the thumbnail on the fly but if I do that, the navigation on the left will also output the thumbnail/tags.

If there is a way I can identify the source of hook then this can be solved easily but just not sure how to do so. Cheers

Link to comment
Share on other sites

That is because the module is singular. Same instance same hooks, and as far as I know you can't know what the source is, whatever that would mean.

If you set "singular" => false, you could create a new instance that will be separate with hooks.

But I think you could also define the item content using a property hook and use that to output custom stuff.

wire()->addHookProperty("Page::itemCode", null, "itemCodeHook");

function itemCodeHook($event){
    $page =  $event->object;
    if($page->template == "xyz"){
        $event->return = "<i>your markup for this page</i>";
    }
}

$options = array("item_tpl" => "{itemCode}");
$nav->render($options);

This way you don't need to hook module.

Link to comment
Share on other sites

A good reference would be the HelloWorld.module that comes with PW in /site/modules/. 

Or here is some great summary write up by the great teppo http://www.flamingruby.com/blog/using-hooks-to-alter-default-behavior-of-processwire/

wire()->addHookProperty("Page::itemCode", null, "itemCodeHook");

When a hook is added in template code we use wire(), as this is the base class of PW that contains the hook methods. addHookProperty() adds a new property to the object specified with "Page", so "Page::itemCode" adds a itemCode to page objects. $page->itemCode would return the code returned by the hook. "null" is usually the context for the hook, but doesn't need one in template code. Last argument is the function name of the callback that is used for this hook.

  • Like 2
Link to comment
Share on other sites

Just pushed some updates. Module is now v1.3.0

Form readme:

Changes in 1.3.0

  • Changed module setting singular to false. This enables you load the module as a new instance with $nav = $modules->MarkupSimpleNavigation. This changes behavior for hooks attached to such an instance as it will only be called for this instance. This allows you to create multiple instances of the module to create different navigations with separate hooks.
  • Added support for Page::listable. So you can exclude listing of pages in runtime by modifying this page permission. This can be done using a system hook on Page::listable and set the hook event to return false.
  • Added new option date_format for created and modified native page properties.
  • Added new option code_formatting, to enable or disable code formatting (indentation and newlines) of output markup.
  • Added new option debug that will output rendertime and selector infos as commented inline html.

Added support for Page::listable

Usually you won't need this, but I needed it in a project so thought it wouldn't hurt. Usually you can hide pages from rendering in a list/navigation by setting the page to hidden in the admin. So this additional check in the module itself, allows you to make pages not listed on runtime. This can be used to check for logged in users that only should see the page listed when logged in.

Example hook inside template just before calling render():

wire()->addHookAfter("Page::listable", null, "hookPageListable");
function hookPageListable($event){
    $page = $event->object;
    if($page->template == "internal-page") {
         $event->return = false; // set listable to false if template "internal-page"
    } 
}

Added new option debug

If set to true this option will add an inline

<!-- [MarkupSimpleNavigation Rendertime: 0.0171] -->

 after the markup output to show rendertime, and

 <!-- [Selector Level1: limit=3] -->

 on each item to see selector being used.

This new version should still be compatible with older versions. But there could be instances where it could behave different when you have multiple navigations and using hooks. But chances are very small you run into this.

If you have questions to the above just ask.

  • Like 9
Link to comment
Share on other sites

Hi Soma,

first I thought "nice module, but maybe I should build a own customized template and navigation code" (html, classes, ...), because it's really simple to build with PW... 

After take a look at your documentation I realized...

    'outer_tpl' => '<ul>||</ul>',
    'inner_tpl' => '<ul>||</ul>',
    'list_tpl' => '<li%s>||</li>',
    'list_field_class' => '',
    'item_tpl' => '<a href="/{url}">{title}</a>',
    'item_current_tpl' => '<a href="/{url}">{title}</a>', 

no hardcoded markup! All configurable via options. No reason to build a own template or to reinvent the wheel. 

Markup Simple Navigation is really a great module! I like modules without hardcoded markup or with dedicated templates for easy customization. 

Simply want to say Thank You! :)

  • Like 3
Link to comment
Share on other sites

Just pushed some updates. Module is now v1.3.0

Form readme:

Changes in 1.3.0

  • Changed module setting singular to false. This enables you load the module as a new instance with $nav = $modules->MarkupSimpleNavigation. This changes behavior for hooks attached to such an instance as it will only be called for this instance. This allows you to create multiple instances of the module to create different navigations with separate hooks.
  • Added support for Page::listable. So you can exclude listing of pages in runtime by modifying this page permission. This can be done using a system hook on Page::listable and set the hook event to return false.
  • Added new option date_format for created and modified native page properties.
  • Added new option code_formatting, to enable or disable code formatting (indentation and newlines) of output markup.
  • Added new option debug that will output rendertime and selector infos as commented inline html.

Added support for Page::listable

Usually you won't need this, but I needed it in a project so thought it wouldn't hurt. Usually you can hide pages from rendering in a list/navigation by setting the page to hidden in the admin. So this additional check in the module itself, allows you to make pages not listed on runtime. This can be used to check for logged in users that only should see the page listed when logged in.

Example hook inside template just before calling render():

wire()->addHookAfter("Page::listable", null, "hookPageListable");
function hookPageListable($event){
    $page = $event->object;
    if($page->template == "internal-page") {
         $event->return = false; // set listable to false if template "internal-page"
    } 
}

Added new option debug

If set to true this option will add an inline

<!-- [MarkupSimpleNavigation Rendertime: 0.0171] -->

 after the markup output to show rendertime, and

 <!-- [Selector Level1: limit=3] -->

 on each item to see selector being used.

This new version should still be compatible with older versions. But there could be instances where it could behave different when you have multiple navigations and using hooks. But chances are very small you run into this.

If you have questions to the above just ask.

Haven't seen your post before *g*

Thanks, nice new options :)

Link to comment
Share on other sites

  • 1 month later...

Thanks again for the update Soma!! This also worked to enable the parent's page link on the menu.


// load MarkupSimpleNavigation module
$nav = $modules->get("MarkupSimpleNavigation");
 
/* SETUP HOOKs ------------- */
 
// hook to have custom items markup 
$nav->addHookAfter('getTagsString', null, 'customNavItems');
 
function customNavItems(HookEvent $event){
    $itempage = $event->arguments('page');
 
// first level items with parent page url
if($itempage->numChildren(true) && count($itempage->parents) < 2 && !$itempage->id=1){
   $title = $itempage->get("title|name");
 $event->return = '<a href="#"  data-toggle="dropdown">' . $title . ' <b class="caret"></b></a>';
    }
 
    // submenus don't need class and data attribs
    if($itempage->numChildren(true) && count($itempage->parents) > 1){
        $event->return = '<a href="#">' . $itempage->get("title|name") . '</a>';
    }
}

/* 
  // first level items parent page with #
    if($itempage->numChildren(true) && count($itempage->parents) < 2){
        $title = $itempage->get("title|name");
        $event->return = '<a href="#" class="dropdown-toggle" data-toggle="dropdown">' . $title . ' <b class="caret"></b></a>';
    }
 
    // submenus don't need class and data attribs
    if($itempage->numChildren(true) && count($itempage->parents) > 1){
        $event->return = '<a href="#">' . $itempage->get("title|name") . '</a>';
    }
}
*/ 

// hook to add custom class to li's that have submenu
$nav->addHookAfter('getListClass', null, 'customListClass');
 
function customListClass(HookEvent $event){
    $itempage = $event->arguments('page');
    // if current list item has children and is level 2 from root
    if($itempage->numChildren(true) && count($itempage->parents) > 1){
        $event->return = ' dropdown-submenu'; // adds class to li
    }
}
Link to comment
Share on other sites

  • 1 month later...

Hi!

Fisrt of all: Great module, it saved me a lot of work :)

But i've run into an issue: 

I'm trying to use this as a sidebar navigation, so i figured an accordion style collapsible list would look better.

Usually this would be simple, but bootstraps js uses ids to select the toggleable items. I'd have to put those into 'inner_tpl'.

What i tried was the following:

function sideNavItems(HookEvent $event){
    $item = $event->arguments('page');

    // first level items need additional attr
    if($item->numChildren(true) && count($item->parents) < 2){
        $title = $item->get("title|name");
        $event->return = '<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-target="#'.$item->get("title").'">' . $title . ' <b class="caret"></b></a>';
    }

    // submenus don't need class and data attribs
    if($item->numChildren(true) && count($item->parents) > 1){
        $event->return = '<a href="#">' . $item->get("title|name") . '</a>';
    }
}

(adding the title as data-target). This would work great, if i had the possibility to add an id to the inner_tpl... I can't figure out how to do that. Any suggestions?

Link to comment
Share on other sites

I don't know Bootstrap well and what extraordinary unnecessary complicated data's and id's and classes it needs. Also I don't want to add features just for Bootstrap.

I'm not sure I can follow. What Id is needed where and don't now if it's all possible.

Link to comment
Share on other sites

I looked at some things and pushed some changed to 1.3.1, I think you'll be able to do what you need

Changes in 1.3.1
 
  • Minor refactoring and optimizations
  • Added support for having placeholder in inner_tpl's opening tag.
 
So you can now also add dynamic values to the opening tag like, much like in the item_tpl
'inner_tpl' => "<ul id='{name}' class='drop-down'>||</ul>",
That will result in {name} being replaced by the name of the page (containing the inner childs)
 

To hook into the parsing of the the list opening template you can use the "MarkupSimpleNavigation::getInnerStringOpen" and do you're own conditional replacements.

  • Like 2
Link to comment
Share on other sites

Wow, thank you :D This is what i was looking for. Bootstrap isn't the easiest of all things, but i'm used to it by now, so there's not really a point in learning something new just yet, since i'm in the middle of something... *returns to work*

Thanks again :)

Link to comment
Share on other sites

hi everybody,

just discovered how powerful this module is, but I need a hint. Using this code everything is OK:

     <?php  
       
    $treeMenu = $modules->get("MarkupSimpleNavigation"); // load the module
           
    
    echo $treeMenu->render();
    
    
    ?>   
 

but I need to tweak some parameters, so I added:

	 <?php  
	   
	$treeMenu = $modules->get("MarkupSimpleNavigation"); // load the module
	       
    $options = array(
    'parent_class' => 'parent',
    'current_class' => 'current',
    'has_children_class' => 'has_children',
    'levels' => true,
    'levels_prefix' => 'level-',
    'max_levels' => null,
    'firstlast' => false,
    'collapsed' => false,
    'show_root' => false,
    'selector' => '',
    'selector_field' => 'nav_selector',
    'outer_tpl' => '<ul>||</ul>',
    'inner_tpl' => '<ul>||</ul>',
    'list_tpl' => '<li%s>||</li>',
    'list_field_class' => '',
    'item_tpl' => '<a href="{url}">{title}</a>',
    'item_current_tpl' => '<a href="{url}">{title}</a>',
    'xtemplates' => '',
    'xitem_tpl' => '<span>{title}</span>',
    'xitem_current_tpl' => '<span>{title}</span>',
    'date_format' => 'Y/m/d',
    'code_formatting' => false,
    'debug' => false
    )
    
    echo $treeMenu->render($options); 
    
    
    ?>		

and here's the result:

Parse Error:     syntax error, unexpected T_ECHO (line 110 of /home/morgenle/public_html/new/site/templates/header.inc)

Link to comment
Share on other sites

That just means you didn't properly close some tags or use a semi-colon to tell PHP you have come to the end of a line just before you come to the echo...and...as you can see, you are missing a semi-colon ; after the closing ) in your array...Hence, the echo is "unexpected....you haven't told PHP you have finished your array :-)....and it's telling you your syntax is wrong...

  • Like 3
Link to comment
Share on other sites

I need to improve my copy & paste php.

Thanks kongondo

Then you also need to be careful about hidden ° nonbreaking spaces as well. They can drive you nuts with PHP throwing errors you "can't" see visually. I usually sort them out in Word.

Link to comment
Share on other sites

Actually I had the same problem. There are some syntax errors on the module page which I think catches a lot of people out.

// removed by soma

Missing ; after closing )

// removed by soma

Missing , after 

'xitem_current_tpl' => '<a href="{url}">{title}</a>'

and missing ; after closing )

Link to comment
Share on other sites

It's on purpose so you have to think a little and not just blindly copy paste! :P

Seriously I fixed them a while ago I thought, must have messed up something with having various versions. I'll correct them. Thanks

(I'll remove you're code as it's not interesting to leave a copy of it here)

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...