Jump to content
Soma

MarkupSimpleNavigation

Recommended Posts

I have played with selectors a little.

My task is solved by

$options = array('selector' => 'id|parent=1006|1003');

1006 and 1003 - ids of "Want in menu 1|2" in my structure.

So we can use multiple "parents" =)

  • Like 1

Share this post


Link to post
Share on other sites

Just wanted to add that parent=1006|1003 will only give direct children of those two pages and not recursively all children and grandchildren... So once you get on 3rd level it will stop. This may not an issue just a limit of this use.

There's a select field has_parent=1006 which does include all that are in that parent with all subchilds, though it doesn't allow for multiple only one ID.

So going templates or checkbox way is most flexible and solid solution.

Edit: you could work around the limit also with something like this (being creative)

<ul class="nav">
<?php echo $nav->render(array('selector' => 'id|has_parent=1006', 'outer_tpl' => '||' )); // outer ul empty ?>
<?php echo $nav->render(array('selector' => 'id|has_parent=1003', 'outer_tpl' => '||' )); ?>
</ul>
  • Like 1

Share this post


Link to post
Share on other sites

You could also have a template "redirect" with only a title and a page field (single page) to select any page you want to go.

Then in the code you should be able to write

array('item_tpl' => '<a href="{redirect_page.url|url}">{title}</a>')

So it would take the url of the referenced page in the page field if found, or if not it takes the url of the page itself. Assuming the page field is named "redirect_page".

Haven't tried but should work.

I tried this but all I am getting is a "page you were looking for is not found" error. The URL is not displaying the redirected page URL. I'm probably doing something wrong since I am still very new to all this.  Great, great module btw!! Thanks for making!

Share this post


Link to post
Share on other sites

@n0sleeves

No you're not doing anything wrong. As I said I didn't test this, just thought it would work.

I found it's not working the way I thought with a page field "pagefieldname.url". MarkupSimpleNavigation uses the $page->get("fieldname|otherfieldname") method to get  values, and subfields don't seem to work here.

So I just went and added this feature so you can now in 1.1.8 use page fields like 'redirect_page' (without url) and it will detect it's a page and spit its url.

To make it work the page field has to be set to "Single Page or false".

'item_tpl' => '<a href="{redirect_page|url}">{title}</a>'

This is now in pushed and available on modules repository.

  • Like 1

Share this post


Link to post
Share on other sites

So I just went and added this feature so you can now in 1.1.8 use page fields like 'redirect_page' (without url) and it will detect it's a page and spit its url.

To make it work the page field has to be set to "Single Page or false".

I have that set. Is there anything else I need to set?

'item_tpl' => '<a href="{redirect_page|url}">{title}</a>'

This is now in pushed and available on modules repository.

Wow, that was fast! You are amazing! Unfortunetly, it's still throwing the same error to me as before, "The page you were looking for is not found.Please use our search engine or navigation above to find the page.".

Here is my php code below in case I'm missing something.

$treeMenu = $modules->get("MarkupSimpleNavigation"); 
$rootPage = $pages->get("/photos/");

echo $treeMenu->render( array(
'collapsed' => true, 
 'item_tpl' => '<a href="{redirect_page|url}">{title}</a>'
), null, $rootPage); 

Thanks so much!

Share this post


Link to post
Share on other sites

Well it works. This module doesnt throw 404 so there must be something wrong with the url the item gets from the page field. Make sure the redirect_page is populated with a page that is viewable.

Share this post


Link to post
Share on other sites

Hi Soma and All,

Thanks, Soma, for building this excellent MODX Wayfarer replacement. I've got it working with a special selector query,

'show_in_menu=1', which makes me happy, happy.

But now, I've been looking around for some actual CSS examples, the way the old Wayfinder page used to have.

You know, working examples of using it for a dropdown / flyout menu, etc.

Does anyone have any links that use Soma's module? Menu CSS details make my brain freeze. I'm not sure why. :-)

Thanks!

Peter

Share this post


Link to post
Share on other sites

Dear Soma,

Also, as a followup, is there a way to add a parameter to the module so that a url or redirect_url could have a flag set in the page to open the url in a new tab?

