You are here

protected function PathFileEntityForm::actions in Path File 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

src/Form/PathFileEntityForm.php, line 52

Class

PathFileEntityForm
Form controller for Path file entity edit forms.

Namespace

Drupal\path_file\Form

Code

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

  // 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 (\Drupal::currentUser()
    ->hasPermission('Administer Path file entity entities')) {

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

    // If already published, the 'publish' button is primary.
    if ($path_file
      ->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['delete']['#access'] = $path_file
    ->access('delete');
  $element['delete']['#weight'] = 100;
  return $element;
}