hook_entity_field_access - trying to check if there's a value 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-&gt;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-&gt;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 &amp;&amp; $operation == 'edit') {
    // Omissis.
  }




  // views_entity_test_entity_field_access()
  if ($field_definition-&gt;getName() == 'test_text_access') {
    if ($items) {
      if ($items-&gt;value == 'no access value') {
        return AccessResult::forbidden()-&gt;addCacheableDependency($items-&gt;getEntity());
      }
    }
  }