I.e. "target=_blank" if "open_in_new_tab=1" ?

MODX had that with weblinks, and it was very cool. Most of the external url redirects that would appear on a menu (in my case) should be opened in a new tab.

I added the target=_blank to the parameter:

    'item_tpl' => '<a href="{redirect_url|url}" target="_blank">{title}</a>',

but then that opens all links in a new tab.

Thanks!

Peter

Share this post


Link to post
Share on other sites

Hey Peter glad you like it.

1. I think there's tons of examples out there. I agree it can be tricky if you're not into it so much. I often use a drop-down script (with some basic CSS) I modified some time ago, it's also used in my Teflon theme. https://github.com/somatonic/droppy it doesn't require anything except jQuery and some CSS. There's also CSS only ways to do it.

2. I'm a little careful not to add even more options. This I agree can be handy, since it's also possible use external urls.

I think there's different ways to achieve it already what you want without adding more. xtemplates, xitem_tpl, options for example, but it would only be possible to add it to all links that have the template you define there.

But there's another way that is possible since a couple versions. Adding a hook to the link item being constructed and modify it. Here simple example for use in templates:

function getTagsString(HookEvent $event){
    $tpl = $event->arguments[0]; // the template string
    $page = $event->arguments[1]; // the page getting rendered
    if($page->open_blank){ 
        // $event->return is the complete link you get from the hook, modify to you needs
        $event->return = str_replace('href=','target="_blank" href=', $event->return);
    }
}
$treeMenu->addHookAfter('getTagsString', null, "getTagsString");

echo $treeMenu->render(); 
  • Like 1

Share this post


Link to post
Share on other sites

Dear Soma,

Thanks for that! Hey, now I'm using my Very First Hook! Woohoo! :-)

Your code works like a charm. Am I correct in thinking that I can rename the 3rd parameter, i.e. the local function?

That's what I did, and it seems to work. Here's my code:

function redirect_url_target_fix(HookEvent $event)
      {
      $page = $event->arguments[1];

      if($page->open_in_new_tab)
            {
            $event->return = str_replace('href=','target="_blank" href=', $event->return);
            }
      }

$treeMenu->addHookAfter('getTagsString', null, "redirect_url_target_fix");

echo $treeMenu->render($tree_menu_options); 

Best regards,

Peter

  • Like 1

Share this post


Link to post
Share on other sites

I'm using Font Awesome, and wanted to be able to add icon markup to parent pages only.

Soma mentioned using "xtemplates", but in my case parent and child pages all use the same template.

So after I flew to Switzerland to hand Soma a beer, he suggested I use a hook.

This may be useful to someone else, so here's what I did:

1. add a text field called "icon" to your template.

2. add the code below before you call $treeMenu->render();

function addIcon(HookEvent $event){
$page = $event->arguments[1];
    if ($page->icon){
        $event->return = "<a href='{$page->url}'><i class='icon icon-{$page->icon}'></i> {$page->title}</a>";
    }
} 

$treeMenu->addHookAfter('getTagsString', null, "addIcon");
  • Like 4

Share this post


Link to post
Share on other sites

what about 2.3 compatibility?

I couldn't find the module in the list of 2.3 compatible modules...

No it's 2.3 compatible, but every new version has a new checkbox to check and I haven't done it yet. I think there's many modules that suffer from this.

  • Like 1

Share this post


Link to post
Share on other sites

No it's 2.3 compatible, but every new version has a new checkbox to check and I haven't done it yet. I think there's many modules that suffer from this.

ahh super! thx

Share this post


Link to post
Share on other sites

Soma (or anyone else that might know),

I need to have some role specific pages in my navigation (i.e. only a user with "travel admin" role sees the "travel admin" navigation item).

I'm using, Custom Page Roles module to define access on a per page basis—so I'm able set the view access as needed. However, those pages still appear in the navigation for all users.

I would usually exclude pages from the navigation by setting the page to hidden, but that won't work in this case, because the page would be hidden from everyone.

Is it possible to hide navigation items when the page is not viewable to that user?

Share this post


Link to post
Share on other sites

