Issue #2856967 by Sam152, yongt9412, alexpott, Berdir, larowlan, timmillwood, nlisgo: Allow admins to select a default starting moderation state

8.7.x
Alex Pott 2018-09-21 12:58:56 +01:00
parent 6b663aa312
commit b6a29fd2f4
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
10 changed files with 183 additions and 6 deletions

View File

@ -33,3 +33,6 @@ workflow.type_settings.content_moderation:
sequence:
type: string
label: 'Bundle ID'
default_moderation_state:
type: string
label: 'Default moderation state'

View File

@ -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;
}

View File

@ -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;
});
}

View File

@ -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);
}
}

View File

@ -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');
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Drupal\Tests\content_moderation\Functional;
/**
* Tests setting a custom default moderation state.
*
* @group content_moderation
*/
class DefaultModerationStateTest extends ModerationStateTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->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);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Drupal\Tests\content_moderation\Functional;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
use Drupal\workflows\Entity\Workflow;
/**
* Tests the upgrade path for updating the 'default_moderation_state' setting.
*
* @group Update
* @group legacy
*
* @see content_moderation_post_update_set_default_moderation_state()
*/
class DefaultModerationStateUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->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']);
}
}

View File

@ -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);
}
}

View File

@ -65,3 +65,4 @@ type_settings:
- article
- page
- recipe
default_moderation_state: draft

View File

@ -57,3 +57,4 @@ type_settings:
- draft
- published
entity_types: { }
default_moderation_state: draft