BundleModerationConfigurationForm::form

public BundleModerationConfigurationForm::form(array $form, FormStateInterface $form_state)

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/content_moderation/src/Form/BundleModerationConfigurationForm.php, line 52

Class

BundleModerationConfigurationForm
Form for configuring moderation usage on a given entity bundle.

Namespace

Drupal\content_moderation\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  /* @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $bundle */
  $bundle = $form_state->getFormObject()->getEntity();
  $form['enable_moderation_state'] = [
    '#type' => 'checkbox',
    '#title' => $this->t('Enable moderation states.'),
    '#description' => $this->t('Content of this type must transition through moderation states in order to be published.'),
    '#default_value' => $bundle->getThirdPartySetting('content_moderation', 'enabled', FALSE),
  ];

  // Add a special message when moderation is being disabled.
  if ($bundle->getThirdPartySetting('content_moderation', 'enabled', FALSE)) {
    $form['enable_moderation_state_note'] = [
      '#type' => 'item',
      '#description' => $this->t('After disabling moderation, any existing forward drafts will be accessible via the "Revisions" tab.'),
      '#states' => [
        'visible' => [
          ':input[name=enable_moderation_state]' => ['checked' => FALSE],
        ],
      ],
    ];
  }

  $states = $this->entityTypeManager->getStorage('moderation_state')->loadMultiple();
  $label = function(ModerationState $state) {
    return $state->label();
  };

  $options_published = array_map($label, array_filter($states, function(ModerationState $state) {
    return $state->isPublishedState();
  }));

  $options_unpublished = array_map($label, array_filter($states, function(ModerationState $state) {
    return !$state->isPublishedState();
  }));

  $form['allowed_moderation_states_unpublished'] = [
    '#type' => 'checkboxes',
    '#title' => $this->t('Allowed moderation states (Unpublished)'),
    '#description' => $this->t('The allowed unpublished moderation states this content-type can be assigned.'),
    '#default_value' => $bundle->getThirdPartySetting('content_moderation', 'allowed_moderation_states', array_keys($options_unpublished)),
    '#options' => $options_unpublished,
    '#required' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name=enable_moderation_state]' => ['checked' => TRUE],
      ],
    ],
  ];

  $form['allowed_moderation_states_published'] = [
    '#type' => 'checkboxes',
    '#title' => $this->t('Allowed moderation states (Published)'),
    '#description' => $this->t('The allowed published moderation states this content-type can be assigned.'),
    '#default_value' => $bundle->getThirdPartySetting('content_moderation', 'allowed_moderation_states', array_keys($options_published)),
    '#options' => $options_published,
    '#required' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name=enable_moderation_state]' => ['checked' => TRUE],
      ],
    ],
  ];

  // The key of the array needs to be a user-facing string so we have to fully
  // render the translatable string to a real string, or else PHP errors on an
  // object used as an array key.
  $options = [
    $this->t('Unpublished')->render() => $options_unpublished,
    $this->t('Published')->render() => $options_published,
  ];

  $form['default_moderation_state'] = [
    '#type' => 'select',
    '#title' => $this->t('Default moderation state'),
    '#options' => $options,
    '#description' => $this->t('Select the moderation state for new content'),
    '#default_value' => $bundle->getThirdPartySetting('content_moderation', 'default_moderation_state', 'draft'),
    '#states' => [
      'visible' => [
        ':input[name=enable_moderation_state]' => ['checked' => TRUE],
      ],
    ],
  ];
  $form['#entity_builders'][] = [$this, 'formBuilderCallback'];

  return parent::form($form, $form_state);
}
doc_Drupal
2016-10-29 08:49:05
Comments
Leave a Comment

Please login to continue.