You are here

protected function SupportTicketForm::actions in Support Ticketing System 8

Returns an array of supported actions for the current entity form.

This function generates a list of Form API elements which represent actions supported by the current entity form.

@todo Consider introducing a 'preview' action here, since it is used by many entity types.

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 An array of supported Form API action elements keyed by name.

Overrides EntityForm::actions

File

modules/support_ticket/src/SupportTicketForm.php, line 231
Contains \Drupal\support_ticket\SupportTicketForm.

Class

SupportTicketForm
Form controller for the support ticket edit forms.

Namespace

Drupal\support_ticket

Code

protected function actions(array $form, FormStateInterface $form_state) {
  $element = parent::actions($form, $form_state);
  $support_ticket = $this->entity;
  $preview_mode = $support_ticket->support_ticket_type->entity
    ->getPreviewMode();
  $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $this->hasBeenPreviewed;

  // If saving is an option, privileged users get dedicated form submit
  // buttons to adjust the publishing status while saving in one go.
  // @todo This adjustment makes it close to impossible for contributed
  //   modules to integrate with "the Save operation" of this form. Modules
  //   need a way to plug themselves into 1) the ::submit() step, and
  //   2) the ::save() step, both decoupled from the pressed form button.
  if ($element['submit']['#access'] && \Drupal::currentUser()
    ->hasPermission('administer support tickets')) {

    // isNew | prev status » default   & publish label             & unpublish label
    // 1     | 1           » publish   & Save and publish          & Save as unpublished
    // 1     | 0           » unpublish & Save and publish          & Save as unpublished
    // 0     | 1           » publish   & Save and keep published   & Save and unpublish
    // 0     | 0           » unpublish & Save and keep unpublished & Save and publish
    // Add a "Publish" button.
    $element['publish'] = $element['submit'];

    // If the "Publish" button is clicked, we want to update the status to "published".
    $element['publish']['#published_status'] = TRUE;
    $element['publish']['#dropbutton'] = 'save';
    if ($support_ticket
      ->isNew()) {
      $element['publish']['#value'] = t('Save and publish');
    }
    else {
      $element['publish']['#value'] = $support_ticket
        ->isPublished() ? t('Save and keep published') : t('Save and publish');
    }
    $element['publish']['#weight'] = 0;

    // Add a "Unpublish" button.
    $element['unpublish'] = $element['submit'];

    // If the "Unpublish" button is clicked, we want to update the status to "unpublished".
    $element['unpublish']['#published_status'] = FALSE;
    $element['unpublish']['#dropbutton'] = 'save';
    if ($support_ticket
      ->isNew()) {
      $element['unpublish']['#value'] = t('Save as unpublished');
    }
    else {
      $element['unpublish']['#value'] = !$support_ticket
        ->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
    }
    $element['unpublish']['#weight'] = 10;

    // If already published, the 'publish' button is primary.
    if ($support_ticket
      ->isPublished()) {
      unset($element['unpublish']['#button_type']);
    }
    else {
      unset($element['publish']['#button_type']);
      $element['unpublish']['#weight'] = -10;
    }

    // Remove the "Save" button.
    $element['submit']['#access'] = FALSE;
  }
  $element['preview'] = array(
    '#type' => 'submit',
    '#access' => $preview_mode != DRUPAL_DISABLED,
    '#value' => t('Preview'),
    '#weight' => 20,
    '#submit' => array(
      '::submitForm',
      '::preview',
    ),
  );
  $element['delete']['#weight'] = 100;
  return $element;
}