/** * Checks that there is a forward revision available. * * This checker assumes the presence of an '_entity_access' requirement key * in the same form as used by EntityAccessCheck. * * @see EntityAccessCheck. * * @param \Symfony\Component\Routing\Route $route * The route to check against. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The parametrized route * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ public function access(Route $route, RouteMatchInterface $route_match) { // This tab should not show up period unless there's a reason to show it. // @todo Do we need any extra cache tags here? $entity = $this->loadEntity($route, $route_match); return $this->moderationInfo->hasForwardRevision($entity) ? AccessResult::allowed()->addCacheableDependency($entity) : AccessResult::forbidden()->addCacheableDependency($entity); }
/** * Implements hook_entity_view_alter(). */ public function entityViewAlter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) { if ($entity->getEntityType()->isRevisionable() && !$this->moderationInfo->isLatestRevision($entity)) { // Hide quickedit, because its super confusing for the user to not edit the // live revision. unset($build['#attributes']['data-quickedit-entity-id']); } }
/** * 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']; } }
/** * Redirect content entity edit forms on save, if there is a forward revision. * * When saving their changes, editors should see those changes displayed on * the next page. * * @param array $form * @param \Drupal\Core\Form\FormStateInterface $form_state */ public function bundleFormRedirect(array &$form, FormStateInterface $form_state) { /* @var ContentEntityInterface $entity */ $entity = $form_state->getFormObject()->getEntity(); if ($this->moderationInfo->hasForwardRevision($entity)) { $entity_type_id = $entity->getEntityTypeId(); $form_state->setRedirect("entity.{$entity_type_id}.latest_version", [$entity_type_id => $entity->id()]); } }
/** * {@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; }
/** * 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 bundleFormAlter(array &$form, FormStateInterface $form_state, $form_id) { if ($this->moderationInfo->isRevisionableBundleForm($form_state->getFormObject())) { /* @var ConfigEntityTypeInterface $bundle */ $bundle = $form_state->getFormObject()->getEntity(); $this->entityTypeManager->getHandler($bundle->getEntityType()->getBundleOf(), 'moderation')->enforceRevisionsBundleFormAlter($form, $form_state, $form_id); } else { if ($this->moderationInfo->isModeratedEntityForm($form_state->getFormObject())) { /* @var ContentEntityInterface $entity */ $entity = $form_state->getFormObject()->getEntity(); $this->entityTypeManager->getHandler($entity->getEntityTypeId(), 'moderation')->enforceRevisionsEntityFormAlter($form, $form_state, $form_id); // Submit handler to redirect to the $form['actions']['submit']['#submit'][] = '\\Drupal\\workbench_moderation\\EntityTypeInfo::bundleFormRedirect'; } } }
/** * Determines if this entity is being moderated for the first time. * * If the previous version of the entity has no moderation state, we assume * that means it predates the presence of moderation states. * * @param \Drupal\Core\Entity\EntityInterface $entity * * @return bool * TRUE if this is the entity's first time being moderated, FALSE otherwise. */ protected function isFirstTimeModeration(EntityInterface $entity) { $original_entity = $this->moderationInformation->getLatestRevision($entity->getEntityTypeId(), $entity->id()); $original_id = $original_entity->moderation_state->target_id; return !($original_entity && $original_id); }