Version 1
[yaffs-website] / web / core / modules / content_moderation / src / Form / BundleModerationConfigurationForm.php
1 <?php
2
3 namespace Drupal\content_moderation\Form;
4
5 use Drupal\content_moderation\Plugin\WorkflowType\ContentModeration;
6 use Drupal\workflows\WorkflowInterface;
7 use Drupal\Core\Entity\EntityForm;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Form for configuring moderation usage on a given entity bundle.
14  */
15 class BundleModerationConfigurationForm extends EntityForm {
16
17   /**
18    * Entity Type Manager service.
19    *
20    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21    */
22   protected $entityTypeManager;
23
24   /**
25    * {@inheritdoc}
26    */
27   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
28     $this->entityTypeManager = $entity_type_manager;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public static function create(ContainerInterface $container) {
35     return new static($container->get('entity_type.manager'));
36   }
37
38   /**
39    * {@inheritdoc}
40    *
41    * Blank out the base form ID so that form alters that use the base form ID to
42    * target both add and edit forms don't pick up this form.
43    */
44   public function getBaseFormId() {
45     return NULL;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function form(array $form, FormStateInterface $form_state) {
52     /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle */
53     $bundle = $this->getEntity();
54     $bundle_of_entity_type = $this->entityTypeManager->getDefinition($bundle->getEntityType()->getBundleOf());
55     /* @var \Drupal\workflows\WorkflowInterface[] $workflows */
56     $workflows = $this->entityTypeManager->getStorage('workflow')->loadMultiple();
57
58     $options = array_map(function (WorkflowInterface $workflow) {
59       return $workflow->label();
60     }, array_filter($workflows, function (WorkflowInterface $workflow) {
61       return $workflow->status() && $workflow->getTypePlugin() instanceof ContentModeration;
62     }));
63
64     $selected_workflow = array_reduce($workflows, function ($carry, WorkflowInterface $workflow) use ($bundle_of_entity_type, $bundle) {
65       $plugin = $workflow->getTypePlugin();
66       if ($plugin instanceof ContentModeration && $plugin->appliesToEntityTypeAndBundle($bundle_of_entity_type->id(), $bundle->id())) {
67         return $workflow->id();
68       }
69       return $carry;
70     });
71     $form['workflow'] = [
72       '#type' => 'select',
73       '#title' => $this->t('Select the workflow to apply'),
74       '#default_value' => $selected_workflow,
75       '#options' => $options,
76       '#required' => FALSE,
77       '#empty_value' => '',
78     ];
79
80     $form['original_workflow'] = [
81       '#type' => 'value',
82       '#value' => $selected_workflow,
83     ];
84
85     $form['bundle'] = [
86       '#type' => 'value',
87       '#value' => $bundle->id(),
88     ];
89
90     $form['entity_type'] = [
91       '#type' => 'value',
92       '#value' => $bundle_of_entity_type->id(),
93     ];
94
95     // Add a special message when moderation is being disabled.
96     if ($selected_workflow) {
97       $form['enable_workflow_note'] = [
98         '#type' => 'item',
99         '#description' => $this->t('After disabling moderation, any existing forward drafts will be accessible via the "Revisions" tab.'),
100         '#access' => !empty($selected_workflow)
101       ];
102     }
103
104     return parent::form($form, $form_state);
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function submitForm(array &$form, FormStateInterface $form_state) {
111     // If moderation is enabled, revisions MUST be enabled as well. Otherwise we
112     // can't have forward revisions.
113     drupal_set_message($this->t('Your settings have been saved.'));
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function save(array $form, FormStateInterface $form_state) {
120     $entity_type_id = $form_state->getValue('entity_type');
121     $bundle_id = $form_state->getValue('bundle');
122     $new_workflow_id = $form_state->getValue('workflow');
123     $original_workflow_id = $form_state->getValue('original_workflow');
124     if ($new_workflow_id === $original_workflow_id) {
125       // Nothing to do.
126       return;
127     }
128     if ($original_workflow_id) {
129       /* @var \Drupal\workflows\WorkflowInterface $workflow */
130       $workflow = $this->entityTypeManager->getStorage('workflow')->load($original_workflow_id);
131       $workflow->getTypePlugin()->removeEntityTypeAndBundle($entity_type_id, $bundle_id);
132       $workflow->save();
133     }
134     if ($new_workflow_id) {
135       /* @var \Drupal\workflows\WorkflowInterface $workflow */
136       $workflow = $this->entityTypeManager->getStorage('workflow')->load($new_workflow_id);
137       $workflow->getTypePlugin()->addEntityTypeAndBundle($entity_type_id, $bundle_id);
138       $workflow->save();
139     }
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   protected function actions(array $form, FormStateInterface $form_state) {
146     $actions['submit'] = [
147       '#type' => 'submit',
148       '#value' => $this->t('Save'),
149       '#submit' => ['::submitForm', '::save'],
150     ];
151
152     return $actions;
153   }
154
155 }