class ContentModerationConfigureEntityTypesForm

Same name in other branches
  1. 8.9.x core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php \Drupal\content_moderation\Form\ContentModerationConfigureEntityTypesForm
  2. 10 core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php \Drupal\content_moderation\Form\ContentModerationConfigureEntityTypesForm
  3. 11.x core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php \Drupal\content_moderation\Form\ContentModerationConfigureEntityTypesForm

The form for editing entity types associated with a workflow.

@internal

Hierarchy

Expanded class hierarchy of ContentModerationConfigureEntityTypesForm

1 string reference to 'ContentModerationConfigureEntityTypesForm'
content_moderation.routing.yml in core/modules/content_moderation/content_moderation.routing.yml
core/modules/content_moderation/content_moderation.routing.yml

File

core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php, line 25

Namespace

Drupal\content_moderation\Form
View source
class ContentModerationConfigureEntityTypesForm extends FormBase {
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity type bundle information service.
     *
     * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
     */
    protected $bundleInfo;
    
    /**
     * The moderation information service.
     *
     * @var \Drupal\content_moderation\ModerationInformationInterface
     */
    protected $moderationInformation;
    
    /**
     * The workflow entity object.
     *
     * @var \Drupal\workflows\WorkflowInterface
     */
    protected $workflow;
    
    /**
     * The entity type definition object.
     *
     * @var \Drupal\Core\Entity\EntityTypeInterface
     */
    protected $entityType;
    
    /**
     * The Messenger service.
     *
     * @var \Drupal\Core\Messenger\MessengerInterface
     */
    protected $messenger;
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager'), $container->get('entity_type.bundle.info'), $container->get('content_moderation.moderation_information'), $container->get('messenger'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info, ModerationInformationInterface $moderation_information, MessengerInterface $messenger) {
        $this->entityTypeManager = $entity_type_manager;
        $this->bundleInfo = $bundle_info;
        $this->moderationInformation = $moderation_information;
        $this->messenger = $messenger;
    }
    
    /**
     * {@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] = [
                    'title' => [
                        'data' => [
                            '#title' => $bundle['label'],
                        ],
                    ],
                    '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',
                    ],
                ],
            ];
        }
        // Get unsupported features for this entity type.
        $warnings = $this->moderationInformation
            ->getUnsupportedFeatures($this->entityType);
        // Display message into the Ajax form returned.
        if ($this->getRequest()
            ->get(MainContentViewSubscriber::WRAPPER_FORMAT) == 'drupal_modal' && !empty($warnings)) {
            $form['warnings'] = [
                '#type' => 'status_messages',
                '#weight' => -1,
            ];
        }
        // Set warning message.
        foreach ($warnings as $warning) {
            $this->messenger
                ->addWarning($warning);
        }
        $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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ContentModerationConfigureEntityTypesForm::$bundleInfo protected property The entity type bundle information service.
ContentModerationConfigureEntityTypesForm::$entityType protected property The entity type definition object.
ContentModerationConfigureEntityTypesForm::$entityTypeManager protected property The entity type manager service.
ContentModerationConfigureEntityTypesForm::$messenger protected property The Messenger service. Overrides MessengerTrait::$messenger
ContentModerationConfigureEntityTypesForm::$moderationInformation protected property The moderation information service.
ContentModerationConfigureEntityTypesForm::$workflow protected property The workflow entity object.
ContentModerationConfigureEntityTypesForm::ajaxCallback public function Ajax callback to close the modal and update the selected text.
ContentModerationConfigureEntityTypesForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ContentModerationConfigureEntityTypesForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ContentModerationConfigureEntityTypesForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ContentModerationConfigureEntityTypesForm::getTitle public function Route title callback.
ContentModerationConfigureEntityTypesForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ContentModerationConfigureEntityTypesForm::__construct public function
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 73
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::messenger public function Gets the messenger. 17
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.