Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function getTitle()
 {
     if (!$this->moderationInfo->isModeratableEntity($this->entity)) {
         // Moderation isn't enabled.
         return parent::getTitle();
     }
     // @todo write a test for this.
     return $this->moderationInfo->isLiveRevision($this->entity) ? $this->t('New draft') : $this->t('Edit draft');
 }
Пример #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 formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $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];
     }
     $options = $this->fieldDefinition->getFieldStorageDefinition()->getOptionsProvider($this->column, $entity)->getSettableOptions($this->currentUser);
     $default = $items->get($delta)->target_id ?: $bundle_entity->getThirdPartySetting('workbench_moderation', 'default_moderation_state', FALSE);
     /** @var \Drupal\workbench_moderation\ModerationStateInterface $default_state */
     $default_state = ModerationState::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()));
     }
     // @todo write a test for this.
     $from = $this->moderationStateTransitionEntityQuery->condition('stateFrom', $default)->execute();
     // Can always keep this one as is.
     $to[$default] = $default;
     // @todo write a test for this.
     $allowed = $bundle_entity->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', []);
     if ($from) {
         /* @var \Drupal\workbench_moderation\ModerationStateTransitionInterface $transition */
         foreach ($this->moderationStateTransitionStorage->loadMultiple($from) as $id => $transition) {
             $to_state = $transition->getToState();
             if ($this->currentUser->hasPermission('use ' . $id . ' transition') && in_array($to_state, $allowed, TRUE)) {
                 $to[$to_state] = $to_state;
             }
         }
     }
     $options = array_intersect_key($options, $to);
     // @todo write a test for this.
     $element += ['#access' => count($options), '#type' => 'select', '#options' => $options, '#default_value' => $default, '#published' => $default ? $default_state->isPublishedState() : FALSE];
     if ($this->currentUser->hasPermission($this->getAdminPermission($entity->getEntityType())) && count($options)) {
         // Use the dropbutton.
         $element['#process'][] = [get_called_class(), 'processActions'];
         // Don't show in sidebar/body.
         $element['#access'] = FALSE;
     } else {
         // Place the field as a details element in the advanced tab-set in e.g.
         // the sidebar.
         $element = ['#type' => 'details', '#group' => 'advanced', '#open' => TRUE, '#weight' => -10, '#title' => t('Moderation state'), 'target_id' => $element];
     }
     return $element;
 }
 public function testIsModeratedEntityFormWithNonContentEntityForm()
 {
     $form = $this->prophesize(EntityFormInterface::class);
     $moderation_information = new ModerationInformation($this->setupModerationEntityManager(TRUE), $this->getUser());
     $this->assertFalse($moderation_information->isModeratedEntityForm($form->reveal()));
 }