language_entity_field_access

language_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL)

Implements hook_entity_field_access()

File

core/modules/language/language.module, line 431
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();
}
doc_Drupal
2016-10-29 09:23:01
Comments
Leave a Comment

Please login to continue.