public function NodeFormController::form

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

Overrides EntityFormController::form

File

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

Class

NodeFormController
Form controller for the node edit forms.

Namespace

Drupal\node

Code

public function form(array $form, array &$form_state) {
  $node = $this->entity;
  if ($this->operation == 'edit') {
    drupal_set_title(t('<em>Edit @type</em> @title', array(
      '@type' => node_get_type_label($node),
      '@title' => $node
        ->label(),
    )), PASS_THROUGH);
  }
  $user_config = config('user.settings');

  // Some special stuff when previewing a node.
  if (isset($form_state['node_preview'])) {
    $form['#prefix'] = $form_state['node_preview'];
    $node->in_preview = TRUE;
  }
  else {
    unset($node->in_preview);
  }

  // Override the default CSS class name, since the user-defined node type
  // name in 'TYPE-node-form' potentially clashes with third-party class
  // names.
  $form['#attributes']['class'][0] = drupal_html_class('node-' . $node->type . '-form');

  // Basic node information.
  // These elements are just values so they are not even sent to the client.
  foreach (array(
    'nid',
    'vid',
    'uid',
    'created',
    'type',
  ) as $key) {
    $form[$key] = array(
      '#type' => 'value',
      '#value' => isset($node->{$key}) ? $node->{$key} : NULL,
    );
  }

  // Changed must be sent to the client, for later overwrite error checking.
  $form['changed'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($node->changed) ? $node->changed : NULL,
  );

  // Invoke hook_form() to get the node-specific bits. Can't use node_invoke()
  // because hook_form() needs to be able to receive $form_state by reference.
  // @todo hook_form() implementations are unable to add #validate or #submit
  //   handlers to the form buttons below. Remove hook_form() entirely.
  $function = node_hook($node->type, 'form');
  if ($function && ($extra = $function($node, $form_state))) {
    $form = NestedArray::mergeDeep($form, $extra);
  }

  // If the node type has a title, and the node type form defined no special
  // weight for it, we default to a weight of -5 for consistency.
  if (isset($form['title']) && !isset($form['title']['#weight'])) {
    $form['title']['#weight'] = -5;
  }
  $language_configuration = module_invoke('language', 'get_default_configuration', 'node', $node->type);
  $form['langcode'] = array(
    '#title' => t('Language'),
    '#type' => 'language_select',
    '#default_value' => $node->langcode,
    '#languages' => Language::STATE_ALL,
    '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'],
  );
  $form['advanced'] = array(
    '#type' => 'vertical_tabs',
    '#attributes' => array(
      'class' => array(
        'entity-meta',
      ),
    ),
    '#weight' => 99,
  );

  // Add a log field if the "Create new revision" option is checked, or if
  // the current user has the ability to check that option.
  $form['revision_information'] = array(
    '#type' => 'details',
    '#group' => 'advanced',
    '#title' => t('Revision information'),
    // Collapsed by default when "Create new revision" is unchecked.
    '#collapsed' => !$node
      ->isNewRevision(),
    '#attributes' => array(
      'class' => array(
        'node-form-revision-information',
      ),
    ),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'node') . '/node.js',
      ),
    ),
    '#weight' => 20,
    '#access' => $node
      ->isNewRevision() || user_access('administer nodes'),
  );
  $form['revision_information']['revision']['revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => $node
      ->isNewRevision(),
    '#access' => user_access('administer nodes'),
  );
  $form['revision_information']['revision']['log'] = array(
    '#type' => 'textarea',
    '#title' => t('Revision log message'),
    '#rows' => 4,
    '#default_value' => !empty($node->log) ? $node->log : '',
    '#description' => t('Briefly describe the changes you have made.'),
    '#states' => array(
      'visible' => array(
        ':input[name="revision"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Node author information for administrators.
  $form['author'] = array(
    '#type' => 'details',
    '#access' => user_access('administer nodes'),
    '#title' => t('Authoring information'),
    '#collapsed' => TRUE,
    '#group' => 'advanced',
    '#attributes' => array(
      'class' => array(
        'node-form-author',
      ),
    ),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'node') . '/node.js',
        array(
          'type' => 'setting',
          'data' => array(
            'anonymous' => $user_config
              ->get('anonymous'),
          ),
        ),
      ),
    ),
    '#weight' => 90,
  );
  $form['author']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Authored by'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => !empty($node->name) ? $node->name : '',
    '#weight' => -1,
    '#description' => t('Leave blank for %anonymous.', array(
      '%anonymous' => $user_config
        ->get('anonymous'),
    )),
  );
  $form['author']['date'] = array(
    '#type' => 'textfield',
    '#title' => t('Authored on'),
    '#maxlength' => 25,
    '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array(
      '%time' => !empty($node->date) ? date_format(date_create($node->date), 'Y-m-d H:i:s O') : format_date($node->created, 'custom', 'Y-m-d H:i:s O'),
      '%timezone' => !empty($node->date) ? date_format(date_create($node->date), 'O') : format_date($node->created, 'custom', 'O'),
    )),
    '#default_value' => !empty($node->date) ? $node->date : '',
  );

  // Node options for administrators.
  $form['options'] = array(
    '#type' => 'details',
    '#access' => user_access('administer nodes'),
    '#title' => t('Promotion options'),
    '#collapsed' => TRUE,
    '#group' => 'advanced',
    '#attributes' => array(
      'class' => array(
        'node-form-options',
      ),
    ),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'node') . '/node.js',
      ),
    ),
    '#weight' => 95,
  );
  $form['options']['promote'] = array(
    '#type' => 'checkbox',
    '#title' => t('Promoted to front page'),
    '#default_value' => $node->promote,
  );
  $form['options']['sticky'] = array(
    '#type' => 'checkbox',
    '#title' => t('Sticky at top of lists'),
    '#default_value' => $node->sticky,
  );

  // This form uses a button-level #submit handler for the form's main submit
  // action. node_form_submit() manually invokes all form-level #submit
  // handlers of the form. Without explicitly setting #submit, Form API would
  // auto-detect node_form_submit() as submit handler, but that is the
  // button-level #submit handler for the 'Save' action.
  $form += array(
    '#submit' => array(),
  );
  return parent::form($form, $form_state, $node);
}