Same name and namespace in other branches
  1. 4.6.x modules/node.module \node_form()
  2. 4.7.x modules/node.module \node_form()
  3. 5.x modules/node/node.module \node_form()
  4. 7.x modules/node/node.pages.inc \node_form()

Generate the node add/edit form array.

1 string reference to 'node_form'
node_forms in modules/node/node.module
Implementation of hook_forms(). All node forms share the same form handler

File

modules/node/node.pages.inc, line 97
Page callbacks for adding, editing, deleting, and revisions management for content.

Code

function node_form(&$form_state, $node) {
  global $user;
  if (isset($form_state['node'])) {
    $node = $form_state['node'] + (array) $node;
  }
  if (isset($form_state['node_preview'])) {
    $form['#prefix'] = $form_state['node_preview'];
  }
  $node = (object) $node;
  foreach (array(
    'body',
    'title',
    'format',
  ) as $key) {
    if (!isset($node->{$key})) {
      $node->{$key} = NULL;
    }
  }
  if (!isset($form_state['node_preview'])) {
    node_object_prepare($node);
  }
  else {
    $node->build_mode = NODE_BUILD_PREVIEW;
  }

  // Set the id of the top-level form tag
  $form['#id'] = 'node-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',
    'language',
  ) 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,
  );

  // Get the node-specific bits.
  if ($extra = node_invoke($node, 'form', $form_state)) {
    $form = array_merge_recursive($form, $extra);
  }
  if (!isset($form['title']['#weight'])) {
    $form['title']['#weight'] = -5;
  }
  $form['#node'] = $node;

  // Add a log field if the "Create new revision" option is checked, or if the
  // current user has the ability to check that option.
  if (!empty($node->revision) || user_access('administer nodes')) {
    $form['revision_information'] = array(
      '#type' => 'fieldset',
      '#title' => t('Revision information'),
      '#collapsible' => TRUE,
      // Collapsed by default when "Create new revision" is unchecked
      '#collapsed' => !$node->revision,
      '#weight' => 20,
    );
    $form['revision_information']['revision'] = array(
      '#access' => user_access('administer nodes'),
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
      '#default_value' => $node->revision,
    );
    $form['revision_information']['log'] = array(
      '#type' => 'textarea',
      '#title' => t('Log message'),
      '#default_value' => isset($node->log) ? $node->log : '',
      '#rows' => 2,
      '#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
    );
  }

  // Node author information for administrators
  $form['author'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer nodes'),
    '#title' => t('Authoring information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => 20,
  );
  $form['author']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Authored by'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => $node->name ? $node->name : '',
    '#weight' => -1,
    '#description' => t('Leave blank for %anonymous.', array(
      '%anonymous' => variable_get('anonymous', t('Anonymous')),
    )),
  );
  $form['author']['date'] = array(
    '#type' => 'textfield',
    '#title' => t('Authored on'),
    '#maxlength' => 25,
    '#description' => t('Format: %time. Leave blank to use the time of form submission.', array(
      '%time' => !empty($node->date) ? $node->date : format_date($node->created, 'custom', 'Y-m-d H:i:s O'),
    )),
  );
  if (isset($node->date)) {
    $form['author']['date']['#default_value'] = $node->date;
  }

  // Node options for administrators
  $form['options'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer nodes'),
    '#title' => t('Publishing options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => 25,
  );
  $form['options']['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Published'),
    '#default_value' => $node->status,
  );
  $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,
  );

  // These values are used when the user has no administrator access.
  foreach (array(
    'uid',
    'created',
  ) as $key) {
    $form[$key] = array(
      '#type' => 'value',
      '#value' => $node->{$key},
    );
  }

  // Add the buttons.
  $form['buttons'] = array();
  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#access' => !variable_get('node_preview', 0) || !form_get_errors() && isset($form_state['node_preview']),
    '#value' => t('Save'),
    '#weight' => 5,
    '#submit' => array(
      'node_form_submit',
    ),
  );
  $form['buttons']['preview'] = array(
    '#type' => 'submit',
    '#value' => t('Preview'),
    '#weight' => 10,
    '#submit' => array(
      'node_form_build_preview',
    ),
  );
  if (!empty($node->nid) && node_access('delete', $node)) {
    $form['buttons']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#weight' => 15,
      '#submit' => array(
        'node_form_delete_submit',
      ),
    );
  }
  $form['#validate'][] = 'node_form_validate';
  $form['#theme'] = array(
    $node->type . '_node_form',
    'node_form',
  );
  return $form;
}