1

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.

1 Answer 1

3

FWIW :

The entity reference field formatters that are in core use the method getEntitiesToView() (From class EntityReferenceFormatterBase) to load the referenced entities.

This method honors entity access :

$access = $this->checkAccess($entity);
// ...
if ($access->isAllowed()) {
  // ...
     $entities[$delta] = $entity;
    }
//...
return $entities;

so yes, it seems normal that nothing is rendered.

1
  • Ah, yes, that makes sense, thanks for the hint. I'm guessing, the function checkAccess() checks the access for every single entity that is referenced and returns FALSE, when all of them are locked? Do you happen to know, if (and how) this can be overridden (for this specific field)?
    – Marcus C.
    Commented Nov 18, 2018 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.