I have been trying to find the reason for this problem for a few days now... without any luck. Which is why I would like to ask for help here...
In a nutshell: I'm working on a simple LMS and have defined a content type Course
that contains an entity reference field field_lessons
, which refers to another content type Lesson
. In the template file node--course--full.html.twig
I'm using the following code to render a "table of contents" in the sidebar:
...
<aside>
{{ content.field_lessons }}
</aside>
...
In addition I'm using a template file field--node--field-lessons--course.html.twig
for rendering the field:
...
<nav class="list-group">
{% for item in items %}
{% if (item.viewaccess) %}
<a class="list-group-item list-group-item-action" href="...">
{% else %}
<span class="list-group-item list-group-item-action disabled">
{% endif %}
<div class="list-group-item-heading">
{{ item.title }}
</div>
...
{% if (item.viewaccess) %}
</a>
{% else %}
</span>
{% endif %}
{% endfor %}
</nav>
...
(Note that I deliberately left out a few insignificant details in the code: These are marked with "...".)
So, depending on whether the current user can access a single lesson (I'm using the variable item.viewaccess
, which depends on hook_node_access()
and is set in hook_preprocess_node()
), each lesson is rendered as active or disabled list-group-item
. So far, this works perfectly.
Something weird happens, however, as soon as all lessons are locked for the current user: In this case, none of the lessons gets rendered and the whole table of contents (i.e. the whole <nav>
block) disappears.
I'm totally stumped at this point. Can anyone offer an idea as to why this happens? Any help would be much appreciated.