From b6a29fd2f452e576be691f57214924e42ee80f11 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Fri, 21 Sep 2018 12:58:56 +0100 Subject: [PATCH] Issue #2856967 by Sam152, yongt9412, alexpott, Berdir, larowlan, timmillwood, nlisgo: Allow admins to select a default starting moderation state --- .../schema/content_moderation.schema.yml | 3 + .../content_moderation.module | 9 +++ .../content_moderation.post_update.php | 16 ++++++ .../Form/ContentModerationConfigureForm.php | 21 ++++++- .../Plugin/WorkflowType/ContentModeration.php | 8 +-- .../Functional/DefaultModerationStateTest.php | 56 +++++++++++++++++++ .../DefaultModerationStateUpdateTest.php | 41 ++++++++++++++ .../ModerationStateFieldItemListTest.php | 33 +++++++++++ .../install/workflows.workflow.editorial.yml | 1 + .../optional/workflows.workflow.editorial.yml | 1 + 10 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 core/modules/content_moderation/tests/src/Functional/DefaultModerationStateTest.php create mode 100644 core/modules/content_moderation/tests/src/Functional/DefaultModerationStateUpdateTest.php diff --git a/core/modules/content_moderation/config/schema/content_moderation.schema.yml b/core/modules/content_moderation/config/schema/content_moderation.schema.yml index d48ffe439f8..7b14efbe492 100644 --- a/core/modules/content_moderation/config/schema/content_moderation.schema.yml +++ b/core/modules/content_moderation/config/schema/content_moderation.schema.yml @@ -33,3 +33,6 @@ workflow.type_settings.content_moderation: sequence: type: string label: 'Bundle ID' + default_moderation_state: + type: string + label: 'Default moderation state' diff --git a/core/modules/content_moderation/content_moderation.module b/core/modules/content_moderation/content_moderation.module index 3481c243e18..347cbaf8aa5 100644 --- a/core/modules/content_moderation/content_moderation.module +++ b/core/modules/content_moderation/content_moderation.module @@ -207,6 +207,15 @@ function content_moderation_entity_access(EntityInterface $entity, $operation, A } } + // Do not allow users to delete the state that is configured as the default + // state for the workflow. + if ($entity instanceof WorkflowInterface) { + $configuration = $entity->getTypePlugin()->getConfiguration(); + if (!empty($configuration['default_moderation_state']) && $operation === sprintf('delete-state:%s', $configuration['default_moderation_state'])) { + return AccessResult::forbidden()->addCacheableDependency($entity); + } + } + return $access_result; } diff --git a/core/modules/content_moderation/content_moderation.post_update.php b/core/modules/content_moderation/content_moderation.post_update.php index ff5a28a9d3b..49f8fd73fcf 100644 --- a/core/modules/content_moderation/content_moderation.post_update.php +++ b/core/modules/content_moderation/content_moderation.post_update.php @@ -5,6 +5,7 @@ * Post update functions for the Content Moderation module. */ +use Drupal\Core\Config\Entity\ConfigEntityUpdater; use Drupal\Core\Site\Settings; use Drupal\workflows\Entity\Workflow; @@ -94,3 +95,18 @@ function content_moderation_post_update_update_cms_default_revisions(&$sandbox) $sandbox['offset'] += $sandbox['limit']; } + +/** + * Set the default moderation state for new content to 'draft'. + */ +function content_moderation_post_update_set_default_moderation_state(&$sandbox) { + \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'workflow', function (Workflow $workflow) { + if ($workflow->get('type') === 'content_moderation') { + $configuration = $workflow->getTypePlugin()->getConfiguration(); + $configuration['default_moderation_state'] = 'draft'; + $workflow->getTypePlugin()->setConfiguration($configuration); + return TRUE; + } + return FALSE; + }); +} diff --git a/core/modules/content_moderation/src/Form/ContentModerationConfigureForm.php b/core/modules/content_moderation/src/Form/ContentModerationConfigureForm.php index 274b9f8e378..f43451ec63d 100644 --- a/core/modules/content_moderation/src/Form/ContentModerationConfigureForm.php +++ b/core/modules/content_moderation/src/Form/ContentModerationConfigureForm.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\workflows\Plugin\WorkflowTypeConfigureFormBase; +use Drupal\workflows\State; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -128,6 +129,21 @@ class ContentModerationConfigureForm extends WorkflowTypeConfigureFormBase imple ], ]; } + + $workflow_type_configuration = $this->workflowType->getConfiguration(); + $form['workflow_settings'] = [ + '#type' => 'details', + '#title' => $this->t('Workflow Settings'), + '#open' => TRUE, + ]; + $form['workflow_settings']['default_moderation_state'] = [ + '#title' => $this->t('Default moderation state'), + '#type' => 'select', + '#required' => TRUE, + '#options' => array_map([State::class, 'labelCallback'], $this->workflowType->getStates()), + '#description' => $this->t('Select the state that new content will be assigned. This state will appear as the default in content forms and the available target states will be based on the transitions available from this state.'), + '#default_value' => isset($workflow_type_configuration['default_moderation_state']) ? $workflow_type_configuration['default_moderation_state'] : 'draft', + ]; return $form; } @@ -135,8 +151,9 @@ class ContentModerationConfigureForm extends WorkflowTypeConfigureFormBase imple * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { - // Configuration is updated from modal windows launched from this form, no - // need to change any configuration here. + $configuration = $this->workflowType->getConfiguration(); + $configuration['default_moderation_state'] = $form_state->getValue(['workflow_settings', 'default_moderation_state']); + $this->workflowType->setConfiguration($configuration); } } diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index 358314a4cad..5182c6b3fd9 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -306,11 +306,11 @@ class ContentModeration extends WorkflowTypeBase implements ContentModerationInt if (!($entity instanceof ContentEntityInterface)) { throw new \InvalidArgumentException('A content entity object must be supplied.'); } - if ($entity instanceof EntityPublishedInterface) { - return $this->getState($entity->isPublished() && !$entity->isNew() ? 'published' : 'draft'); + if ($entity instanceof EntityPublishedInterface && !$entity->isNew()) { + return $this->getState($entity->isPublished() ? 'published' : 'draft'); } - // Workflows determines the initial state for non-publishable entities. - return parent::getInitialState(); + + return $this->getState(!empty($this->configuration['default_moderation_state']) ? $this->configuration['default_moderation_state'] : 'draft'); } } diff --git a/core/modules/content_moderation/tests/src/Functional/DefaultModerationStateTest.php b/core/modules/content_moderation/tests/src/Functional/DefaultModerationStateTest.php new file mode 100644 index 00000000000..f5f6a5c004a --- /dev/null +++ b/core/modules/content_moderation/tests/src/Functional/DefaultModerationStateTest.php @@ -0,0 +1,56 @@ +drupalLogin($this->adminUser); + $this->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE); + $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content'); + } + + /** + * Test a workflow with a default moderation state set. + */ + public function testPublishedDefaultState() { + // Set the default moderation state to be "published". + $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $this->workflow->id(), [ + 'type_settings[workflow_settings][default_moderation_state]' => 'published', + ], 'Save'); + + $this->drupalGet('node/add/moderated_content'); + $this->assertEquals('published', $this->assertSession()->selectExists('moderation_state[0][state]')->getValue()); + $this->submitForm([ + 'title[0][value]' => 'moderated content', + ], 'Save'); + + $node = $this->getNodeByTitle('moderated content'); + $this->assertEquals('published', $node->moderation_state->value); + } + + /** + * Test access to deleting the default state. + */ + public function testDeleteDefaultStateAccess() { + $this->drupalGet('admin/config/workflow/workflows/manage/editorial/state/archived/delete'); + $this->assertSession()->statusCodeEquals(200); + + $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $this->workflow->id(), [ + 'type_settings[workflow_settings][default_moderation_state]' => 'archived', + ], 'Save'); + + $this->drupalGet('admin/config/workflow/workflows/manage/editorial/state/archived/delete'); + $this->assertSession()->statusCodeEquals(403); + } + +} diff --git a/core/modules/content_moderation/tests/src/Functional/DefaultModerationStateUpdateTest.php b/core/modules/content_moderation/tests/src/Functional/DefaultModerationStateUpdateTest.php new file mode 100644 index 00000000000..35ba1dd1e03 --- /dev/null +++ b/core/modules/content_moderation/tests/src/Functional/DefaultModerationStateUpdateTest.php @@ -0,0 +1,41 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.4.0.bare.standard.php.gz', + __DIR__ . '/../../fixtures/update/drupal-8.4.0-content_moderation_installed.php', + ]; + } + + /** + * Tests updating the default moderation state setting. + */ + public function testUpdateDefaultModerationState() { + $workflow = Workflow::load('editorial'); + $this->assertArrayNotHasKey('default_moderation_state', $workflow->getTypePlugin()->getConfiguration()); + + $this->runUpdates(); + + $workflow = Workflow::load('editorial'); + $this->assertEquals('draft', $workflow->getTypePlugin()->getConfiguration()['default_moderation_state']); + } + +} diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php index af0bb4fd7d6..844e77d9308 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ModerationStateFieldItemListTest.php @@ -6,6 +6,7 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait; +use Drupal\workflows\Entity\Workflow; /** * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList @@ -281,4 +282,36 @@ class ModerationStateFieldItemListTest extends KernelTestBase { ]; } + /** + * Test customising the default moderation state. + */ + public function testWorkflowCustomisedInitialState() { + $workflow = Workflow::load('editorial'); + $configuration = $workflow->getTypePlugin()->getConfiguration(); + + // Test a node for a workflow that hasn't been updated to include the + // 'default_moderation_state' setting. We must be backwards compatible with + // configuration that was exported before this change was introduced. + $this->assertFalse(isset($configuration['default_moderation_state'])); + $legacy_configuration_node = Node::create([ + 'title' => 'Test title', + 'type' => 'example', + ]); + $this->assertEquals('draft', $legacy_configuration_node->moderation_state->value); + $legacy_configuration_node->save(); + $this->assertEquals('draft', $legacy_configuration_node->moderation_state->value); + + $configuration['default_moderation_state'] = 'published'; + $workflow->getTypePlugin()->setConfiguration($configuration); + $workflow->save(); + + $updated_default_node = Node::create([ + 'title' => 'Test title', + 'type' => 'example', + ]); + $this->assertEquals('published', $updated_default_node->moderation_state->value); + $legacy_configuration_node->save(); + $this->assertEquals('published', $updated_default_node->moderation_state->value); + } + } diff --git a/core/profiles/demo_umami/config/install/workflows.workflow.editorial.yml b/core/profiles/demo_umami/config/install/workflows.workflow.editorial.yml index 9959410e1e6..601289de944 100644 --- a/core/profiles/demo_umami/config/install/workflows.workflow.editorial.yml +++ b/core/profiles/demo_umami/config/install/workflows.workflow.editorial.yml @@ -65,3 +65,4 @@ type_settings: - article - page - recipe + default_moderation_state: draft diff --git a/core/profiles/standard/config/optional/workflows.workflow.editorial.yml b/core/profiles/standard/config/optional/workflows.workflow.editorial.yml index 52dbc27542d..0d1154b36e3 100644 --- a/core/profiles/standard/config/optional/workflows.workflow.editorial.yml +++ b/core/profiles/standard/config/optional/workflows.workflow.editorial.yml @@ -57,3 +57,4 @@ type_settings: - draft - published entity_types: { } + default_moderation_state: draft