Commit d4165ff1 authored by catch's avatar catch
parent 69bf50fe
<?php
/**
* @file
* API documentation for Content Moderation module.
*/
/**
* @defgroup content_moderation_plugin Content Moderation Workflow Type Plugin
* @{
* The Workflow Type plugin implemented by Content Moderation links revisionable
* entities to workflows.
*
* In the Content Moderation Workflow Type Plugin, one method requires the
* entity object to be passed in as a parameter, even though the interface
* defined by Workflows module doesn't require this:
* @code
* $workflow_type_plugin->getInitialState($entity);
* @endcode
* This is used to determine the initial moderation state based on the
* publishing status of the entity.
* @}
*/
......@@ -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?
......
......@@ -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);
......
......@@ -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;
}
/**
......
......@@ -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();
}
}
......@@ -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());
......
......@@ -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);
}
......
......@@ -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.
......
......@@ -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');
......
<?php
/**
* @file
* API documentation for Workflows module.
*/
/**
* @defgroup workflow_type_plugins Workflow Type Plugins
* @{
* Any module harnessing Workflows module must define a Workflow Type Plugin.
* This allows the module to tailor the workflow to its specific need. For
* example, Content Moderation module uses its the Workflow Type Plugin to link
* workflows to entities. On their own, workflows are a stand-alone concept. It
* takes a module such as Content Moderation to give the workflow context.
* @}
*/
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment