/** * {@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->isModeratedEntity($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()); if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) { $original_entity = $original_entity->getTranslation($entity->language()->getId()); } if ($entity->moderation_state->target_id) { $new_state_id = $entity->moderation_state->target_id; } else { $new_state_id = $default = $this->moderationInformation->loadBundleEntity($entity->getEntityType()->getBundleEntityType(), $entity->bundle())->getThirdPartySetting('content_moderation', 'default_moderation_state'); } if ($new_state_id) { $new_state = ModerationStateEntity::load($new_state_id); } // @todo - what if $new_state_id references something that does not exist or // is null. if (!$this->validation->isTransitionAllowed($original_entity->moderation_state->entity, $new_state)) { $this->context->addViolation($constraint->message, ['%from' => $original_entity->moderation_state->entity->label(), '%to' => $new_state->label()]); } }
/** * {@inheritdoc} */ public function convert($value, $definition, $name, array $defaults) { $entity = parent::convert($value, $definition, $name, $defaults); if ($entity && $this->moderationInformation->isModeratedEntity($entity) && !$this->moderationInformation->isLatestRevision($entity)) { $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults); $latest_revision = $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 ($latest_revision instanceof EntityInterface && $entity instanceof TranslatableInterface) { $latest_revision = $this->entityManager->getTranslationFromContext($latest_revision, NULL, array('operation' => 'entity_upcast')); } if ($latest_revision->isRevisionTranslationAffected()) { $entity = $latest_revision; } } return $entity; }
/** * Alters bundle forms to enforce revision handling. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param string $form_id * The form id. * * @see hook_form_alter() */ public function formAlter(array &$form, FormStateInterface $form_state, $form_id) { $form_object = $form_state->getFormObject(); if ($form_object instanceof BundleEntityFormBase) { $type = $form_object->getEntity()->getEntityType(); if ($this->moderationInfo->canModerateEntitiesOfEntityType($type)) { $this->entityTypeManager->getHandler($type->getBundleOf(), 'moderation')->enforceRevisionsBundleFormAlter($form, $form_state, $form_id); } } elseif ($form_object instanceof ContentEntityFormInterface) { $entity = $form_object->getEntity(); if ($this->moderationInfo->isModeratedEntity($entity)) { $this->entityTypeManager->getHandler($entity->getEntityTypeId(), 'moderation')->enforceRevisionsEntityFormAlter($form, $form_state, $form_id); // Submit handler to redirect to the latest version, if available. $form['actions']['submit']['#submit'][] = [EntityTypeInfo::class, 'bundleFormRedirect']; } } }
/** * 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->isModeratedEntity($entity)) { return; } if (!$this->moderationInfo->isLatestRevision($entity)) { return; } /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ if ($entity->isDefaultRevision()) { return; } $component = $display->getComponent('content_moderation_control'); if ($component) { $build['content_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity); $build['content_moderation_control']['#weight'] = $component['weight']; } }
/** * {@inheritdoc} */ public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { $result = parent::access($object, $account, TRUE)->andif(AccessResult::forbiddenIf($this->moderationInfo->isModeratedEntity($object))->addCacheableDependency($object)); return $return_as_object ? $result : $result->isAllowed(); }