You are here

public function QuickTransitionForm::buildForm in Moderation Sidebar 8

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/QuickTransitionForm.php, line 80

Class

QuickTransitionForm
The QuickTransitionForm provides quick buttons for changing transitions.

Namespace

Drupal\moderation_sidebar\Form

Code

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

  // Return an empty form if the user does not have appropriate permissions.
  if (!$entity
    ->access('update')) {
    return [];
  }

  // If this is not the default revision and is the latest translation
  // affected revision, then show a discard draft button.
  if (!$entity
    ->isDefaultRevision() && $entity
    ->isLatestTranslationAffectedRevision()) {
    $form['discard_draft'] = [
      '#type' => 'submit',
      '#id' => 'moderation-sidebar-discard-draft',
      '#value' => $this
        ->t('Discard draft'),
      '#attributes' => [
        'class' => [
          'moderation-sidebar-link',
          'button',
          'button--danger',
        ],
      ],
      '#submit' => [
        '::discardDraft',
      ],
    ];
  }

  // Persist the entity so we can access it in the submit handler.
  $form_state
    ->set('entity', $entity);
  $transitions = $this->validation
    ->getValidTransitions($entity, $this
    ->currentUser());
  $workflow = $this->moderationInformation
    ->getWorkFlowForEntity($entity);
  $disabled_transitions = $this
    ->configFactory()
    ->getEditable('moderation_sidebar.settings')
    ->get('workflows.' . $workflow
    ->id() . '_workflow.disabled_transitions');

  // Exclude self-transitions.

  /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $current_state */
  $current_state = $this
    ->getModerationState($entity);

  /** @var \Drupal\workflows\TransitionInterface[] $transitions */
  $transitions = array_filter($transitions, function ($transition) use ($current_state) {
    return $transition
      ->to()
      ->id() != $current_state
      ->id();
  });
  $is_transition_enabled = FALSE;
  foreach ($transitions as $transition) {

    // Exclude disabled transitions.
    if (empty($disabled_transitions[$transition
      ->id()])) {
      $form[$transition
        ->id()] = [
        '#type' => 'submit',
        '#id' => $transition
          ->id(),
        '#value' => $transition
          ->label(),
        '#attributes' => [
          'class' => [
            'moderation-sidebar-link',
            'button--primary',
          ],
        ],
      ];
      $is_transition_enabled = TRUE;
    }
  }

  // Show only, if at least one transition is enabled.
  if ($is_transition_enabled) {
    $form['revision_log_toggle'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use custom log message'),
      '#default_value' => FALSE,
      '#attributes' => [
        'class' => [
          'moderation-sidebar-revision-log-toggle',
        ],
      ],
    ];
    $form['revision_log'] = [
      '#type' => 'textarea',
      '#placeholder' => $this
        ->t('Briefly describe this state change.'),
      '#attributes' => [
        'class' => [
          'moderation-sidebar-revision-log',
        ],
      ],
      '#states' => [
        'visible' => [
          ':input[name="revision_log_toggle"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  return $form;
}