0

I am building a navigation section which is a structure. The idea is that it is a flexible place to build links to other entries/ custom urls etc. It has one entry.type called page which has an entries Field called 'entryLink' to link to other entries.

It has no urls of its own set in 'Entries in this section have their own URLs'.

When i try to get the entriesField called 'entryLink' all i can get is the title:

 {% set navEntries = craft.entries.section('navigation').hidePage('< 1')  %}
   {% for nav in navEntries %}
     <p> {{ nav.entryLink.first() }}</p>
 {% endfor %}

If i try and get the slug or uri I always get an error. Eg when using :

 {% set navEntries = craft.entries.section('navigation').hidePage('< 1')  %}
   {% for nav in navEntries %}
     <p> {{ nav.entryLink.first().uri }}</p>
 {% endfor %}

I get

Impossible to access an attribute ("uri") on a null variable

What am I missing? I notice the documentation here: https://craftcms.com/docs/entries-fields specifies 'target locale' which i do not have in my entrie field setup.

  • If you solved your own problem, please post it as an answer instead of adding it to the question. You can accept your own answer. – Lindsey D Nov 25 '16 at 3:22
1

So this is what worked for me. It's a bootstrap drop-down menu system using the nav tag from the structure 'menu' with custom fields as described above

<ul class="nav navbar-nav navbar-right">
{% set menu = craft.entries.section('menu').hidePage('< 1') %}
{% set navlink = "" %}

{% nav entry in menu %}
    {% if entry.entryLink.first|length %}
    {% set navlink = entry.entryLink.first.uri %}
    {% else %}
    {% set navlink = entry.customMenuUrl %}
    {% endif %}

    {% if entry.level == 1 and entry.children|length %}
        <li class="dropdown">
            <a  href="{{ siteUrl }}{{ navlink}}"  role="button" data-hover="dropdown" data-length="{{ entry.title|length }}" aria-haspopup="true" aria-expanded="false">{{ entry.title }}<span class="caret"></span></a>
            <!-- get children -->
            <ul class="dropdown-menu" >
                <!-- get children -->
                {% for child in entry.getDescendants().hidePage('< 1') %}
                {% if child.entryLink.first|length %}

                <li> <a  href="{{ siteUrl }}{{  child.entryLink.first.uri }}" class="{{ macros.isActiveChildClass(child.slug) | trim }} level{{ child.level }}">{{ child.entryLink.first.title }}</a></li>
                {% endif %}
                {% endfor %}
            </ul>
        </li>
    {% elseif entry.level == 1 and not entry.hasDescendants() %}
     <li><a href="{{ siteUrl }}{{ navlink }}" class="{{ macros.isParentMenuActive(entry.slug) |trim }} level{{ entry.level }}">{{ entry.title }}</a></li>
    {% endif %}


{% endnav %}

This has managed to produce a bootstrap drop down menu from the nav tag on a structure section with two fields: customMenuUrl (plain text) and entryLink (Entries field)

|improve this answer|||||

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.