Unverified Commit 42a76cf1 authored by larowlan's avatar larowlan
Browse files

Issue #2896721 by Sam152, Wim Leers: Remove the concept of decorated...

Issue #2896721 by Sam152, Wim Leers: Remove the concept of decorated states/transitions from worfklow type plugins.
parent a16b9c74
......@@ -104,7 +104,8 @@ public function checkWorkflowAccess(WorkflowInterface $entity, $operation, Accou
/**
* {@inheritdoc}
*/
public function decorateState(StateInterface $state) {
public function getState($state_id) {
$state = parent::getState($state_id);
if (isset($this->configuration['states'][$state->id()]['published']) && isset($this->configuration['states'][$state->id()]['default_revision'])) {
$state = new ContentModerationState($state, $this->configuration['states'][$state->id()]['published'], $this->configuration['states'][$state->id()]['default_revision']);
}
......
......@@ -75,20 +75,6 @@ public function workflowStateHasData(WorkflowInterface $workflow, StateInterface
return FALSE;
}
/**
* {@inheritdoc}
*/
public function decorateState(StateInterface $state) {
return $state;
}
/**
* {@inheritdoc}
*/
public function decorateTransition(TransitionInterface $transition) {
return $transition;
}
/**
* {@inheritdoc}
*/
......@@ -200,13 +186,12 @@ public function getState($state_id) {
if (!isset($this->configuration['states'][$state_id])) {
throw new \InvalidArgumentException("The state '$state_id' does not exist in workflow.'");
}
$state = new State(
return new State(
$this,
$state_id,
$this->configuration['states'][$state_id]['label'],
$this->configuration['states'][$state_id]['weight']
);
return $this->decorateState($state);
}
/**
......@@ -337,7 +322,7 @@ public function getTransition($transition_id) {
if (!$this->hasTransition($transition_id)) {
throw new \InvalidArgumentException("The transition '$transition_id' does not exist in workflow.'");
}
$transition = new Transition(
return new Transition(
$this,
$transition_id,
$this->configuration['transitions'][$transition_id]['label'],
......@@ -345,7 +330,6 @@ public function getTransition($transition_id) {
$this->configuration['transitions'][$transition_id]['to'],
$this->configuration['transitions'][$transition_id]['weight']
);
return $this->decorateTransition($transition);
}
/**
......
......@@ -5,8 +5,6 @@
/**
* An interface for state value objects.
*
* @see \Drupal\workflows\WorkflowTypeInterface::decorateState()
*
* @internal
* The workflow system is currently experimental and should only be leveraged
* by experimental modules and development releases of contributed modules.
......
......@@ -88,27 +88,6 @@ public function workflowHasData(WorkflowInterface $workflow);
*/
public function workflowStateHasData(WorkflowInterface $workflow, StateInterface $state);
/**
* Decorates states so the WorkflowType can add additional information.
*
* @param \Drupal\workflows\StateInterface $state
* The state object to decorate.
*
* @return \Drupal\workflows\StateInterface
* The decorated state object.
*/
public function decorateState(StateInterface $state);
/**
* Decorates transitions so the WorkflowType can add additional information.
* @param \Drupal\workflows\TransitionInterface $transition
* The transition object to decorate.
*
* @return \Drupal\workflows\TransitionInterface
* The decorated transition object.
*/
public function decorateTransition(TransitionInterface $transition);
/**
* Gets the initial state for the workflow.
*
......
<?php
namespace Drupal\workflow_type_test;
use Drupal\workflows\StateInterface;
/**
* A value object representing a workflow state.
*/
class DecoratedState implements StateInterface {
/**
* The vanilla state object from the Workflow module.
*
* @var \Drupal\workflows\StateInterface
*/
protected $state;
/**
* Extra information added to state.
*
* @var string
*/
protected $extra;
/**
* DecoratedState constructor.
*
* @param \Drupal\workflows\StateInterface $state
* The vanilla state object from the Workflow module.
* @param string $extra
* (optional) Extra information stored on the state. Defaults to ''.
*/
public function __construct(StateInterface $state, $extra = '') {
$this->state = $state;
$this->extra = $extra;
}
/**
* Gets the extra information stored on the state.
*
* @return string
*/
public function getExtra() {
return $this->extra;
}
/**
* {@inheritdoc}
*/
public function id() {
return $this->state->id();
}
/**
* {@inheritdoc}
*/
public function label() {
return $this->state->label();
}
/**
* {@inheritdoc}
*/
public function weight() {
return $this->state->weight();
}
/**
* {@inheritdoc}
*/
public function canTransitionTo($to_state_id) {
return $this->state->canTransitionTo($to_state_id);
}
/**
* {@inheritdoc}
*/
public function getTransitionTo($to_state_id) {
return $this->state->getTransitionTo($to_state_id);
}
/**
* {@inheritdoc}
*/
public function getTransitions() {
return $this->state->getTransitions();
}
}
<?php
namespace Drupal\workflow_type_test;
use Drupal\workflows\TransitionInterface;
/**
* A value object representing a workflow transition.
*/
class DecoratedTransition implements TransitionInterface {
/**
* The vanilla transition object from the Workflow module.
*
* @var \Drupal\workflows\TransitionInterface
*/
protected $transition;
/**
* Extra information added to transition.
*
* @var string
*/
protected $extra;
/**
* DecoratedTransition constructor.
*
* @param \Drupal\workflows\TransitionInterface $transition
* The vanilla transition object from the Workflow module.
* @param string $extra
* (optional) Extra information stored on the transition. Defaults to ''.
*/
public function __construct(TransitionInterface $transition, $extra = '') {
$this->transition = $transition;
$this->extra = $extra;
}
/**
* Gets the extra information stored on the transition.
*
* @return string
*/
public function getExtra() {
return $this->extra;
}
/**
* {@inheritdoc}
*/
public function id() {
return $this->transition->id();
}
/**
* {@inheritdoc}
*/
public function label() {
return $this->transition->label();
}
/**
* {@inheritdoc}
*/
public function from() {
return $this->transition->from();
}
/**
* {@inheritdoc}
*/
public function to() {
return $this->transition->to();
}
/**
* {@inheritdoc}
*/
public function weight() {
return $this->transition->weight();
}
}
......@@ -8,8 +8,6 @@
use Drupal\workflows\StateInterface;
use Drupal\workflows\TransitionInterface;
use Drupal\workflows\WorkflowInterface;
use Drupal\workflow_type_test\DecoratedState;
use Drupal\workflow_type_test\DecoratedTransition;
/**
* Test workflow type.
......@@ -23,45 +21,16 @@ class ComplexTestType extends WorkflowTypeFormBase {
use StringTranslationTrait;
/**
* {@inheritDoc}
*/
public function decorateState(StateInterface $state) {
if (isset($this->configuration['states'][$state->id()])) {
$extra = isset($this->configuration['states'][$state->id()]['extra']) ? $this->configuration['states'][$state->id()]['extra'] : '';
$state = new DecoratedState($state, $extra);
}
else {
$state = new DecoratedState($state);
}
return $state;
}
/**
* {@inheritDoc}
*/
public function decorateTransition(TransitionInterface $transition) {
if (isset($this->configuration['transitions'][$transition->id()])) {
$extra = isset($this->configuration['transitions'][$transition->id()]['extra']) ? $this->configuration['transitions'][$transition->id()]['extra'] : '';
$transition = new DecoratedTransition($transition, $extra);
}
else {
$transition = new DecoratedTransition($transition);
}
return $transition;
}
/**
* {@inheritdoc}
*/
public function buildStateConfigurationForm(FormStateInterface $form_state, WorkflowInterface $workflow, StateInterface $state = NULL) {
/** @var \Drupal\workflow_type_test\DecoratedState $state */
$form = [];
$form['extra'] = [
'#type' => 'textfield',
'#title' => $this->t('Extra'),
'#description' => $this->t('Extra information added to state'),
'#default_value' => isset($state) ? $state->getExtra() : FALSE,
'#default_value' => isset($this->configuration['states'][$state->id()]['extra']) ? $this->configuration['states'][$state->id()]['extra'] : '',
];
return $form;
}
......@@ -70,13 +39,12 @@ public function buildStateConfigurationForm(FormStateInterface $form_state, Work
* {@inheritdoc}
*/
public function buildTransitionConfigurationForm(FormStateInterface $form_state, WorkflowInterface $workflow, TransitionInterface $transition = NULL) {
/** @var \Drupal\workflow_type_test\DecoratedTransition $transition */
$form = [];
$form['extra'] = [
'#type' => 'textfield',
'#title' => $this->t('Extra'),
'#description' => $this->t('Extra information added to transition'),
'#default_value' => isset($transition) ? $transition->getExtra() : FALSE,
'#default_value' => isset($this->configuration['transitions'][$transition->id()]['extra']) ? $this->configuration['transitions'][$transition->id()]['extra'] : '',
];
return $form;
}
......
......@@ -292,52 +292,4 @@ public function testWorkflowConfigurationForm() {
$this->assertEquals('Extra global settings', $workflow->getTypePlugin()->getConfiguration()['example_setting']);
}
/**
* Tests that workflow types can add form fields to states and transitions.
*/
public function testWorkflowDecoration() {
// Create a minimal workflow for testing.
$workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test']);
$workflow
->getTypePlugin()
->addState('published', 'Published')
->addTransition('publish', 'Publish', ['published'], 'published');
$workflow->save();
$this->assertEquals('', $workflow->getTypePlugin()->getState('published')->getExtra());
$this->assertEquals('', $workflow->getTypePlugin()->getTransition('publish')->getExtra());
$this->drupalLogin($this->createUser(['administer workflows']));
// Add additional state information when editing.
$this->drupalGet('admin/config/workflow/workflows/manage/test/state/published');
$this->assertSession()->pageTextContains('Extra information added to state');
$this->submitForm(['type_settings[workflow_type_complex_test][extra]' => 'Extra state information'], 'Save');
// Add additional transition information when editing.
$this->drupalGet('admin/config/workflow/workflows/manage/test/transition/publish');
$this->assertSession()->pageTextContains('Extra information added to transition');
$this->submitForm(['type_settings[workflow_type_complex_test][extra]' => 'Extra transition information'], 'Save');
$workflow_storage = $this->container->get('entity_type.manager')->getStorage('workflow');
/** @var \Drupal\workflows\WorkflowInterface $workflow */
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertEquals('Extra state information', $workflow->getTypePlugin()->getState('published')->getExtra());
$this->assertEquals('Extra transition information', $workflow->getTypePlugin()->getTransition('publish')->getExtra());
// Add additional state information when adding.
$this->drupalGet('admin/config/workflow/workflows/manage/test/add_state');
$this->assertSession()->pageTextContains('Extra information added to state');
$this->submitForm(['label' => 'Draft', 'id' => 'draft', 'type_settings[workflow_type_complex_test][extra]' => 'Extra state information on add'], 'Save');
// Add additional transition information when adding.
$this->drupalGet('admin/config/workflow/workflows/manage/test/add_transition');
$this->assertSession()->pageTextContains('Extra information added to transition');
$this->submitForm(['id' => 'draft_published', 'label' => 'Publish', 'from[draft]' => 'draft', 'to' => 'published', 'type_settings[workflow_type_complex_test][extra]' => 'Extra transition information on add'], 'Save');
$workflow = $workflow_storage->loadUnchanged('test');
$this->assertEquals('Extra state information on add', $workflow->getTypePlugin()->getState('draft')->getExtra());
$this->assertEquals('Extra transition information on add', $workflow->getTypePlugin()->getTransition('draft_published')->getExtra());
}
}
......@@ -4,8 +4,6 @@
use Drupal\KernelTests\KernelTestBase;
use Drupal\workflows\Entity\Workflow;
use Drupal\workflow_type_test\DecoratedState;
use Drupal\workflow_type_test\DecoratedTransition;
/**
* Workflow entity tests that require modules or storage.
......@@ -21,22 +19,6 @@ class ComplexWorkflowTypeTest extends KernelTestBase {
*/
public static $modules = ['workflows', 'workflow_type_test'];
/**
* Tests a workflow type that decorates transitions and states.
*
* @covers ::getState
* @covers ::getTransition
*/
public function testComplexType() {
$workflow = new Workflow(['id' => 'test', 'type' => 'workflow_type_complex_test'], 'workflow');
$workflow
->getTypePlugin()
->addState('draft', 'Draft')
->addTransition('create_new_draft', 'Create new draft', ['draft'], 'draft');
$this->assertInstanceOf(DecoratedState::class, $workflow->getTypePlugin()->getState('draft'));
$this->assertInstanceOf(DecoratedTransition::class, $workflow->getTypePlugin()->getTransition('create_new_draft'));
}
/**
* @covers \Drupal\workflows\Entity\Workflow::loadMultipleByType
*/
......
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