You are here

protected function NodeForm::actions in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/node/src/NodeForm.php \Drupal\node\NodeForm::actions()

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

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

Overrides EntityForm::actions

File

core/modules/node/src/NodeForm.php, line 237
Contains \Drupal\node\NodeForm.

Class

NodeForm
Form controller for the node edit forms.

Namespace

Drupal\node

Code

protected function actions(array $form, FormStateInterface $form_state) {
  $element = parent::actions($form, $form_state);
  $node = $this->entity;
  $preview_mode = $node->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 nodes')) {

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

    // If already published, the 'publish' button is primary.
    if ($node
      ->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 && ($node
      ->access('create') || $node
      ->access('update')),
    '#value' => t('Preview'),
    '#weight' => 20,
    '#submit' => array(
      '::submitForm',
      '::preview',
    ),
  );
  $element['delete']['#access'] = $node
    ->access('delete');
  $element['delete']['#weight'] = 100;
  return $element;
}