diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index e61f25e..71213ee 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -2,14 +2,18 @@ namespace Drupal\content_moderation\Plugin\WorkflowType; +use Drupal\content_moderation\ModerationInformationInterface; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\content_moderation\ContentModerationState; use Drupal\workflows\Plugin\WorkflowTypeBase; use Drupal\workflows\StateInterface; use Drupal\workflows\WorkflowInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Attaches workflows to content entity types and their bundles. @@ -19,11 +23,58 @@ * label = @Translation("Content moderation"), * ) */ -class ContentModeration extends WorkflowTypeBase { +class ContentModeration extends WorkflowTypeBase implements ContainerFactoryPluginInterface { use StringTranslationTrait; /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * The moderation information service. + * + * @var \Drupal\content_moderation\ModerationInformationInterface + */ + protected $moderationInfo; + + /** + * Constructs an ContentModeration object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info + * Moderation information service. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_info) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityTypeManager = $entity_type_manager; + $this->moderationInfo = $moderation_info; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('content_moderation.moderation_information') + ); + } + + /** * {@inheritdoc} */ public function checkWorkflowAccess(WorkflowInterface $entity, $operation, AccountInterface $account) { @@ -145,7 +196,7 @@ public function addEntityTypeAndBundle($entity_type_id, $bundle_id) { } /** - * {@inheritDoc} + * {@inheritdoc} */ public function defaultConfiguration() { // This plugin does not store anything per transition. @@ -156,11 +207,57 @@ public function defaultConfiguration() { } /** - * @inheritDoc + * {@inheritdoc} */ public function calculateDependencies() { // @todo : Implement calculateDependencies() method. return []; } + public function editForm(WorkflowInterface $workflow) { + $form['attachments'] = [ + '#type' => 'details', + '#title' => $this->t('Select types to use this workflow'), + '#open' => TRUE, + '#tree' => TRUE, + ]; + + $entity_types = $this->entityTypeManager->getDefinitions(); + foreach ($entity_types as $entity_type) { + if ($this->moderationInfo->canModerateEntitiesOfEntityType($entity_type)) { + $form['attachments'][$entity_type->id()] = [ + '#type' => 'checkboxes', + '#title' => $entity_type->getLabel()->render(), + '#options' => [], + ]; + $defaults = []; + $bundles = $this->entityTypeManager->getStorage($entity_type->getBundleEntityType())->loadMultiple(); + foreach ($bundles as $bundle) { + $form['attachments'][$entity_type->id()]['#options'][$bundle->id()] = $bundle->label(); + $defaults[$bundle->id()] = $this->appliesToEntityTypeAndBundle($entity_type->id(), $bundle->id()); + } + $form['attachments'][$entity_type->id()]['#default_value'] = array_keys(array_filter($defaults)); + } + } + + return $form; + } + + /** + * {@inheritdoc} + */ + public function editFormSave(WorkflowInterface &$workflow, array $form, FormStateInterface $formState) { + $type_plugin = $workflow->getTypePlugin(); + foreach ($formState->getValues()['attachments'] as $entity_type_id => $bundle_ids) { + foreach ($bundle_ids as $bundle_id => $checked) { + if ($checked) { + $type_plugin->addEntityTypeAndBundle($entity_type_id, $bundle_id); + } + else { + $type_plugin->removeEntityTypeAndBundle($entity_type_id, $bundle_id); + } + } + } + } + } diff --git a/core/modules/workflows/src/Form/WorkflowEditForm.php b/core/modules/workflows/src/Form/WorkflowEditForm.php index d8f99f9..8d2e398 100644 --- a/core/modules/workflows/src/Form/WorkflowEditForm.php +++ b/core/modules/workflows/src/Form/WorkflowEditForm.php @@ -179,6 +179,8 @@ public function form(array $form, FormStateInterface $form_state) { '#markup' => $workflow->toLink($this->t('Add a new transition'), 'add-transition-form')->toString(), ]; + $form += $workflow->getTypePlugin()->editForm($workflow); + return $form; } @@ -188,6 +190,7 @@ public function form(array $form, FormStateInterface $form_state) { public function save(array $form, FormStateInterface $form_state) { /* @var \Drupal\workflows\WorkflowInterface $workflow */ $workflow = $this->entity; + $workflow->getTypePlugin()->editFormSave($workflow, $form, $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/WorkflowTypeBase.php b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php index 1ed9fca..7f9462f 100644 --- a/core/modules/workflows/src/Plugin/WorkflowTypeBase.php +++ b/core/modules/workflows/src/Plugin/WorkflowTypeBase.php @@ -49,28 +49,28 @@ public function checkWorkflowAccess(WorkflowInterface $entity, $operation, Accou } /** - * {@inheritDoc} + * {@inheritdoc} */ public function decorateState(StateInterface $state) { return $state; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function deleteState($state_id) { unset($this->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,10 +118,24 @@ public function defaultConfiguration() { } /** - * {@inheritDoc} + * {@inheritdoc} */ public function calculateDependencies() { return []; } + /** + * {@inheritdoc} + */ + public function editForm(WorkflowInterface $workflow) { + return []; + } + + /** + * {@inheritdoc} + */ + public function editFormSave(WorkflowInterface &$workflow, array $form, FormStateInterface $form_state) { + + } + } diff --git a/core/modules/workflows/src/WorkflowTypeInterface.php b/core/modules/workflows/src/WorkflowTypeInterface.php index 17fddec..01c4b92 100644 --- a/core/modules/workflows/src/WorkflowTypeInterface.php +++ b/core/modules/workflows/src/WorkflowTypeInterface.php @@ -117,4 +117,29 @@ public function buildStateConfigurationForm(FormStateInterface $form_state, Work */ public function buildTransitionConfigurationForm(FormStateInterface $form_state, WorkflowInterface $workflow, TransitionInterface $transition = NULL); + /** + * Returns a form array to append to the Workflow entity edit form. + * + * @param \Drupal\workflows\WorkflowInterface $workflow + * THe workflow the form will be added to. + * + * @return array + * A form array to be used for type specific workflow settings. + * + * @see \Drupal\workflows\Form\WorkflowTransitionEditForm::form() + */ + public function editForm(WorkflowInterface $workflow); + + /** + * Save workflow type specific settings. + * + * @param \Drupal\workflows\WorkflowInterface $workflow + * The workflow the submitting form was added to. + * @param array $form + * The form array that was submitted. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state for the submitted form. + */ + public function editFormSave(WorkflowInterface &$workflow, array $form, FormStateInterface $form_state); + }