Issue #2856967 by Sam152, yongt9412, alexpott, Berdir, larowlan, timmillwood, nlisgo: Allow admins to select a default starting moderation state
parent
6b663aa312
commit
b6a29fd2f4
|
@ -33,3 +33,6 @@ workflow.type_settings.content_moderation:
|
||||||
sequence:
|
sequence:
|
||||||
type: string
|
type: string
|
||||||
label: 'Bundle ID'
|
label: 'Bundle ID'
|
||||||
|
default_moderation_state:
|
||||||
|
type: string
|
||||||
|
label: 'Default moderation state'
|
||||||
|
|
|
@ -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;
|
return $access_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
* Post update functions for the Content Moderation module.
|
* Post update functions for the Content Moderation module.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
|
||||||
use Drupal\Core\Site\Settings;
|
use Drupal\Core\Site\Settings;
|
||||||
use Drupal\workflows\Entity\Workflow;
|
use Drupal\workflows\Entity\Workflow;
|
||||||
|
|
||||||
|
@ -94,3 +95,18 @@ function content_moderation_post_update_update_cms_default_revisions(&$sandbox)
|
||||||
|
|
||||||
$sandbox['offset'] += $sandbox['limit'];
|
$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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
use Drupal\Core\Form\FormStateInterface;
|
use Drupal\Core\Form\FormStateInterface;
|
||||||
use Drupal\Core\Url;
|
use Drupal\Core\Url;
|
||||||
use Drupal\workflows\Plugin\WorkflowTypeConfigureFormBase;
|
use Drupal\workflows\Plugin\WorkflowTypeConfigureFormBase;
|
||||||
|
use Drupal\workflows\State;
|
||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
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;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,8 +151,9 @@ class ContentModerationConfigureForm extends WorkflowTypeConfigureFormBase imple
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||||
// Configuration is updated from modal windows launched from this form, no
|
$configuration = $this->workflowType->getConfiguration();
|
||||||
// need to change any configuration here.
|
$configuration['default_moderation_state'] = $form_state->getValue(['workflow_settings', 'default_moderation_state']);
|
||||||
|
$this->workflowType->setConfiguration($configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -306,11 +306,11 @@ class ContentModeration extends WorkflowTypeBase implements ContentModerationInt
|
||||||
if (!($entity instanceof ContentEntityInterface)) {
|
if (!($entity instanceof ContentEntityInterface)) {
|
||||||
throw new \InvalidArgumentException('A content entity object must be supplied.');
|
throw new \InvalidArgumentException('A content entity object must be supplied.');
|
||||||
}
|
}
|
||||||
if ($entity instanceof EntityPublishedInterface) {
|
if ($entity instanceof EntityPublishedInterface && !$entity->isNew()) {
|
||||||
return $this->getState($entity->isPublished() && !$entity->isNew() ? 'published' : 'draft');
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ use Drupal\KernelTests\KernelTestBase;
|
||||||
use Drupal\node\Entity\Node;
|
use Drupal\node\Entity\Node;
|
||||||
use Drupal\node\Entity\NodeType;
|
use Drupal\node\Entity\NodeType;
|
||||||
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
|
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
|
||||||
|
use Drupal\workflows\Entity\Workflow;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,3 +65,4 @@ type_settings:
|
||||||
- article
|
- article
|
||||||
- page
|
- page
|
||||||
- recipe
|
- recipe
|
||||||
|
default_moderation_state: draft
|
||||||
|
|
|
@ -57,3 +57,4 @@ type_settings:
|
||||||
- draft
|
- draft
|
||||||
- published
|
- published
|
||||||
entity_types: { }
|
entity_types: { }
|
||||||
|
default_moderation_state: draft
|
||||||
|
|
Loading…
Reference in New Issue