diff --git a/core/modules/content_moderation/content_moderation.module b/core/modules/content_moderation/content_moderation.module index 3686710..171092f 100644 --- a/core/modules/content_moderation/content_moderation.module +++ b/core/modules/content_moderation/content_moderation.module @@ -249,5 +249,7 @@ function content_moderation_workflow_update(WorkflowInterface $entity) { */ function content_moderation_entity_type_build(array &$entity_types) { /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */ - $entity_types['workflow']->setFormClass('edit-type', WorkflowTypeEditForm::class); + if (isset($entity_types['workflow'])) { + $entity_types['workflow']->setFormClass('edit-type', WorkflowTypeEditForm::class); + } } diff --git a/core/modules/content_moderation/content_moderation.routing.yml b/core/modules/content_moderation/content_moderation.routing.yml index e69de29..2e2c5ce 100644 --- a/core/modules/content_moderation/content_moderation.routing.yml +++ b/core/modules/content_moderation/content_moderation.routing.yml @@ -0,0 +1,7 @@ +entity.workflow.edit_type_form: + path: '/admin/config/workflow/workflows/manage/{workflow}/type/{entity_type}' + defaults: + _entity_form: 'workflow.edit-type' + _title: 'Add / remove type' + requirements: + _entity_access: 'workflow.edit' \ No newline at end of file diff --git a/core/modules/content_moderation/src/Form/WorkflowTypeEditForm.php b/core/modules/content_moderation/src/Form/WorkflowTypeEditForm.php index e69de29..97ff773 100644 --- a/core/modules/content_moderation/src/Form/WorkflowTypeEditForm.php +++ b/core/modules/content_moderation/src/Form/WorkflowTypeEditForm.php @@ -0,0 +1,115 @@ +workflow = $workflow; + $this->entityType = $this->entityTypeManager->getDefinition($entity_type); + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function form(array $form, FormStateInterface $form_state) { + $form = parent::form($form, $form_state); + + $options = []; + $defaults =[]; + foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle) { + if (!\Drupal::service('content_moderation.moderation_information')->shouldModerateEntitiesOfBundle($this->entityType, $bundle_id) + || $this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id(), $bundle_id)) { + $options[$bundle_id] = ['type' => $bundle['label']]; + $defaults[$bundle_id] = $this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id(), $bundle_id); + } + } + + if (!empty($options)) { + $form['bundles'] = [ + '#type' => 'tableselect', + '#header' => [ + 'type' => $this->t('Type'), + ], + '#options' => $options, + '#default_value' => $defaults, + ]; + } + + return $form; + } + + /** + * {@inheritdoc} + */ + public function actions(array $form, FormStateInterface $form_state) { + $actions['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Save'), + '#ajax' => [ + 'callback' => [$this, 'ajaxSave'], + ], + ]; + return $actions; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + foreach ($form_state->getValue('bundles') as $bundle_id => $checked) { + if ($checked) { + $this->workflow->getTypePlugin()->addEntityTypeAndBundle($this->entityType->id(), $bundle_id); + } + else { + $this->workflow->getTypePlugin()->removeEntityTypeAndBundle($this->entityType->id(), $bundle_id); + } + } + $this->workflow->save(); + } + + /** + * Ajax callback. + * + * @param array $form + * @param \Drupal\Core\Form\FormStateInterface $form_state + * + * @return \Drupal\Core\Ajax\AjaxResponse + */ + public function ajaxSave(array $form, FormStateInterface $form_state) { + $selected_bundles = []; + foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle) { + if ($this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id(), $bundle_id)) { + $selected_bundles[$bundle_id] = $bundle['label']; + } + } + $response = new AjaxResponse(); + $response->addCommand(new CloseDialogCommand()); + $response->addCommand(new HtmlCommand('#selected-' . $this->entityType->id(), !empty($selected_bundles) ? $this->t("
Selected: %bundles
", ['%bundles' => implode(', ', $selected_bundles)]) : '')); + return $response; + } + +} diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index 76fa18a..2dba0e6 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -2,6 +2,7 @@ namespace Drupal\content_moderation\Plugin\WorkflowType; +use Drupal\Component\Serialization\Json; use Drupal\content_moderation\ModerationInformationInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; @@ -377,6 +378,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta 'aria-label' => $this->t('Add / remove types'), 'class' => ['use-ajax'], 'data-dialog-type' => 'modal', + 'data-dialog-options' => Json::encode([ + 'width' => 700, + 'height' => 400, + ]), ], ], ], diff --git a/core/modules/content_moderation/src/Tests/ModerationStateBlockTest.php b/core/modules/content_moderation/src/Tests/ModerationStateBlockTest.php index c0913ed..d2ff836 100644 --- a/core/modules/content_moderation/src/Tests/ModerationStateBlockTest.php +++ b/core/modules/content_moderation/src/Tests/ModerationStateBlockTest.php @@ -52,9 +52,8 @@ public function testCustomBlockModeration() { $this->drupalLogin($this->rootUser); // Enable moderation for custom blocks. - $edit['type_settings[attachments][block_content][basic]'] = TRUE; - $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial', $edit, t('Save')); - $this->assertText(t('Saved the Editorial workflow Workflow.')); + $edit['bundles[basic]'] = TRUE; + $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/block_content', $edit, t('Save')); // Create a custom block at block/add and save it as draft. $body = 'Body of moderated block'; diff --git a/core/modules/content_moderation/src/Tests/ModerationStateNodeTest.php b/core/modules/content_moderation/src/Tests/ModerationStateNodeTest.php index 53ce3a4..2525331 100644 --- a/core/modules/content_moderation/src/Tests/ModerationStateNodeTest.php +++ b/core/modules/content_moderation/src/Tests/ModerationStateNodeTest.php @@ -52,8 +52,8 @@ public function testCreatingContent() { $this->assertText(t('The Moderated content moderated content has been deleted.')); // Disable content moderation. - $edit['type_settings[attachments][node][moderated_content]'] = FALSE; - $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial', $edit, t('Save'));; + $edit['bundles[moderated_content]'] = FALSE; + $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', $edit, t('Save'));; // Ensure the parent environment is up-to-date. // @see content_moderation_workflow_insert() \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); diff --git a/core/modules/content_moderation/src/Tests/ModerationStateTestBase.php b/core/modules/content_moderation/src/Tests/ModerationStateTestBase.php index a1b4dba..e8fbc5b 100644 --- a/core/modules/content_moderation/src/Tests/ModerationStateTestBase.php +++ b/core/modules/content_moderation/src/Tests/ModerationStateTestBase.php @@ -119,8 +119,10 @@ protected function createContentTypeFromUi($content_type_name, $content_type_id, public function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') { $this->drupalGet('/admin/config/workflow/workflows'); $this->assertLinkByHref('admin/config/workflow/workflows/manage/' . $workflow_id); - $edit['type_settings[attachments][node][' . $content_type_id . ']'] = TRUE; - $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id, $edit, t('Save')); + $this->drupalGet('/admin/config/workflow/workflows/manage/' . $workflow_id); + $this->assertText('Content'); + $edit['bundles[' . $content_type_id . ']'] = TRUE; + $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node', $edit, t('Save')); // Ensure the parent environment is up-to-date. // @see content_moderation_workflow_insert() \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationStateTestBase.php b/core/modules/content_moderation/tests/src/Functional/ModerationStateTestBase.php index 806954d..4c7cc6e 100644 --- a/core/modules/content_moderation/tests/src/Functional/ModerationStateTestBase.php +++ b/core/modules/content_moderation/tests/src/Functional/ModerationStateTestBase.php @@ -116,8 +116,8 @@ protected function createContentTypeFromUi($content_type_name, $content_type_id, public function enableModerationThroughUi($content_type_id, $workflow_id = 'editorial') { $this->drupalGet('/admin/config/workflow/workflows'); $this->assertLinkByHref('admin/config/workflow/workflows/manage/' . $workflow_id); - $edit['type_settings[attachments][node][' . $content_type_id . ']'] = TRUE; - $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id, $edit, t('Save')); + $edit['bundles[' . $content_type_id . ']'] = TRUE; + $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/node', $edit, t('Save')); // Ensure the parent environment is up-to-date. // @see content_moderation_workflow_insert() \Drupal::service('entity_type.bundle.info')->clearCachedBundles();