1

I am using a structure for navigation. The structure has two fields : entry field: 'contentLink' custom link: 'customLink'

These are used to create a menu structure like: About - sub item 1 - sub item 2 - sub item 3

The section for this is set to have no url templates setup.

The entry links for sub item 1 to 3 point at entries in a page structure called 'pages' which is currently flat (ie no children)

In my template i am using a drop-down menu and code to get child items which works OK. {% set menuentries = craft.entries.section('topnav') %}

{% for entry in menuentries %}

    {% if entry.contentLink.first|length %}
        {# attempt to set navlink var #}
        {% set navlink = entry.contentLink.first().slug %}

    {% elseif entry.customLink|length %}

        {% set navlink = entry.customLink %}

    {% endif %}

     {% if entry.level == 1 and entry.children|length %}
        <li class="dropdown">
            <a href="{{ entry.url }}" class="dropdown-toggle" data-toggle="dropdown">{{ entry.title }} <span class="caret"></span></a>

            <ul class="dropdown-menu" role="menu">
                {% for child in entry.getDescendants() %}
                    <li class="">
                        <a href="{{ siteUrl }}{{ child.slug }}">{{ child.title }}</a>
                    </li>
            {% endfor %}
             </ul>
         </li>

    {% elseif entry.level == 1 and not entry.hasDescendants() %}

         <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
    {% endif %}
{% endfor %}

But i cant figure out how to get: a) a url for child.slug that reflects the heirarchy of the menu structure b) parent and child active classes for highlighting / breadcrumb purposes

question restated: How can I output child urls that reflect the a structured menu hierarchy like: 'about/sub item1' when the structure has no URL format specified?

1

Craft can't really produce the uri patterns/routes you need automatically without giving it something to work with. With that said there are a couple of ways to approach this.

1) One option is to simply make your pages section into a structure; creating the needed hierarchy elements, and use it directly to generate your navigation. You may also want to specify additional 'entryType(s)' for header items, etc (i.e. items that don't have a corresponding page and/or are used exclusively for creating the nested route structure or for navigation headers, buttons, etc). You can then test entry.type in your template and deny or reroute the request accordingly.

2) Another option is to define a url format for your nav structure, which represents the uri pattern(s) that you would like to have (i.e. if the structure represents the site root then set the uri to {slug} and children to {parent.uri}/{slug}).

Using this approach, you would want to set up a template for your navigation structure which essentially includes the corresponding page template, as needed.

Something like this perhaps:

{% if entry is defined %}
    {% set relatedPage = entry.contentLink.first %}
    {% type = relatedPage.type %}
    {% include ["pages/_types/" ~ type, "pages/_types/default"] with { 'entry': relatedPage } %}
{% else %}
    {% redirect '404' %}
{% endif %}

Notice that when including the page template, we are redefining the entry variable to be the related page, so that the 'page' template doesn't have to worry about it, and can be loaded directly or via the navigation template as needed.

Note: you may want to disable urls for your pages channel if you don't ever want users to access pages through those routes.

  • Thanks Douglas. I see the dilemma I present when no Url format exists. The approach I took seems widespread but I thought I was missing something special. It seems I cant find a detailed example of building a nested structure navigation with hierarchical url routes that have active parent and child items. I think option 1 sounds like i might need some singles as 'index' pages to channels when there is a bit more content required above a list of entries. – joomkit Mar 22 '16 at 22:49
  • And no 2 sounds like it might be my solution for now - a pure navigation template. I am not sure I quite understand the purpose of redefining entry as 'related' - what problem does this solve? – joomkit Mar 22 '16 at 22:54
  • If you did not redefine 'entry', then when you included the page template, the page template would have to reference content via the relatedPage variable (i.e. {{ entry.relatedPage.title }} ) — the 'entry' variable would refer to the navigation entry, not the related page entry. Redefining it would allow the page template to refer to entry as if it had been requested directly. Does that make sense? – Douglas McDonald Mar 22 '16 at 23:09
  • As to your first question, I personally like to use option 1 for most of my projects. I typically use a 'pages' structure that represents all top level pages in the site, including index pages. I then use separate sections/channels to hold repeating content (i.e. products, news, case studies, projects, people, whatever the case may be), and set up their Url structures to seemlessly mesh with the pages structure. (i.e. pages would define the uri 'news'.. and the channel 'news' would begin with the uri news/{slug}. – Douglas McDonald Mar 22 '16 at 23:17
  • Thanks - i get the need for redefining entry. Still figuring out the rest ;) – joomkit Mar 23 '16 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.