Jump to content

Menu Manager


joshuag
 Share

Recommended Posts

Hi all,

Thinking about how I could create a menu manager to use in my processwire websites. I use the module Markup Simple Navigation all the time and that is fantastic, or generate custom menus with the API.

However, continually I am faced with the challenge of including multiple navigation items in my menus to pages that exist somewhere else in the page tree...

A couple of times in life I have created page selector fields or trees in the pagetree that represent menus, but this never seems like the best solution and I always design it in a way that doesn't seem 100% intuitive.

I am all for reducing the amount of choices the users have to make about managing the content and structure of their websites, but this keeps happening to me. Even if there was a way that I could do this for myself so that I could organize menus myself somehow.

So looking for feedback. How do other people deal with this problem? I hope my description was clear.

Thanks in advance for any input.

- joshua

Link to comment
Share on other sites

ok another idea... I just though of creating a template... and the only field that the template would have would be a page field. So that we could add "pages" to the website and all they do is link or redirect (best option) to the REAL page that is selected in the page field, then the "fake page" would output in my menu... although again, this seems like a complicated solution to a simple problem... and I would like to abstract as much of this away from the users as possible... hmmmm.

Link to comment
Share on other sites

I almost always try to structure sites in a manner is consistent structurally and navigationally. I also think this is one thing that differentiates ProcessWire sites from some other CMSs (like Drupal), where the navigation structure is completely disconnected from the content organization -- it's maddening. :) So I think it's good to consider structure and navigation in the same process when possible. If you have a huge disconnect between the two, it's good to look at re-evaluating the structure. But I totally understand the need to have a custom menu in one place, or insert a page from a menu in another place, etc. Here's how I do it:

When you need to have a navigation link in an existing structure, even though the page isn't in that structure, you can give it a placeholder. Create a field called "redirect_url" of type "URL". Create a template called "redirect" and add your "redirect_url" field to it. Then paste the following in your /site/templates/redirect.php file:

<?php $session->redirect($page->redirect_url);  

Now whenever you need an extra navigation item somewhere in your structure, without the page actually being there, create a placeholder page using the "redirect" template. Put in the URL to the page you want it to go to. When the page is viewed, it'll end up where it's supposed to go.

If you want to avoid the 301 redirect, you can also just update your navigation output code to give preference to a redirect_url field when it exists. And it'll use redirect_url when it's populated, and url when it's not:

<a href="<?=$page->get('redirect_url|url')?>"><?=$page->title?></a>

When you need to create a custom menu that has nothing to do with the site's structure, use a page reference field. For instance, lets say you wanted to create a "footer_links" navigation. You'd create a new multi-page reference field called "footer_links". Choose "Page List Select Multiple" as the Input field for it. Add this field to your homepage template. Edit your homepage and use that field to select the pages, anywhere in your site, that will be in the footer links navigation. Then output them like this:

<ul id='footer_links'>
<?php foreach($pages->get('/')->footer_links as $item): ?>
<li><a href='<?=$item->url?>'><?=$item->title?></a></li>
<?php endforeach; ?>
</ul>

Let me know if I've answered consistently with what you were looking for, or if you are trying to do something else?

  • Like 2
Link to comment
Share on other sites

Menu's can get messy so i generally stick to navigations which follow the tree structure. If it's only a couple of pages outside of the structure you could append or prepend..etc.

- Ryan beat me to it -

Couldn't agree more.

Link to comment
Share on other sites

The necessity to include "virtual" pages in the page tree comes often from the customer. So you have no choice.

Then

<a href="<?=$page->get('redirect_url|url')?>"><?=$page->title?></a>

instead of a redirect is preferable. But there is an other reason you might wish this "short cuts".

If you expect external links to change frequently and you have them scattered over your site you would appreciate the benefit of changing them only at one well known place. So if you do some work on it, I suggest not to limit yourself to the menu.

  • Like 1
Link to comment
Share on other sites

@ryan - I did exactly that.. a redirect template, but instead of a field for a url, I used a page input field. Works wonders. Totally happy with this solution, even the redirect is ok IMO. Although easy (like you said) to modify the navigation output to give preference to the page field url instead.

I agree wholeheartedly that the navigation in almost all cases should reflect that page tree, and normally that is what happens.

This is exactly why I love PW. No joke. I feel like anything is possible and I always end up with an awesome solution that doesn't complicate things.

:)

  • Like 4
Link to comment
Share on other sites

Ok guys i´m not so confirm with php ... but who can i give this navigation link an additional Option like a "target" ??

The reason is that i´m don´t see the fun of it when the user click on that link and leave my site for ever ... i find the better solution is to open a new window.

By the way i use this method from ryan:

...

When you need to have a navigation link in an existing structure, even though the page isn't in that structure, you can give it a placeholder. Create a field called "redirect_url" of type "URL". Create a template called "redirect" and add your "redirect_url" field to it. Then paste the following in your /site/templates/redirect.php file:

<?php $session->redirect($page->redirect_url); 

Now whenever you need an extra navigation item somewhere in your structure, without the page actually being there, create a placeholder page using the "redirect" template. Put in the URL to the page you want it to go to. When the page is viewed, it'll end up where it's supposed to go.

If you want to avoid the 301 redirect, you can also just update your navigation output code to give preference to a redirect_url field when it exists. And it'll use redirect_url when it's populated, and url when it's not:

<a href="<?=$page->get('redirect_url|url')?>"><?=$page->title?></a>
Link to comment
Share on other sites

Why not just specify it in your html output:

<a target="_blank" href="<?=$page->get('redirect_url|url')?>"><?=$page->title?></a>

If you need an option to choose between opening your redirect page in a new tab/window and the same window you could add a new field to your redirect template, let's call it "blank" and the caption "Open in a new window?" with the type of checkbox. And then check against it in your code.

So it would look something like this:

$blankAttr = $page->blank ? 'target="_blank"' : '';

<a <?=$blankAttr?> href="<?=$page->get('redirect_url|url')?>"><?=$page->title?></a>

Edit: also it should be possible to detect whether your url is intenal or external using some tricky regexp and avoid using additional field, but my knowledge of regexps is very limited so can't advise here.

Cheers.

  • Like 2
Link to comment
Share on other sites

Sorry slkwrm my mistake...

it´s late but didn´t use this code from ryan

If you want to avoid the 301 redirect, you can also just update your navigation output code to give preference to a redirect_url field when it exists. And it'll use redirect_url when it's populated, and url when it's not:

<a href="<?=$page->get('redirect_url|url')?>"><?=$page->title?></a>

because i didn´t understand it :-[

The only thing i have done is this one and there are no "a href" tag in it soo this is my problem?!

When you need to have a navigation link in an existing structure, even though the page isn't in that structure, you can give it a placeholder. Create a field called "redirect_url" of type "URL". Create a template called "redirect" and add your "redirect_url" field to it. Then paste the following in your /site/templates/redirect.php file:

<?php $session->redirect($page->redirect_url); 

Now whenever you need an extra navigation item somewhere in your structure, without the page actually being there, create a placeholder page using the "redirect" template. Put in the URL to the page you want it to go to. When the page is viewed, it'll end up where it's supposed to go.

I thought i need that second code from ryan or now from you only when i would change the integration of the navigation or when i would put this link "outside" a normal navigation ...

ok its tooo late i will look tomorrow a second time above - hope that will help.

cu

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...