function EntityOperations::entityView

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityView()
  2. 10 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityView()
  3. 11.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityView()

Act on entities being assembled before rendering.

See also

hook_entity_view()

EntityFieldManagerInterface::getExtraFields()

File

core/modules/content_moderation/src/EntityOperations.php, line 274

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\content_moderation

Code

public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    if (!$this->moderationInfo
        ->isModeratedEntity($entity)) {
        return;
    }
    if (isset($entity->in_preview) && $entity->in_preview) {
        return;
    }
    // If the component is not defined for this display, we have nothing to do.
    if (!$display->getComponent('content_moderation_control')) {
        return;
    }
    // The moderation form should be displayed only when viewing the latest
    // (translation-affecting) revision, unless it was created as published
    // default revision.
    if (($entity->isDefaultRevision() || $entity->wasDefaultRevision()) && $this->isPublished($entity)) {
        return;
    }
    if (!$entity->isLatestRevision() && !$entity->isLatestTranslationAffectedRevision()) {
        return;
    }
    $build['content_moderation_control'] = $this->formBuilder
        ->getForm(EntityModerationForm::class, $entity);
}

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