You are here

public function EntityModerationForm::buildForm in Workbench Moderation 8.2

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

@inheritDoc

Overrides FormInterface::buildForm

File

src/Form/EntityModerationForm.php, line 60

Class

EntityModerationForm

Namespace

Drupal\workbench_moderation\Form

Code

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

  /** @var 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 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',
  ];
  return $form;
}