function ContentModerationConfigureEntityTypesForm::buildForm

Same name and namespace in other branches
  1. 8.9.x core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php \Drupal\content_moderation\Form\ContentModerationConfigureEntityTypesForm::buildForm()
  2. 10 core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php \Drupal\content_moderation\Form\ContentModerationConfigureEntityTypesForm::buildForm()
  3. 11.x core/modules/content_moderation/src/Form/ContentModerationConfigureEntityTypesForm.php \Drupal\content_moderation\Form\ContentModerationConfigureEntityTypesForm::buildForm()

Overrides FormInterface::buildForm

File

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

Class

ContentModerationConfigureEntityTypesForm
The form for editing entity types associated with a workflow.

Namespace

Drupal\content_moderation\Form

Code

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;
}

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