diff --git a/core/modules/workflows/src/ConfigurableWorkflowTypeInterface.php b/core/modules/workflows/src/ConfigurableWorkflowTypeInterface.php new file mode 100644 index 0000000..57585b2 --- /dev/null +++ b/core/modules/workflows/src/ConfigurableWorkflowTypeInterface.php @@ -0,0 +1,16 @@ + $workflow->toLink($this->t('Add a new transition'), 'add-transition-form')->toString(), ]; + $is_configurable = $workflow->getTypePlugin() instanceof ConfigurableWorkflowTypeInterface; + $form_state->set('is_configurable', $is_configurable); + + if ($is_configurable) { + $form['type_settings'] = [ + '#tree' => TRUE, + ]; + $sub_form_state = SubformState::createForSubform($form['type_settings'], $form, $form_state); + $form['type_settings'] += $workflow->getTypePlugin()->buildConfigurationForm($form['type_settings'], $sub_form_state); + } + return $form; } /** * {@inheritdoc} */ + public function validateForm(array &$form, FormStateInterface $form_state) { + /* @var \Drupal\workflows\WorkflowInterface $workflow */ + $workflow = $this->entity; + if ($form_state->get('is_configurable')) { + $sub_form_state = SubformState::createForSubform($form['type_settings'], $form, $form_state); + $workflow->getTypePlugin()->validateConfigurationForm($form['type_settings'], $sub_form_state); + } + } + + /** + * {@inheritdoc} + */ public function save(array $form, FormStateInterface $form_state) { /* @var \Drupal\workflows\WorkflowInterface $workflow */ $workflow = $this->entity; + if ($form_state->get('is_configurable')) { + $sub_form_state = SubformState::createForSubform($form['type_settings'], $form, $form_state); + $workflow->getTypePlugin()->submitConfigurationForm($form['type_settings'], $sub_form_state); + } $workflow->save(); + drupal_set_message($this->t('Saved the %label Workflow.', ['%label' => $workflow->label()])); $form_state->setRedirectUrl($workflow->toUrl('collection')); } diff --git a/core/modules/workflows/src/Plugin/ConfigurableWorkflowTypeBase.php b/core/modules/workflows/src/Plugin/ConfigurableWorkflowTypeBase.php new file mode 100644 index 0000000..4dec6ce --- /dev/null +++ b/core/modules/workflows/src/Plugin/ConfigurableWorkflowTypeBase.php @@ -0,0 +1,30 @@ +configuration['states'][$state_id]); } /** - * {@inheritDoc} + * {@inheritdoc} */ public function decorateTransition(TransitionInterface $transition) { return $transition; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function deleteTransition($transition_id) { unset($this->configuration['transitions'][$transition_id]); @@ -91,14 +91,14 @@ public function buildTransitionConfigurationForm(FormStateInterface $form_state, } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getConfiguration() { return $this->configuration; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function setConfiguration(array $configuration) { $this->configuration = NestedArray::mergeDeep( @@ -108,7 +108,7 @@ public function setConfiguration(array $configuration) { } /** - * {@inheritDoc} + * {@inheritdoc} */ public function defaultConfiguration() { return [ @@ -118,7 +118,7 @@ public function defaultConfiguration() { } /** - * {@inheritDoc} + * {@inheritdoc} */ public function calculateDependencies() { return []; diff --git a/core/modules/workflows/src/WorkflowInterface.php b/core/modules/workflows/src/WorkflowInterface.php index 0efedea..b86f423 100644 --- a/core/modules/workflows/src/WorkflowInterface.php +++ b/core/modules/workflows/src/WorkflowInterface.php @@ -281,7 +281,7 @@ public function deleteTransition($transition_id); /** * Gets the workflow type plugin. * - * @return \Drupal\workflows\WorkflowTypeInterface + * @return \Drupal\workflows\WorkflowTypeInterface|\Drupal\workflows\ConfigurableWorkflowTypeInterface * The workflow type plugin. */ public function getTypePlugin(); diff --git a/core/modules/workflows/tests/modules/workflow_type_test/config/schema/workflow_type_test.schema.yml b/core/modules/workflows/tests/modules/workflow_type_test/config/schema/workflow_type_test.schema.yml index 4f12fdd..bf89faa 100644 --- a/core/modules/workflows/tests/modules/workflow_type_test/config/schema/workflow_type_test.schema.yml +++ b/core/modules/workflows/tests/modules/workflow_type_test/config/schema/workflow_type_test.schema.yml @@ -11,6 +11,9 @@ workflow.type_settings.workflow_type_complex_test: type: mapping label: 'Workflow complex test type settings' mapping: + example_setting: + type: string + label: 'Example setting' states: type: sequence label: 'Additional state configuration' diff --git a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/ComplexTestType.php b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/ComplexTestType.php index 8440c1c..458ada4 100644 --- a/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/ComplexTestType.php +++ b/core/modules/workflows/tests/modules/workflow_type_test/src/Plugin/WorkflowType/ComplexTestType.php @@ -4,7 +4,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\workflows\Plugin\WorkflowTypeBase; +use Drupal\workflows\Plugin\ConfigurableWorkflowTypeBase; use Drupal\workflows\StateInterface; use Drupal\workflows\TransitionInterface; use Drupal\workflows\WorkflowInterface; @@ -19,7 +19,7 @@ * label = @Translation("Workflow Type Complex Test"), * ) */ -class ComplexTestType extends WorkflowTypeBase { +class ComplexTestType extends ConfigurableWorkflowTypeBase { use StringTranslationTrait; @@ -79,4 +79,33 @@ public function buildTransitionConfigurationForm(FormStateInterface $form_state, return $form; } + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return parent::defaultConfiguration() + [ + 'example_setting' => '', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['example_setting'] = [ + '#type' => 'textfield', + '#title' => $this->t('Example global workflow setting'), + '#description' => $this->t('Extra information added to the workflow'), + '#default_value' => $this->configuration['example_setting'], + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['example_setting'] = $form_state->getValue('example_setting'); + } + } diff --git a/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php b/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php index ed5eb6f..5151056 100644 --- a/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php +++ b/core/modules/workflows/tests/src/Functional/WorkflowUiTest.php @@ -213,6 +213,28 @@ public function testWorkflowCreation() { } /** + * Test the workflow configuration form. + */ + public function testWorkflowConfigurationForm() { + $workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test', 'label' => 'Test']); + $workflow + ->addState('published', 'Published') + ->addTransition('publish', 'Publish', ['published'], 'published') + ->save(); + + $this->drupalLogin($this->createUser(['administer workflows'])); + + // Add additional information to the workflow via the configuration form. + $this->drupalGet('admin/config/workflow/workflows/manage/test'); + $this->assertSession()->pageTextContains('Example global workflow setting'); + $this->submitForm(['type_settings[example_setting]' => 'Extra global settings'], 'Save'); + + $workflow_storage = $this->container->get('entity_type.manager')->getStorage('workflow'); + $workflow = $workflow_storage->loadUnchanged('test'); + $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() {