You are here

public function EntityModerationForm::buildForm in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/Form/EntityModerationForm.php \Drupal\workbench_moderation\Form\EntityModerationForm::buildForm()

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/EntityModerationForm.php, line 78

Class

EntityModerationForm
Defines a form for entity moderation.

Namespace

Drupal\workbench_moderation\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL) {

  /** @var \Drupal\workbench_moderation\Entity\ModerationState $current_state */
  $current_state = $entity->moderation_state->entity;
  $transitions = $this->validation
    ->getValidTransitions($entity, $this
    ->currentUser());

  // Exclude self-transitions.
  $transitions = array_filter($transitions, function (ModerationStateTransition $transition) use ($current_state) {
    return $transition
      ->getToState() != $current_state
      ->id();
  });
  $target_states = [];

  /** @var \Drupal\workbench_moderation\Entity\ModerationStateTransition $transition */
  foreach ($transitions as $transition) {
    $target_states[$transition
      ->getToState()] = $transition
      ->label();
  }
  if (!count($target_states)) {
    return $form;
  }
  if ($current_state) {
    $form['current'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Status'),
      '#markup' => $current_state
        ->label(),
    ];
  }

  // Persist the entity so we can access it in the submit handler.
  $form_state
    ->set('entity', $entity);
  $form['new_state'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Moderate'),
    '#options' => $target_states,
  ];
  $form['revision_log'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Log message'),
    '#size' => 30,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Apply'),
  ];
  $form['#theme'] = [
    'entity_moderation_form',
  ];
  $form['#attached']['library'][] = 'workbench_moderation/entity-moderation-form';
  return $form;
}