I am trying to check if a given FieldItemListInterface is empty or not (and if not, grab the entity)

Using the example in https://www.drupal.org/project/examples - the team I'm on was able to get an access function written that should theoretically work only if there are items with an entity in them.

However whenever we have the correct field definition name, and check for items, they're all empty:

if ($items) {
// Code does not execute here - $items value is always NULL.

I'm not quite sure why but for some reason the value is always NULL, even if I would think it should be full.

Why exactly would this value be NULL if the field definition exists on a given entity, and has a value?

What am I missing?

Further code example:

function MY_MODULE_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {

if ($field_definition->getName() != 'field_attendee_only_content') {
    return AccessResult::neutral();

// Code gets to here.

  if ($items) {
// Code here is never executed. $items is ALWAYS NULL.
    $entity = $items->getEntity();

For additional information, the field in question is a file field.


In the documentation for EntityAccessControlHandler::fieldAccess(), the method that invokes the hook you are implementing, $items is defined as follows.

\Drupal\Core\Field\FieldItemListInterface $items: (optional) The field values for which to check access, or NULL if access is checked for the field definition, without any specific value available. Defaults to NULL.

This could also happen if the entity containing the field is being created, and not modified. That is the reason why Drupal core implementations of that hook always check the value of $items before using it.

  // language_entity_field_access()
  // Only allow edit access on a langcode field if the entity it is attached to
  // is configured to have an alterable language. Also without items we can not
  // decide whether or not to allow access.
  if ($items && $operation == 'edit') {
    // Omissis.
  // views_entity_test_entity_field_access()
  if ($field_definition->getName() == 'test_text_access') {
    if ($items) {
      if ($items->value == 'no access value') {
        return AccessResult::forbidden()->addCacheableDependency($items->getEntity());
  • The entity in question is being viewed, not modified. Could it be because it is a file field?
    – Jack Ryan
    Apr 26 '17 at 19:26
  • There could be other cases where Drupal passes the default value for $items. I am not sure the field type makes any difference, though.
    – apaderno
    Apr 26 '17 at 20:33
  • 1
    One example for view without items is views. Views will call the hook without items to check whether to show e.g. a table column for that field or not.
    – Berdir
    Apr 26 '17 at 22:10
  • Ah, my fields are in a view that is being attached to a node using display suite. Is that possibly the source behind my problem?
    – Jack Ryan
    Apr 26 '17 at 23:08
  • @AndrewAlexander Since you are using a view, yes. The Views module loads entities directly from the database; so, it cannot pass any value for $items.
    – apaderno
    Apr 27 '17 at 7:08

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.