You are here

function language_entity_field_access in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/language/language.module \language_entity_field_access()

Implements hook_entity_field_access().

File

core/modules/language/language.module, line 379
Add language handling functionality to Drupal.

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();
}