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

  On 12/26/2013 at 2:25 PM, Soma said:

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

  On 3/11/2014 at 8:15 PM, Marco Angeli said:

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
×
×
  • Create New...