function LanguageHooks::entityFieldAccess

Implements hook_entity_field_access().

File

core/modules/language/src/Hook/LanguageHooks.php, line 302

Class

LanguageHooks
Hook implementations for language.

Namespace

Drupal\language\Hook

Code

public function entityFieldAccess($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.