99847df173ef3f7d6f3bb14d53060076f0729b88
[yaffs-website] / web / core / modules / content_moderation / src / Form / ContentModerationConfigureEntityTypesForm.php
1 <?php
2
3 namespace Drupal\content_moderation\Form;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\content_moderation\ModerationInformationInterface;
7 use Drupal\Core\Ajax\AjaxResponse;
8 use Drupal\Core\Ajax\CloseDialogCommand;
9 use Drupal\Core\Ajax\HtmlCommand;
10 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
11 use Drupal\Core\Entity\EntityTypeManagerInterface;
12 use Drupal\Core\Form\FormBase;
13 use Drupal\Core\Form\FormStateInterface;
14 use Drupal\workflows\WorkflowInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17
18 /**
19  * The form for editing entity types associated with a workflow.
20  */
21 class ContentModerationConfigureEntityTypesForm extends FormBase {
22
23   /**
24    * The entity type manager service.
25    *
26    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27    */
28   protected $entityTypeManager;
29
30   /**
31    * The entity type bundle information service.
32    *
33    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
34    */
35   protected $bundleInfo;
36
37   /**
38    * The moderation information service.
39    *
40    * @var \Drupal\content_moderation\ModerationInformationInterface
41    */
42   protected $moderationInformation;
43
44   /**
45    * The workflow entity object.
46    *
47    * @var \Drupal\workflows\WorkflowInterface
48    */
49   protected $workflow;
50
51   /**
52    * The entity type definition object.
53    *
54    * @var \Drupal\Core\Entity\EntityTypeInterface
55    */
56   protected $entityType;
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function create(ContainerInterface $container) {
62     return new static(
63       $container->get('entity_type.manager'),
64       $container->get('entity_type.bundle.info'),
65       $container->get('content_moderation.moderation_information')
66     );
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info, ModerationInformationInterface $moderation_information) {
73     $this->entityTypeManager = $entity_type_manager;
74     $this->bundleInfo = $bundle_info;
75     $this->moderationInformation = $moderation_information;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function getFormId() {
82     return 'workflow_type_edit_form';
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $entity_type_id = NULL) {
89     $this->workflow = $workflow;
90     try {
91       $this->entityType = $this->entityTypeManager->getDefinition($entity_type_id);
92     }
93     catch (PluginNotFoundException $e) {
94       throw new NotFoundHttpException();
95     }
96
97     $options = $defaults = [];
98     foreach ($this->bundleInfo->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle) {
99       // Check if moderation is enabled for this bundle on any workflow.
100       $moderation_enabled = $this->moderationInformation->shouldModerateEntitiesOfBundle($this->entityType, $bundle_id);
101       // Check if moderation is enabled for this bundle on this workflow.
102       $workflow_moderation_enabled = $this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id(), $bundle_id);
103       // Only show bundles that are not enabled anywhere, or enabled on this
104       // workflow.
105       if (!$moderation_enabled || $workflow_moderation_enabled) {
106         // Add the bundle to the options if it's not enabled on a workflow,
107         // unless the workflow it's enabled on is this one.
108         $options[$bundle_id] = [
109           'type' => $bundle['label'],
110         ];
111         // Add the bundle to the list of default values if it's enabled on this
112         // workflow.
113         $defaults[$bundle_id] = $workflow_moderation_enabled;
114       }
115     }
116
117     if (!empty($options)) {
118       $bundles_header = $this->t('All @entity_type types', ['@entity_type' => $this->entityType->getLabel()]);
119       if ($bundle_entity_type_id = $this->entityType->getBundleEntityType()) {
120         $bundles_header = $this->t('All @entity_type_plural_label', ['@entity_type_plural_label' => $this->entityTypeManager->getDefinition($bundle_entity_type_id)->getPluralLabel()]);
121       }
122       $form['bundles'] = [
123         '#type' => 'tableselect',
124         '#header' => [
125           'type' => $bundles_header,
126         ],
127         '#options' => $options,
128         '#default_value' => $defaults,
129         '#attributes' => ['class' => ['no-highlight']],
130       ];
131     }
132
133     $form['actions'] = ['#type' => 'actions'];
134     $form['actions']['submit'] = [
135       '#type' => 'submit',
136       '#button_type' => 'primary',
137       '#value' => $this->t('Save'),
138       '#ajax' => [
139         'callback' => [$this, 'ajaxcallback'],
140       ],
141     ];
142     $form['actions']['cancel'] = [
143       '#type' => 'button',
144       '#value' => $this->t('Cancel'),
145       '#ajax' => [
146         'callback' => [$this, 'ajaxcallback'],
147       ],
148     ];
149
150     return $form;
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public function submitForm(array &$form, FormStateInterface $form_state) {
157     foreach ($form_state->getValue('bundles') as $bundle_id => $checked) {
158       if ($checked) {
159         $this->workflow->getTypePlugin()->addEntityTypeAndBundle($this->entityType->id(), $bundle_id);
160       }
161       else {
162         $this->workflow->getTypePlugin()->removeEntityTypeAndBundle($this->entityType->id(), $bundle_id);
163       }
164     }
165     $this->workflow->save();
166   }
167
168   /**
169    * Ajax callback to close the modal and update the selected text.
170    *
171    * @return \Drupal\Core\Ajax\AjaxResponse
172    *   An ajax response object.
173    */
174   public function ajaxCallback() {
175     $selected_bundles = [];
176     foreach ($this->bundleInfo->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle) {
177       if ($this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id(), $bundle_id)) {
178         $selected_bundles[$bundle_id] = $bundle['label'];
179       }
180     }
181     $selected_bundles_list = [
182       '#theme' => 'item_list',
183       '#items' => $selected_bundles,
184       '#context' => ['list_style' => 'comma-list'],
185       '#empty' => $this->t('none'),
186     ];
187     $response = new AjaxResponse();
188     $response->addCommand(new CloseDialogCommand());
189     $response->addCommand(new HtmlCommand('#selected-' . $this->entityType->id(), $selected_bundles_list));
190     return $response;
191   }
192
193   /**
194    * Route title callback.
195    */
196   public function getTitle(WorkflowInterface $workflow = NULL, $entity_type_id) {
197     $this->entityType = $this->entityTypeManager->getDefinition($entity_type_id);
198
199     $title = $this->t('Select the @entity_type types for the @workflow workflow', ['@entity_type' => $this->entityType->getLabel(), '@workflow' => $workflow->label()]);
200     if ($bundle_entity_type_id = $this->entityType->getBundleEntityType()) {
201       $title = $this->t('Select the @entity_type_plural_label for the @workflow workflow', ['@entity_type_plural_label' => $this->entityTypeManager->getDefinition($bundle_entity_type_id)->getPluralLabel(), '@workflow' => $workflow->label()]);
202     }
203
204     return $title;
205   }
206
207 }