You are here

protected function MiconForm::actions in Micon 8

Same name and namespace in other branches
  1. 2.x src/Form/MiconForm.php \Drupal\micon\Form\MiconForm::actions()

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

src/Form/MiconForm.php, line 94

Class

MiconForm
Class MiconForm.

Namespace

Drupal\micon\Form

Code

protected function actions(array $form, FormStateInterface $form_state) {
  $element = parent::actions($form, $form_state);
  $micon = $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 ($micon
    ->isNew()) {
    $element['publish']['#value'] = $this
      ->t('Save and publish');
  }
  else {
    $element['publish']['#value'] = $micon
      ->status() ? $this
      ->t('Save and keep published') : $this
      ->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 ($micon
    ->isNew()) {
    $element['unpublish']['#value'] = $this
      ->t('Save as unpublished');
  }
  else {
    $element['unpublish']['#value'] = !$micon
      ->status() ? $this
      ->t('Save and keep unpublished') : $this
      ->t('Save and unpublish');
  }
  $element['unpublish']['#weight'] = 10;

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

  // Remove the "Save" button.
  $element['submit']['#access'] = FALSE;
  $element['delete']['#access'] = $micon
    ->access('delete');
  $element['delete']['#weight'] = 100;
  return $element;
}