X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=inline;f=web%2Fcore%2Fmodules%2Fcontent_moderation%2Fsrc%2FForm%2FContentModerationConfigureEntityTypesForm.php;fp=web%2Fcore%2Fmodules%2Fcontent_moderation%2Fsrc%2FForm%2FContentModerationConfigureEntityTypesForm.php;h=99847df173ef3f7d6f3bb14d53060076f0729b88;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php b/web/core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php new file mode 100644 index 000000000..99847df17 --- /dev/null +++ b/web/core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php @@ -0,0 +1,207 @@ +get('entity_type.manager'), + $container->get('entity_type.bundle.info'), + $container->get('content_moderation.moderation_information') + ); + } + + /** + * {@inheritdoc} + */ + public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info, ModerationInformationInterface $moderation_information) { + $this->entityTypeManager = $entity_type_manager; + $this->bundleInfo = $bundle_info; + $this->moderationInformation = $moderation_information; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'workflow_type_edit_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $entity_type_id = NULL) { + $this->workflow = $workflow; + try { + $this->entityType = $this->entityTypeManager->getDefinition($entity_type_id); + } + catch (PluginNotFoundException $e) { + throw new NotFoundHttpException(); + } + + $options = $defaults = []; + foreach ($this->bundleInfo->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle) { + // Check if moderation is enabled for this bundle on any workflow. + $moderation_enabled = $this->moderationInformation->shouldModerateEntitiesOfBundle($this->entityType, $bundle_id); + // Check if moderation is enabled for this bundle on this workflow. + $workflow_moderation_enabled = $this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id(), $bundle_id); + // Only show bundles that are not enabled anywhere, or enabled on this + // workflow. + if (!$moderation_enabled || $workflow_moderation_enabled) { + // Add the bundle to the options if it's not enabled on a workflow, + // unless the workflow it's enabled on is this one. + $options[$bundle_id] = [ + 'type' => $bundle['label'], + ]; + // Add the bundle to the list of default values if it's enabled on this + // workflow. + $defaults[$bundle_id] = $workflow_moderation_enabled; + } + } + + if (!empty($options)) { + $bundles_header = $this->t('All @entity_type types', ['@entity_type' => $this->entityType->getLabel()]); + if ($bundle_entity_type_id = $this->entityType->getBundleEntityType()) { + $bundles_header = $this->t('All @entity_type_plural_label', ['@entity_type_plural_label' => $this->entityTypeManager->getDefinition($bundle_entity_type_id)->getPluralLabel()]); + } + $form['bundles'] = [ + '#type' => 'tableselect', + '#header' => [ + 'type' => $bundles_header, + ], + '#options' => $options, + '#default_value' => $defaults, + '#attributes' => ['class' => ['no-highlight']], + ]; + } + + $form['actions'] = ['#type' => 'actions']; + $form['actions']['submit'] = [ + '#type' => 'submit', + '#button_type' => 'primary', + '#value' => $this->t('Save'), + '#ajax' => [ + 'callback' => [$this, 'ajaxcallback'], + ], + ]; + $form['actions']['cancel'] = [ + '#type' => 'button', + '#value' => $this->t('Cancel'), + '#ajax' => [ + 'callback' => [$this, 'ajaxcallback'], + ], + ]; + + return $form; + } + + /** + * {@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 to close the modal and update the selected text. + * + * @return \Drupal\Core\Ajax\AjaxResponse + * An ajax response object. + */ + public function ajaxCallback() { + $selected_bundles = []; + foreach ($this->bundleInfo->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle) { + if ($this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id(), $bundle_id)) { + $selected_bundles[$bundle_id] = $bundle['label']; + } + } + $selected_bundles_list = [ + '#theme' => 'item_list', + '#items' => $selected_bundles, + '#context' => ['list_style' => 'comma-list'], + '#empty' => $this->t('none'), + ]; + $response = new AjaxResponse(); + $response->addCommand(new CloseDialogCommand()); + $response->addCommand(new HtmlCommand('#selected-' . $this->entityType->id(), $selected_bundles_list)); + return $response; + } + + /** + * Route title callback. + */ + public function getTitle(WorkflowInterface $workflow = NULL, $entity_type_id) { + $this->entityType = $this->entityTypeManager->getDefinition($entity_type_id); + + $title = $this->t('Select the @entity_type types for the @workflow workflow', ['@entity_type' => $this->entityType->getLabel(), '@workflow' => $workflow->label()]); + if ($bundle_entity_type_id = $this->entityType->getBundleEntityType()) { + $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()]); + } + + return $title; + } + +}