I have the following content types (for a simple LMS) defined:
Lessons
, for which there are rather complex rules for handling view access.Courses
, which contain entity references to one or moreLessons
.
Now, in the Twig template for rendering a Course
node, I am trying to create a "table of contents" (a list of Lessons
), using the following snippet:
<nav id='toc_lessons' class="list-group">
{% for key, lesson in content.field_lessons %}
{% if lesson['#node'].title.value|render|striptags|trim %}
<a class="list-group-item list-group-item-action
href="{{ url('<current>') }}/?lesson={{ lesson['#node'].nid.value }}">
{{ lesson['#node'].title.value }}
{{ (*****) ? '<span class="label">LOCKED</span>' }}
</a>
{% endif %}
{% endfor %}
</nav>
This works well. However, the problem with this code is the line that I marked with (*****): I would like to do a simple access check, i.e. I would like to check, if the user is allowed to view the Lesson
. If not, the label "LOCKED" should be added.
In my implementation of function THEME_preprocess_node
I have already defined such a check that is called whenever a Lesson
is rendered:
function THEME_preprocess_node(&$variables) {
...
$node = $variables['elements']['#node'];
$variables['viewaccess'] = $node->access('view');
...
}
But can I call the preprocessing function for a Lesson
from within the Course
template to receive the variable viewaccess
?
Another idea would be to modify the above snippet as follows:
<nav id='toc_lessons' class="list-group">
{% for key, lesson in content.field_lessons %}
{% if lesson['#node'].title.value|render|striptags|trim %}
{{ bamboo_render_entity('node', lesson['#node'].nid.value, 'toc_entry') }}
{% endif %}
{% endfor %}
</nav>
so, I am now rendering the Lesson
using Bamboo Twig and the output <a>...</a>
is fully handled by the Lesson
template, which works fine. The problem now is that a Lesson that the user does not have view access for, is not rendered at all.
Now, I'm stuck. Can anybody offer any ideas?
Any help would be much appreciated.