You are here

public function ContentModerationConfigureForm::buildConfigurationForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/src/Form/ContentModerationConfigureForm.php \Drupal\content_moderation\Form\ContentModerationConfigureForm::buildConfigurationForm()
  2. 10 core/modules/content_moderation/src/Form/ContentModerationConfigureForm.php \Drupal\content_moderation\Form\ContentModerationConfigureForm::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

core/modules/content_moderation/src/Form/ContentModerationConfigureForm.php, line 67

Class

ContentModerationConfigureForm
The content moderation WorkflowType configuration form.

Namespace

Drupal\content_moderation\Form

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $workflow = $form_state
    ->getFormObject()
    ->getEntity();
  $header = [
    'type' => $this
      ->t('Items'),
    'operations' => $this
      ->t('Operations'),
  ];
  $form['entity_types_container'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('This workflow applies to:'),
    '#open' => TRUE,
  ];
  $form['entity_types_container']['entity_types'] = [
    '#type' => 'table',
    '#header' => $header,
    '#empty' => $this
      ->t('There are no entity types.'),
  ];
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  foreach ($entity_types as $entity_type) {
    if (!$this->moderationInfo
      ->canModerateEntitiesOfEntityType($entity_type)) {
      continue;
    }
    $selected_bundles = [];
    foreach ($this->entityTypeBundleInfo
      ->getBundleInfo($entity_type
      ->id()) as $bundle_id => $bundle) {
      if ($this->workflowType
        ->appliesToEntityTypeAndBundle($entity_type
        ->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'),
    ];
    $form['entity_types_container']['entity_types'][$entity_type
      ->id()] = [
      'type' => [
        '#type' => 'inline_template',
        '#template' => '<strong>{{ label }}</strong></br><span id="selected-{{ entity_type_id }}">{{ selected_bundles }}</span>',
        '#context' => [
          'label' => $this
            ->t('@bundle types', [
            '@bundle' => $entity_type
              ->getLabel(),
          ]),
          'entity_type_id' => $entity_type
            ->id(),
          'selected_bundles' => $selected_bundles_list,
        ],
      ],
      'operations' => [
        '#type' => 'operations',
        '#links' => [
          'select' => [
            'title' => $this
              ->t('Select'),
            'url' => Url::fromRoute('content_moderation.workflow_type_edit_form', [
              'workflow' => $workflow
                ->id(),
              'entity_type_id' => $entity_type
                ->id(),
            ]),
            'attributes' => [
              'class' => [
                'use-ajax',
              ],
              'data-dialog-type' => 'modal',
              'data-dialog-options' => Json::encode([
                'width' => 700,
              ]),
            ],
          ],
        ],
      ],
    ];
  }
  $workflow_type_configuration = $this->workflowType
    ->getConfiguration();
  $form['workflow_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Workflow Settings'),
    '#open' => TRUE,
  ];
  $form['workflow_settings']['default_moderation_state'] = [
    '#title' => $this
      ->t('Default moderation state'),
    '#type' => 'select',
    '#required' => TRUE,
    '#options' => array_map([
      State::class,
      'labelCallback',
    ], $this->workflowType
      ->getStates()),
    '#description' => $this
      ->t('Select the state that new content will be assigned. This state will appear as the default in content forms and the available target states will be based on the transitions available from this state.'),
    '#default_value' => isset($workflow_type_configuration['default_moderation_state']) ? $workflow_type_configuration['default_moderation_state'] : 'draft',
  ];
  return $form;
}