/**
  * Act on entities being assembled before rendering.
  *
  * This is a hook bridge.
  *
  * @see hook_entity_view()
  * @see EntityFieldManagerInterface::getExtraFields()
  */
 public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode)
 {
     if (!$this->moderationInfo->isModeratableEntity($entity)) {
         return;
     }
     if (!$this->moderationInfo->isLatestRevision($entity)) {
         return;
     }
     /** @var ContentEntityInterface $entity */
     if ($entity->isDefaultRevision()) {
         return;
     }
     $component = $display->getComponent('workbench_moderation_control');
     if ($component) {
         $build['workbench_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity);
         $build['workbench_moderation_control']['#weight'] = $component['weight'];
     }
 }
 /**
  * {@inheritdoc}
  */
 public function convert($value, $definition, $name, array $defaults)
 {
     $entity = parent::convert($value, $definition, $name, $defaults);
     if ($entity && $this->moderationInformation->isModeratableEntity($entity) && !$this->moderationInformation->isLatestRevision($entity)) {
         $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
         $entity = $this->moderationInformation->getLatestRevision($entity_type_id, $value);
         // If the entity type is translatable, ensure we return the proper
         // translation object for the current context.
         if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
             $entity = $this->entityManager->getTranslationFromContext($entity, NULL, array('operation' => 'entity_upcast'));
         }
     }
     return $entity;
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     /** @var \Drupal\Core\Entity\EntityInterface $entity */
     $entity = $value->getEntity();
     // Ignore entities that are not subject to moderation anyway.
     if (!$this->moderationInformation->isModeratableEntity($entity)) {
         return;
     }
     // Ignore entities that are being created for the first time.
     if ($entity->isNew()) {
         return;
     }
     // Ignore entities that are being moderated for the first time, such as
     // when they existed before moderation was enabled for this entity type.
     if ($this->isFirstTimeModeration($entity)) {
         return;
     }
     $original_entity = $this->moderationInformation->getLatestRevision($entity->getEntityTypeId(), $entity->id());
     $next_moderation_state_id = $entity->moderation_state->target_id;
     $original_moderation_state_id = $original_entity->moderation_state->target_id;
     if (!$this->validation->isTransitionAllowed($original_moderation_state_id, $next_moderation_state_id)) {
         $this->context->addViolation($constraint->message, ['%from' => $original_entity->moderation_state->entity->label(), '%to' => $entity->moderation_state->entity->label()]);
     }
 }