Skip to content
Grav 2.0 is officially stable. Read the announcement →
Support

Custom menu link / Custom controller

Solved by Francesco Quagliati View solution

Started by Francesco Quagliati 6 years ago · 3 replies · 835 views
6 years ago

Hi! I'm working on a simple website, it has some pages, in a couple of 2-levels menu, let's say:

  • A
    • AA
    • AB
    • AC
  • B
    • BA
    • BB
    • BC

What i'd like is that when i click on A i view directly the AA page, and when i click on B i want to see BB, so when clicking on a menu item with childs, i'd like to go to the first child page.

Is there a way to do so?

A related question is: is there a way to insert a custom menu item with a custom url and a custom controller in code, just to handle this or other situations where the simple folder->file structure is not enough?

I saw this topic, but if possible i'd like to have the first level clickable..
/forum/archive/link-in-menu-shouldnt-be-clickable-t2012

Thanks!

6 years ago Solution

Oh!
I actually just found that (in my subtheme of quark) i can do it in this way, in a copy of templates/macros/macros.html.twig in my theme:

TWIG
{% macro nav_loop(page) %}
  {% import _self as macros %}
  {% for p in page.children.visible %}
    {% set active_page = (p.active or p.activeChild) ? 'active' : '' %}
    {% if p.children.visible.count > 0 %}
      {% set child = p.children.visible|first %}
      <li>
        <a href="{{ child.url }}" class="{{ active_page }}">
          {{ p.menu }}
        </a>
        <ul>
          {{ macros.nav_loop(p) }}
        </ul>
      </li>
    {% else %}
      <li>
        <a href="{{ p.url }}" class="{{ active_page }}">
          {{ p.menu }}
        </a>
      </li>
    {% endif %}
  {% endfor %}
{% endmacro %}

Basically i added that {% set child ..... %} and replaced p.url with child.url in the src.

My question remains thou, at least partially.
From what i can understand the menu is built differently from theme to theme, so there's no a common way to do something to it.
But besides the way it's created, is there a way to tell that at a certain route / url the response have to be given from a custom controller? (let's say that for a single page i need to do some PHP work with some service call or something like that, or create a custom redirect to other pages of the site with certain conditions...

6 years ago

@FrancescoQ, Welcome to the forum.

In general, it is not that handy to ask two distinct questions in one post. Would you mind splitting the post into two separate questions each with a proper title?

Since you have already answered your first question (kudos) , you may mark this thread as being 'solved' by clicking the 'solution' icon in your own reply.

I'd be happy to look into your new post...

6 years ago

You're absolutely right, thanks, i opened a new post for the custom path/route controller
/forum/general/custom-controller-for-a-certain-route-path-t8614

Thanks!

Suggested topics

Topic Participants Replies Views Activity
Support · by Duc , 10 hours ago
1 34 10 hours ago
Support · by Thomas, 1 week ago
3 109 19 hours ago
Support · by Anna, 4 days ago
2 101 2 days ago
Support · by Justin Young, 2 days ago
1 68 2 days ago
Support · by Duc , 1 week ago
2 106 6 days ago