function language_entity_field_access

Same name and namespace in other branches
  1. 9 core/modules/language/language.module \language_entity_field_access()
  2. 8.9.x core/modules/language/language.module \language_entity_field_access()

Implements hook_entity_field_access().

File

core/modules/language/language.module, line 400

Code

function language_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, ?FieldItemListInterface $items = NULL) {
  // 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') {
    // Check if we are dealing with a langcode field.
    $langcode_key = $items->getEntity()
      ->getEntityType()
      ->getKey('langcode');
    if ($field_definition->getName() == $langcode_key) {
      // Grant access depending on whether the entity language can be altered.
      $entity = $items->getEntity();
      $config = ContentLanguageSettings::loadByEntityTypeBundle($entity->getEntityTypeId(), $entity->bundle());
      return AccessResult::forbiddenIf(!$config->isLanguageAlterable());
    }
  }
  return AccessResult::neutral();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.