示例#1
0
 /**
  * @inheritDoc
  */
 public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL)
 {
     /** @var ModerationState $current_state */
     $current_state = $entity->moderation_state->entity;
     $transitions = $this->validation->getValidTransitions($entity, $this->currentUser());
     // Exclude self-transitions.
     $transitions = array_filter($transitions, function (ModerationStateTransition $transition) use($current_state) {
         return $transition->getToState() != $current_state->id();
     });
     $target_states = [];
     /** @var ModerationStateTransition $transition */
     foreach ($transitions as $transition) {
         $target_states[$transition->getToState()] = $transition->label();
     }
     if ($current_state) {
         $form['current'] = ['#type' => 'item', '#title' => $this->t('Status'), '#markup' => $current_state->label()];
     }
     // Persist the entity so we can access it in the submit handler.
     $form_state->set('entity', $entity);
     $form['new_state'] = ['#type' => 'select', '#title' => $this->t('Moderate'), '#options' => $target_states];
     $form['revision_log'] = ['#type' => 'textfield', '#title' => $this->t('Log message'), '#size' => 30];
     $form['submit'] = ['#type' => 'submit', '#value' => $this->t('Apply')];
     $form['#theme'] = ['entity_moderation_form'];
     return $form;
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     /** @var ContentEntityInterface $entity */
     $entity = $items->getEntity();
     /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */
     $bundle_entity = $this->entityTypeManager->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle());
     if (!$this->moderationInformation->isModeratableEntity($entity)) {
         // @todo write a test for this.
         return $element + ['#access' => FALSE];
     }
     $default = $items->get($delta)->target_id ?: $bundle_entity->getThirdPartySetting('workbench_moderation', 'default_moderation_state', FALSE);
     /** @var \Drupal\workbench_moderation\ModerationStateInterface $default_state */
     $default_state = $this->entityTypeManager->getStorage('moderation_state')->load($default);
     if (!$default || !$default_state) {
         throw new \UnexpectedValueException(sprintf('The %s bundle has an invalid moderation state configuration, moderation states are enabled but no default is set.', $bundle_entity->label()));
     }
     $transitions = $this->validator->getValidTransitions($entity, $this->currentUser);
     $target_states = [];
     /** @var ModerationStateTransition $transition */
     foreach ($transitions as $transition) {
         $target_states[$transition->getToState()] = $transition->label();
     }
     // @todo write a test for this.
     $element += ['#access' => FALSE, '#type' => 'select', '#options' => $target_states, '#default_value' => $default, '#published' => $default ? $default_state->isPublishedState() : FALSE];
     // Use the dropbutton.
     $element['#process'][] = [get_called_class(), 'processActions'];
     return $element;
 }
 /**
  * @inheritDoc
  */
 public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL)
 {
     $target_states = $this->validation->getValidTransitionTargets($entity, $this->currentUser());
     $target_states = array_map(function (ModerationState $state) {
         return $state->label();
     }, $target_states);
     /** @var ModerationState $current_state */
     $current_state = $entity->moderation_state->entity;
     if ($current_state) {
         $form['current'] = ['#type' => 'markup', '#markup' => $this->t('Current status: %state', ['%state' => $current_state->label()])];
     }
     // Persist the entity so we can access it in the submit handler.
     $form_state->set('entity', $entity);
     $form['new_state'] = ['#type' => 'select', '#title' => $this->t('New state'), '#options' => $target_states];
     $form['submit'] = ['#type' => 'submit', '#value' => $this->t('Update')];
     return $form;
 }
 /**
  * {@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()]);
     }
 }
 /**
  * @covers ::isTransitionAllowed
  * @covers ::calculatePossibleTransitions
  */
 public function testIsTransitionAllowedWithInValidTransition()
 {
     $state_transition_validation = new StateTransitionValidation($this->setupEntityTypeManager(), $this->setupQueryFactory());
     $this->assertFalse($state_transition_validation->isTransitionAllowed('published', 'needs_review'));
     $this->assertFalse($state_transition_validation->isTransitionAllowed('published', 'staging'));
     $this->assertFalse($state_transition_validation->isTransitionAllowed('staging', 'needs_review'));
     $this->assertFalse($state_transition_validation->isTransitionAllowed('staging', 'staging'));
     $this->assertFalse($state_transition_validation->isTransitionAllowed('needs_review', 'published'));
 }