/**
* 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\ContentEntityInterface $entity
* The entity being moderated.
*
* @return bool
* TRUE if this is the entity's first time being moderated, FALSE otherwise.
*/
// If a new revision of the content has been created, add a new content
// moderation state revision.
if(!$content_moderation_state->isNew() && $content_moderation_state->content_entity_revision_id->value != $entity_revision_id){ $content_moderation_state = $storage->createRevision($content_moderation_state, $entity->isDefaultRevision()); }
// Create the ContentModerationState entity for the inserted entity.
$moderation_state = $entity->moderation_state->value; /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ if(!$moderation_state){ $moderation_state = $workflow->getTypePlugin()->getInitialState($entity)->id(); }
/**
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being deleted.
*
* @see hook_entity_delete()
*/
// Existing entities will have a corresponding content_moderation_state
// entity associated with them.
if(!$entity->isNew() && $content_moderation_state = $this->loadContentModerationStateRevision($entity)){ return$content_moderation_state->moderation_state->value; }
// It is possible that the bundle does not exist at this point. For example,
// the node type form creates a fake Node entity to get default values.
// @see \Drupal\node\NodeTypeForm::form()
$workflow = $moderation_info->getWorkFlowForEntity($entity); return$workflow ? $workflow->getTypePlugin()->getInitialState($entity)->id() : NULL; }
/**
* Load the content moderation state revision associated with an entity.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity the content moderation state entity will be loaded from.
*
* @return \Drupal\content_moderation\Entity\ContentModerationStateInterface|null
* The content_moderation_state revision or FALSE if none exists.
*/
// Try creating a duplicate state.
$this->drupalGet('admin/config/workflow/workflows/manage/test'); $this->clickLink('Add a new state'); $this->submitForm(['label' => 'Draft', 'id' => 'draft'], 'Save'); $this->assertSession()->pageTextContains('The machine-readable name is already in use. It must be unique.');
// Ensure that weight changes the state ordering.
$workflow = $workflow_storage->loadUnchanged('test'); $this->assertEquals('published', $workflow->getTypePlugin()->getInitialState()->id()); $this->drupalGet('admin/config/workflow/workflows/manage/test'); $this->submitForm(['states[draft][weight]' => '-1'], 'Save'); $workflow = $workflow_storage->loadUnchanged('test'); $this->assertEquals('draft', $workflow->getTypePlugin()->getInitialState()->id());
// Verify that we are still on the workflow edit page.
$this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test');