/** * {@inheritdoc} */ public function getTitle() { if (!$this->moderationInfo->isModeratedEntity($this->entity)) { // Moderation isn't enabled. return parent::getTitle(); } // @todo https://www.drupal.org/node/2779933 write a test for this. return $this->moderationInfo->isLiveRevision($this->entity) ? $this->t('New draft') : $this->t('Edit draft'); }
/** * {@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->isModeratedEntity($entity)) { // @todo https://www.drupal.org/node/2779933 write a test for this. return $element + ['#access' => FALSE]; } $default = $items->get($delta)->value ?: $bundle_entity->getThirdPartySetting('content_moderation', 'default_moderation_state', FALSE); /** @var \Drupal\content_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 \Drupal\content_moderation\Entity\ModerationStateTransition $transition */ foreach ($transitions as $transition) { $target_states[$transition->getToState()] = $transition->label(); } // @todo https://www.drupal.org/node/2779933 write a test for this. $element += ['#access' => FALSE, '#type' => 'select', '#options' => $target_states, '#default_value' => $default, '#published' => $default ? $default_state->isPublishedState() : FALSE, '#key_column' => $this->column]; $element['#element_validate'][] = array(get_class($this), 'validateElement'); // Use the dropbutton. $element['#process'][] = [get_called_class(), 'processActions']; return $element; }
/** * @covers ::isModeratedEntity */ public function testIsModeratedEntityForNonBundleEntityType() { $entity_type = new ContentEntityType(['id' => 'test_entity_type']); $entity = $this->prophesize(ContentEntityInterface::class); $entity->getEntityType()->willReturn($entity_type); $entity->bundle()->willReturn('test_entity_type'); $entity_storage = $this->prophesize(EntityStorageInterface::class); $entity_type_manager = $this->getEntityTypeManager($entity_storage->reveal()); $moderation_information = new ModerationInformation($entity_type_manager, $this->getUser()); $this->assertEquals(FALSE, $moderation_information->isModeratedEntity($entity->reveal())); }