protected function NodeFormController::actions

Overrides Drupal\Core\Entity\EntityFormController::actions().

Overrides EntityFormController::actions

File

drupal/core/modules/node/lib/Drupal/node/NodeFormController.php, line 243
Definition of Drupal\node\NodeFormController.

Class

NodeFormController
Form controller for the node edit forms.

Namespace

Drupal\node

Code

protected function actions(array $form, array &$form_state) {
  $element = parent::actions($form, $form_state);
  $node = $this->entity;
  $preview_mode = variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL);
  $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || !form_get_errors() && isset($form_state['node_preview']);

  // 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'] && user_access('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'];
    $element['publish']['#dropbutton'] = 'save';
    if ($node
      ->isNew()) {
      $element['publish']['#value'] = t('Save and publish');
    }
    else {
      $element['publish']['#value'] = $node->status ? t('Save and keep published') : t('Save and publish');
    }
    $element['publish']['#weight'] = 0;
    array_unshift($element['publish']['#submit'], array(
      $this,
      'publish',
    ));

    // Add a "Unpublish" button.
    $element['unpublish'] = $element['submit'];
    $element['unpublish']['#dropbutton'] = 'save';
    if ($node
      ->isNew()) {
      $element['unpublish']['#value'] = t('Save as unpublished');
    }
    else {
      $element['unpublish']['#value'] = !$node->status ? t('Save and keep unpublished') : t('Save and unpublish');
    }
    $element['unpublish']['#weight'] = 10;
    array_unshift($element['unpublish']['#submit'], array(
      $this,
      'unpublish',
    ));

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