Call preprocess_node (or get variables) from Twig template? 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 more Lessons.


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:

&lt;nav id='toc_lessons' class="list-group"&gt;
  {% for key, lesson in content.field_lessons %}
    {% if lesson['#node'].title.value|render|striptags|trim %}

        &lt;a class="list-group-item list-group-item-action
           href="{{ url('&lt;current&gt;') }}/?lesson={{ lesson['#node'].nid.value }}"&gt;
          {{ lesson['#node'].title.value }}
          {{ (*****) ? '&lt;span class="label"&gt;LOCKED&lt;/span&gt;' }}
        &lt;/a&gt;

    {% endif %}
  {% endfor %}
&lt;/nav&gt;


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(&amp;$variables) {
  ...
  $node = $variables['elements']['#node'];
  $variables['viewaccess'] = $node-&gt;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:

&lt;nav id='toc_lessons' class="list-group"&gt;
  {% 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 %}
&lt;/nav&gt;


so, I am now rendering the Lesson using Bamboo Twig and the output &lt;a&gt;...&lt;/a&gt; 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.
You're pretty close. I think there are a couple of approaches to this. This is what I would do

preprocess the lessons entity reference field for the courses bundle, iterate through the values, check the access, set a variable with the access value for each item. Then in field--field-lessons.twig.html, render the field if the access value is true.

&lt;?php
//the name of your lessons entity reference field on courses
function THEME_preprocess_field__field_lessons(&amp;$variables) {

  if($variables['element']['#bundle'] == 'courses') {
    $account = \Drupal::currentUser();
    $ent_refs = $variables['element']['#items']-&gt;referencedEntities();

    foreach ($ent_refs as $key =&gt; $ent_ref) {
      $check = $ent_ref-&gt;access('view', $account);
      $variables['items'][$key]['my_access'] = FALSE;

      if ($check) {
        $variables['items'][$key]['my_access'] = TRUE;
      }
    }

  }
}


then in field--field-lessons.twig.html

...
{% for item in items %}
    {% if item.my_access == TRUE %}
      I'm Unlocked: {{ item.content }}
    {% else %}
      I'm locked :(
    {% endif %}
  {% endfor %}
...