Hi Tom, I did this but it was a while ago and I am up against a deadline so can't spend long right now but just in case this code is self explanatory I paste it in the hope it helps, I am 99.9% sure I solved it with the great Soma's help ;)

$nav = $modules->get("MarkupSimpleNavigation"); // load the module and have an instance kept in $nav var
$options = array(
        'levels' => true,
        'show_root' => true,
        'item_tpl' => '<a href="{url}">{title} <em>{headline}</em></a>',
        'item_current_tpl' => '<a href="{url}">{title} <em>{headline}</em></a>',
        'xtemplates' => 'editors_only',
        'xitem_tpl' => '<a class="editors_only" href="{url}">{title} <span>Editors only</span><em>{headline}</em></a>',
        'xitem_current_tpl' => '<a href="{url}">{title} <em>{headline}</em></a>'
);
echo $nav->render($options);

Hope it helps!

Share this post


Link to post
Share on other sites

Thanks Alan,

I think that solution still requires I use a separate template to define view access - right?

I'm building an admin area and will end up with a variety of different roles (some of which only need access to a single additional page).

Adding new templates just to manage access could get messy quickly in this case. Custom Page Roles is perfect—assuming there's a way to make it play nicely with MarkupSimpleNavigation.

Share this post


Link to post
Share on other sites

Ah, gotchya, sorry not able to help :/

Share this post


Link to post
Share on other sites

Soma (or anyone else that might know),

I need to have some role specific pages in my navigation (i.e. only a user with "travel admin" role sees the "travel admin" navigation item).

I'm using, Custom Page Roles module to define access on a per page basis—so I'm able set the view access as needed. However, those pages still appear in the navigation for all users.

I would usually exclude pages from the navigation by setting the page to hidden, but that won't work in this case, because the page would be hidden from everyone.

Is it possible to hide navigation items when the page is not viewable to that user?

The CustomPageRoles module is alpha, proof of concept and it says that pages still will be rendered in navigation. You'd have to exclude them when cycling the pages in the loop with $page->viewable().

But implementing this check in the modules manager would get kinda complicated and result in some potential problems with how it all works together.

The MarkupSimpleNavigation is access aware, but CustomPageRoles doesn't exclude pages when using with find() or children() calls, that's why it doesn't work also with MSN (heh).

You can do use page_roles (page field) to do what you want with a simple check for user roles simply with this:

echo $treeMenu->render(array("selector" => "page_roles={$user->roles}"));

Because of the use of a selector you have to add and define page roles for all pages you have in navigation. It's not using the viewable access method, but simply checking the page field for if any of the user's roles is added.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks Soma,

That method works. :)

The MarkupSimpleNavigation is access aware

When I was just trying to use templates to define access it didn't seem to be access aware.

Possibly because defining a root page?

$rootPage = $pages->get("/faculty/");

Edit: Hmm, nope, doesn't seem to be access aware even without that.

Share this post


Link to post
Share on other sites

If the /faculty/ template does have access rights set, yes then it wouldn't work as you get the page explicitly with get("/faculty/").

But for children when I remove view access for a role they won't show up in navigation unless you explicitly check the option to show them in lists and searches.

  • Like 1

Share this post


Link to post
Share on other sites

Yep, that was it — /faculty/ didn't have access rights set.

My brain gets scrambled at times with access control, especially when jumping between template based and page based control.

Is it beer:30 yet?

;)

Share this post


