You are here

protected function UnitForm::actions in Booking and Availability Management Tools for Drupal 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/bat_unit/src/Entity/Form/UnitForm.php, line 161
Contains \Drupal\bat_unit\Entity\Form\UnitForm.

Class

UnitForm
Form controller for Unit edit forms.

Namespace

Drupal\bat_unit\Entity\Form

Code

protected function actions(array $form, FormStateInterface $form_state) {
  $element = parent::actions($form, $form_state);
  $entity = $this->entity;

  // 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 ($entity
    ->isNew()) {
    $element['publish']['#value'] = t('Save and publish');
  }
  else {
    $element['publish']['#value'] = $entity
      ->getStatus() ? 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 ($entity
    ->isNew()) {
    $element['unpublish']['#value'] = t('Save as unpublished');
  }
  else {
    $element['unpublish']['#value'] = !$entity
      ->getStatus() ? t('Save and keep unpublished') : t('Save and unpublish');
  }
  $element['unpublish']['#weight'] = 10;

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

  // Remove the "Save" button.
  $element['submit']['#access'] = FALSE;
  return $element;
}