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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Gadgetto
      Status update links (inside this thread) for SnipWire development will be always posted here:
      2019-10-18
      2019-08-08
      2019-06-15
      2019-06-02
      2019-05-25
      If you are interested, you can test the current state of development:
      https://github.com/gadgetto/SnipWire
      Please note that the software is not yet intended for use in a production system (alpha version).
      If you like, you can also submit feature requests and suggestions for improvement. I also accept pull requests.
      ---- INITIAL POST FROM 2019-05-25 ----
      I wanted to let you know that I am currently working on a new ProcessWire module that fully integrates the Snipcart Shopping Cart System into ProcessWire. (this is a customer project, so I had to postpone the development of my other module GroupMailer).
      The new module SnipWire offers full integration of the Snipcart Shopping Cart System into ProcessWire.
      Here are some highlights:
      simple setup with (optional) pre-installed templates, product fields, sample products (quasi a complete shop system to get started immediately) store dashboard with all data from the snipcart system (no change to the snipcart dashboard itself required) Integrated REST API for controlling and querying snipcart data webhooks to trigger events from Snipcart (new order, new customer, etc.) multi currency support self-defined/configurable tax rates etc. Development is already well advanced and I plan to release the module in the next 2-3 months.
      I'm not sure yet if this will be a "Pro" module or if it will be made available for free.
      I would be grateful for suggestions and hints!
      (please have a look at the screenshots to get an idea what I'm talking about)
       




    • By eelkenet
      Hi! I've created a small Inputfield module called InputfieldFloatRange which allows you to use an HTML5 <input type="range" ../> slider as an InputField. I needed something like this for a project where the client needs to be able to tweak this value more based on 'a feeling' than just entering a boring old number. Maybe more people can use this so I'm hereby releasing it into the wild.  
       
      What is it?
      The missing range slider Inputfield for Processwire. 
      What does it do?
      This module extends InputfieldFloat and allows you to use HTML5 range sliders for number fields in your templates.
      It includes a visible and editable value field, to override/tweak the value if required.  
      Features
      Min/max values Precision (number of decimals) Steps (Read more) Manual override of the selected value (will still adhere to the rules above) Usage
      Clone / zip repo Install FieldtypeFloatRange, this automatically installs the Inputfield Create new field of type `Float (range)` or convert an existing `Float`, `Integer` or `Text` field. To render the field's value simply echo `$page->field` Demo
      A field with Min=0, Max=1, Step=0.2, Precision=2

      Field with settings Min=0, Max=200, Step=0.25, Precision=2

       
      Todo
      Make the display-field's size configurable (will use the Input Size field setting)  Hopefully become redundant If it's usable for others I'll add it to the Modules list  
      Changelog
      v002
      - Fix issue where setting the step value to an empty value created problem with validation
      - Make the display-field optional 
      v001
      - Initial release
       
      Thanks!
       
       
    • By Robin S
      Another little admin helper module...
      Template Field Widths
      Adds a "Field widths" field to Edit Template that allows you to quickly set the widths of inputfields in the template.

      Why?
      When setting up a new template or trying out different field layouts I find it a bit slow and tedious to have to open each field individually in a modal just to set the width. This module speeds up the process.
      Installation
      Install the Template Field Widths module.
      Config options
      You can set the default presentation of the "Field widths" field to collapsed or open. Field widths entered into the Template Field Widths inputfield are only applied if the Edit Template form is submitted with the Template Field Widths inputfield in an opened state. "Collapsed" is the recommended setting if you think you might also use core inputs for setting field widths in a template context. You can choose Name or Label as the primary identifier shown for the field. The unchosen alternative will become the title attribute shown on hover. You can choose to show the original field width next to the template context field width.  
      https://github.com/Toutouwai/TemplateFieldWidths
      https://modules.processwire.com/modules/template-field-widths/
    • By adrian
      Tracy Debugger for ProcessWire
      The ultimate “swiss army knife” debugging and development tool for the ProcessWire CMF/CMS

       
      Integrates and extends Nette's Tracy debugging tool and adds 35+ custom tools designed for effective ProcessWire debugging and lightning fast development
      The most comprehensive set of instructions and examples is available at: https://adrianbj.github.io/TracyDebugger
      Modules Directory: http://modules.processwire.com/modules/tracy-debugger/
      Github: https://github.com/adrianbj/TracyDebugger
      A big thanks to @tpr for introducing me to Tracy and for the idea for this module and for significant feedback, testing, and feature suggestions.
×
×
  • Create New...