3 namespace Drupal\content_moderation\Form;
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;
19 * The form for editing entity types associated with a workflow.
21 class ContentModerationConfigureEntityTypesForm extends FormBase {
24 * The entity type manager service.
26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
28 protected $entityTypeManager;
31 * The entity type bundle information service.
33 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
35 protected $bundleInfo;
38 * The moderation information service.
40 * @var \Drupal\content_moderation\ModerationInformationInterface
42 protected $moderationInformation;
45 * The workflow entity object.
47 * @var \Drupal\workflows\WorkflowInterface
52 * The entity type definition object.
54 * @var \Drupal\Core\Entity\EntityTypeInterface
56 protected $entityType;
61 public static function create(ContainerInterface $container) {
63 $container->get('entity_type.manager'),
64 $container->get('entity_type.bundle.info'),
65 $container->get('content_moderation.moderation_information')
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;
81 public function getFormId() {
82 return 'workflow_type_edit_form';
88 public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $entity_type_id = NULL) {
89 $this->workflow = $workflow;
91 $this->entityType = $this->entityTypeManager->getDefinition($entity_type_id);
93 catch (PluginNotFoundException $e) {
94 throw new NotFoundHttpException();
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
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'],
111 // Add the bundle to the list of default values if it's enabled on this
113 $defaults[$bundle_id] = $workflow_moderation_enabled;
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()]);
123 '#type' => 'tableselect',
125 'type' => $bundles_header,
127 '#options' => $options,
128 '#default_value' => $defaults,
129 '#attributes' => ['class' => ['no-highlight']],
133 $form['actions'] = ['#type' => 'actions'];
134 $form['actions']['submit'] = [
136 '#button_type' => 'primary',
137 '#value' => $this->t('Save'),
139 'callback' => [$this, 'ajaxcallback'],
142 $form['actions']['cancel'] = [
144 '#value' => $this->t('Cancel'),
146 'callback' => [$this, 'ajaxcallback'],
156 public function submitForm(array &$form, FormStateInterface $form_state) {
157 foreach ($form_state->getValue('bundles') as $bundle_id => $checked) {
159 $this->workflow->getTypePlugin()->addEntityTypeAndBundle($this->entityType->id(), $bundle_id);
162 $this->workflow->getTypePlugin()->removeEntityTypeAndBundle($this->entityType->id(), $bundle_id);
165 $this->workflow->save();
169 * Ajax callback to close the modal and update the selected text.
171 * @return \Drupal\Core\Ajax\AjaxResponse
172 * An ajax response object.
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'];
181 $selected_bundles_list = [
182 '#theme' => 'item_list',
183 '#items' => $selected_bundles,
184 '#context' => ['list_style' => 'comma-list'],
185 '#empty' => $this->t('none'),
187 $response = new AjaxResponse();
188 $response->addCommand(new CloseDialogCommand());
189 $response->addCommand(new HtmlCommand('#selected-' . $this->entityType->id(), $selected_bundles_list));
194 * Route title callback.
196 public function getTitle(WorkflowInterface $workflow = NULL, $entity_type_id) {
197 $this->entityType = $this->entityTypeManager->getDefinition($entity_type_id);
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()]);