You are here

public function BundleModerationConfigurationForm::form in Workbench Moderation 8.2

Same name and namespace in other branches
  1. 8 src/Form/BundleModerationConfigurationForm.php \Drupal\workbench_moderation\Form\BundleModerationConfigurationForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/Form/BundleModerationConfigurationForm.php, line 52

Class

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

Namespace

Drupal\workbench_moderation\Form

Code

public function form(array $form, FormStateInterface $form_state) {

  /* @var 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('workbench_moderation', 'enabled', FALSE),
  ];

  // Add a special message when moderation is being disabled.
  if ($bundle
    ->getThirdPartySetting('workbench_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('workbench_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('workbench_moderation', 'allowed_moderation_states', array_keys($options_published)),
    '#options' => $options_published,
    '#required' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name=enable_moderation_state]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // This is screwy, but 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 chokes 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('workbench_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);
}