function config_entity_revisions_entity_access in Config Entity Revisions 8.2
Implements hook_entity_access().
Overrides content_moderation_entity_access, providing a version that handles configuration entities with revisions as well.
Entities should be viewable if unpublished and the user has the appropriate permission. This permission is therefore effectively mandatory for any user that wants to moderate things.
File
- ./
config_entity_revisions.module, line 25 - Config entity revisions procedural hooks.
Code
function config_entity_revisions_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
$moderation_info = Drupal::service('content_moderation.moderation_information');
$access_result = NULL;
if ($operation === 'view') {
$access_result = $entity instanceof EntityPublishedInterface && !$entity
->isPublished() ? AccessResult::allowedIfHasPermission($account, 'view any unpublished content') : AccessResult::neutral();
$access_result
->addCacheableDependency($entity);
}
elseif ($operation === 'update' && $moderation_info
->isModeratedEntity($entity) && $entity->moderation_state) {
/** @var \Drupal\content_moderation\StateTransitionValidation $transition_validation */
$transition_validation = \Drupal::service('content_moderation.state_transition_validation');
if ($entity instanceof ConfigEntityRevisionsConfigEntityInterface) {
$entity = $entity
->getContentEntity();
}
$valid_transition_targets = $transition_validation
->getValidTransitions($entity, $account);
$access_result = $valid_transition_targets ? AccessResult::neutral() : AccessResult::forbidden('No valid transitions exist for given account.');
$access_result
->addCacheableDependency($entity);
$access_result
->addCacheableDependency($account);
$workflow = $moderation_info
->getWorkflowForEntity($entity);
$access_result
->addCacheableDependency($workflow);
foreach ($valid_transition_targets as $valid_transition_target) {
$access_result
->addCacheableDependency($valid_transition_target);
}
}
return $access_result;
}