Link to post
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 Sten
      Hello
      Till now I hacked something with the twig template but it works no more with new PW versions so I look forward to create a module. I am working on a site in multiple languages : French, English, Italian, German, Spanish, Portuguese, Hebrew, Russian. The new posts are entered in any language with a field for language. Till now, I got twig files to get the translations with constants defined for each part of the pages.
      So I'd like to create a module to include theses files added according to the url /fr/en/...
      Have you some observations to do before I begin about the direction to take ?
      Thank you
    • By ukyo
      Mystique Module for ProcessWire CMS/CMF
      Github repo : https://github.com/trk/Mystique
      Mystique module allow you to create dynamic fields and store dynamic fields data on database by using a config file.
      Requirements
      ProcessWire 3.0 or newer PHP 7.0 or newer FieldtypeMystique InputfieldMystique Installation
      Install the module from the modules directory:
      Via Composer:
      composer require trk/mystique Via git clone:
      cd your-processwire-project-folder/ cd site/modules/ git clone https://github.com/trk/Mystique.git Module in live reaction with your Mystique config file
      This mean if you remove a field from your config file, field will be removed from edit screen. As you see on youtube video.
      Using Mystique with your module or use different configs path, autoload need to be true for modules
      Default configs path is site/templates/configs/, and your config file name need to start with Mystique. and need to end with .php extension.
      Adding custom path not supporting anymore !
      // Add your custom path inside your module class`init` function, didn't tested outside public function init() { $path = __DIR__ . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR; Mystique::add($path); } Mystique module will search site/modules/**/configs/Mystique.*.php and site/templates/Mystique.*.php paths for Mystique config files.
      All config files need to return a PHP ARRAY like examples.
      Usage almost same with ProcessWire Inputfield Api, only difference is set and showIf usage like on example.
      <?php namespace ProcessWire; /** * Resource : testing-mystique */ return [ 'title' => __('Testing Mystique'), 'fields' => [ 'text_field' => [ 'label' => __('You can use short named types'), 'description' => __('In file showIf working like example'), 'notes' => __('Also you can use $input->set() method'), 'type' => 'text', 'showIf' => [ 'another_text' => "=''" ], 'set' => [ 'showCount' => InputfieldText::showCountChars, 'maxlength' => 255 ], 'attr' => [ 'attr-foo' => 'bar', 'attr-bar' => 'foo' ] ], 'another_text' => [ 'label' => __('Another text field (default type is text)') ] ] ]; Example:
      site/templates/configs/Mystique.seo-fields.php <?php namespace ProcessWire; /** * Resource : seo-fields */ return [ 'title' => __('Seo fields'), 'fields' => [ 'window_title' => [ 'label' => __('Window title'), 'type' => Mystique::TEXT, // or InputfieldText 'useLanguages' => true, 'attr' => [ 'placeholder' => __('Enter a window title') ] ], 'navigation_title' => [ 'label' => __('Navigation title'), 'type' => Mystique::TEXT, // or InputfieldText 'useLanguages' => true, 'showIf' => [ 'window_title' => "!=''" ], 'attr' => [ 'placeholder' => __('Enter a navigation title') ] ], 'description' => [ 'label' => __('Description for search engines'), 'type' => Mystique::TEXTAREA, 'useLanguages' => true ], 'page_tpye' => [ 'label' => __('Type'), 'type' => Mystique::SELECT, 'options' => [ 'basic' => __('Basic page'), 'gallery' => __('Gallery'), 'blog' => __('Blog') ] ], 'show_on_nav' => [ 'label' => __('Display this page on navigation'), 'type' => Mystique::CHECKBOX ] ] ]; Searching data on Mystique field is limited. Because, Mystique saving data to database in json format. When you make search for Mystique field, operator not important. Operator will be changed with %= operator.
      Search example
      $navigationPages = pages()->find('my_mystique_field.show_on_nav=1'); $navigationPages = pages()->find('my_mystique_field.page_tpye=gallery');
    • By Robin S
      This is a module I made as an experiment a while ago and never got around to releasing publicly. At the time it was prompted by discussions around using Repeater fields for "page builder" purposes, where the depth feature could possibly be used for elements that would be nested inside other elements. I thought it would be useful to enforce some depth rules and translate the depth data into a multi-dimensional array structure.
      I'm not using this module anywhere myself but maybe it's useful to somebody.
      Repeater Depth Helper
      This module does two things relating to Repeater fields that have the "Item depth" option enabled:
      It enforces some depth rules for Repeater fields on save. Those rules are:
      The first item must have a depth of zero. Each item depth must not be more than one greater than previous item depth. It provides a RepeaterPageArray::getDepthStructure helper method that returns a nested depth structure for a Repeater field value.
      Helper method
      The module adds a RepeaterPageArray::getDepthStructure method that returns a multi-dimensional array where the key is the page ID and the value is an array of nested "child" items, or null if there are no nested children.
      Example

      The module doesn't make any assumptions about how you might want to use the depth structure array, but here is a way you might use it to output a nested unordered list.
      // Output a nested unordered list from a depth structure array function outputNestedList($depth_structure, $repeater_items) { $out = "<ul>"; foreach($depth_structure as $page_id => $nested_children) { $out .= "<li>" . $repeater_items->get("id=$page_id")->title; // Go recursive if there are nested children if(is_array($nested_children)) $out .= outputNestedList($nested_children, $repeater_items); $out .= "</li>"; } $out .= "</ul>"; return $out; } $repeater_items = $page->my_repeater; $depth_structure = $repeater_items->getDepthStructure(); echo outputNestedList($depth_structure, $repeater_items);
       
      https://github.com/Toutouwai/RepeaterDepthHelper
      https://modules.processwire.com/modules/repeater-depth-helper/
    • By MoritzLost
      Cacheable Placeholders
      This module allows you to have pieces of dynamic content inside cached output. This aims to solve the common problem of having a mostly cacheable site, but with pieces of dynamic output here and there.  Consider this simple example, where you want to output a custom greeting to the current user:
      <h1>Good morning, <?= ucfirst($user->name) ?></h1> This snippet means you can't use the template cache (at least for logged-in users), because each user has a different name. Even if 99% of your output is static, you can only cache the pieces that you know won't include this personal greeting. A more common example would be CSRF tokens for HTML forms - those need to be unique by definition, so you can't cache the form wholesale.
      This module solves this problem by introducing cacheable placeholders - small placeholder tokens that get replaced during every request. The replacement is done inside a Page::render hook so it runs during every request, even if the response is served from the template cache. So you can use something like this:
      <h1>Good morning, {{{greeting}}}</h1> Replacement tokens are defined with a callback function that produces the appropriate output and added to the module through a simple hook:
      // site/ready.php wire()->addHookAfter('CachePlaceholders::getTokens', function (HookEvent $e) { $tokens = $e->return; $tokens['greeting'] = [ 'callback' => function (array $tokenData) { return ucfirst(wire('user')->name); } ]; $e->return = $tokens; }); Tokens can also include parameters that are parsed and passed to the callback function. There are more fully annotated examples and step-by-step instructions in the README on Github!
      Features
      A simple and fast token parser that calls the appropriate callback and runs automatically. Tokens may include multiple named or positional parameters, as well as multi-value parameters. A manual mode that allows you to replace tokens in custom pieces of cached content (useful if you're using the $cache API). Some built-in tokens for common use-cases: CSRF-Tokens, replacing values from superglobals and producing random hexadecimal strings. The token format is completely customizable, all delimiters can be changed to avoid collisions with existing tag parsers or template languages. Links
      Github Repository & documentation Module directory If you are interested in learning more, the README is very extensive, with more usage examples, code samples and usage instructions!
    • By MoritzLost
      This module allows you to integrate hCaptcha bot / spam protection into ProcessWire forms. hCaptcha is a great alternative to Google ReCaptcha, especially if you are in the EU and need to comply with privacy regulations.

      The development of this module is sponsored by schwarzdesign.
      The module is built as an Inputfield, allowing you to integrate it into any ProcessWire form you want. It's primarily intended for frontend forms and can be added to Form Builder forms for automatic spam protection. There's a step-by-step guide for adding the hCaptcha widget to Form Builder forms in the README, as well as instructions for API usage.
      Features
      Inputfield that displays an hCaptcha widget in ProcessWire forms. The inputfield verifies the hCaptcha response upon submission, and adds a field error if it is invalid. All hCaptcha configuration options for the widget (theme, display size etc) can be changed through the inputfield configuration, as well as programmatically. hCaptcha script options can be changed through a hook. Error messages can be translated through ProcessWire's site translations. hCaptcha secret keys and site-keys can be set for each individual inputfield or globally in your config.php. Error codes and failures are logged to help you find configuration errors. Please check the README for setup instructions.
      Links
      Github Repository and documentation InputfieldHCaptcha in the module directory (pending approval) Screenshots (configuration)

      Screenshots (hCaptcha widget)

       
       

       
×
×
  • Create New...