function content_moderation_entity_field_access

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/content_moderation.module \content_moderation_entity_field_access()
  2. 8.9.x core/modules/content_moderation/content_moderation.module \content_moderation_entity_field_access()
  3. 10 core/modules/content_moderation/content_moderation.module \content_moderation_entity_field_access()

Implements hook_entity_field_access().

File

core/modules/content_moderation/content_moderation.module, line 257

Code

function content_moderation_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, ?FieldItemListInterface $items = NULL) {
    if ($items && $operation === 'edit') {
        
        /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
        $moderation_info = Drupal::service('content_moderation.moderation_information');
        $entity_type = \Drupal::entityTypeManager()->getDefinition($field_definition->getTargetEntityTypeId());
        $entity = $items->getEntity();
        // Deny edit access to the published field if the entity is being moderated.
        if ($entity_type->hasKey('published') && $moderation_info->isModeratedEntity($entity) && $entity->moderation_state && $field_definition->getName() == $entity_type->getKey('published')) {
            return AccessResult::forbidden('Cannot edit the published field of moderated entities.');
        }
    }
    return AccessResult::neutral();
}

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