diff --git a/core/modules/content_moderation/content_moderation.api.php b/core/modules/content_moderation/content_moderation.api.php new file mode 100644 index 0000000..a56813e --- /dev/null +++ b/core/modules/content_moderation/content_moderation.api.php @@ -0,0 +1,23 @@ +getInitialState($entity); + * @endcode + * This is used to determine the initial moderation state based on the + * publishing status of the entity. + * @} + */ diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php index 22e901b..d276ad7 100644 --- a/core/modules/content_moderation/src/EntityOperations.php +++ b/core/modules/content_moderation/src/EntityOperations.php @@ -191,7 +191,7 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) { $moderation_state = $entity->moderation_state->value; /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ if (!$moderation_state) { - $moderation_state = $workflow->getTypePlugin()->getInitialState($workflow, $entity)->id(); + $moderation_state = $workflow->getTypePlugin()->getInitialState($entity)->id(); } // @todo what if $entity->moderation_state is null at this point? diff --git a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php index 9a35f90..bfdc815 100644 --- a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php +++ b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php @@ -116,7 +116,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen } $workflow = $this->moderationInformation->getWorkflowForEntity($entity); - $default = $items->get($delta)->value ? $workflow->getTypePlugin()->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity); + $default = $items->get($delta)->value ? $workflow->getTypePlugin()->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($entity); /** @var \Drupal\workflows\Transition[] $transitions */ $transitions = $this->validator->getValidTransitions($entity, $this->currentUser); diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 99e5f55..de62224 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -39,7 +39,7 @@ protected function getModerationStateId() { // 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($workflow, $entity)->id() : NULL; + return $workflow ? $workflow->getTypePlugin()->getInitialState($entity)->id() : NULL; } /** diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index aac75b9..ceaa565 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -4,6 +4,7 @@ use Drupal\content_moderation\ModerationInformationInterface; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityPublishedInterface; @@ -340,11 +341,19 @@ public function getConfiguration() { /** * {@inheritdoc} */ - public function getInitialState(WorkflowInterface $workflow, $entity = NULL) { + public function getInitialState($entity = NULL) { + // Workflows are not tied to entities, but Content Moderation adds the + // relationship between Workflows and entities. Content Moderation needs the + // entity object to be able to determine the initial state based on + // publishing status. + if (!($entity instanceof ContentEntityInterface)) { + throw new \InvalidArgumentException('A content entity object must be supplied.'); + } if ($entity instanceof EntityPublishedInterface) { - return $workflow->getTypePlugin()->getState($entity->isPublished() && !$entity->isNew() ? 'published' : 'draft'); + return $this->getState($entity->isPublished() && !$entity->isNew() ? 'published' : 'draft'); } - return parent::getInitialState($workflow); + // Workflows determines the initial state for non-publishable entities. + return parent::getInitialState(); } } diff --git a/core/modules/content_moderation/src/StateTransitionValidation.php b/core/modules/content_moderation/src/StateTransitionValidation.php index aca956f..fc09e5e 100644 --- a/core/modules/content_moderation/src/StateTransitionValidation.php +++ b/core/modules/content_moderation/src/StateTransitionValidation.php @@ -40,7 +40,7 @@ public function __construct(ModerationInformationInterface $moderation_info) { */ public function getValidTransitions(ContentEntityInterface $entity, AccountInterface $user) { $workflow = $this->moderationInfo->getWorkflowForEntity($entity); - $current_state = $entity->moderation_state->value ? $workflow->getTypePlugin()->getState($entity->moderation_state->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity); + $current_state = $entity->moderation_state->value ? $workflow->getTypePlugin()->getState($entity->moderation_state->value) : $workflow->getTypePlugin()->getInitialState($entity); return array_filter($current_state->getTransitions(), function(Transition $transition) use ($workflow, $user) { return $user->hasPermission('use ' . $workflow->id() . ' transition ' . $transition->id()); diff --git a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php index b7e18d9..23481a4 100644 --- a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php +++ b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php @@ -117,7 +117,7 @@ public function onDependencyRemoval(array $dependencies) { /** * {@inheritdoc} */ - public function getInitialState(WorkflowInterface $workflow) { + public function getInitialState() { $ordered_states = $this->getStates(); return reset($ordered_states); } diff --git a/core/modules/workflows/src/WorkflowTypeInterface.php b/core/modules/workflows/src/WorkflowTypeInterface.php index 9fd1a9a..032422e 100644 --- a/core/modules/workflows/src/WorkflowTypeInterface.php +++ b/core/modules/workflows/src/WorkflowTypeInterface.php @@ -80,13 +80,10 @@ public function workflowStateHasData(WorkflowInterface $workflow, StateInterface /** * Gets the initial state for the workflow. * - * @param \Drupal\workflows\WorkflowInterface $workflow - * The workflow entity. - * * @return \Drupal\workflows\StateInterface * The initial state. */ - public function getInitialState(WorkflowInterface $workflow); + public function getInitialState(); /** * Gets the required states of workflow type. diff --git a/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php b/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php index 49be0fc..b924f48 100644 --- a/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php +++ b/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php @@ -189,11 +189,11 @@ public function testWorkflowCreation() { // Ensure that weight changes the state ordering. $workflow = $workflow_storage->loadUnchanged('test'); - $this->assertEquals('published', $workflow->getTypePlugin()->getInitialState($workflow)->id()); + $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($workflow)->id()); + $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'); diff --git a/core/modules/workflows/workflows.api.php b/core/modules/workflows/workflows.api.php new file mode 100644 index 0000000..ca55c87 --- /dev/null +++ b/core/modules/workflows/workflows.api.php @@ -0,0 +1,17 @@ +