You are here

function node_form in Drupal 5

Same name and namespace in other branches
  1. 4 modules/node.module \node_form()
  2. 6 modules/node/node.pages.inc \node_form()
  3. 7 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.module, line 2072
The core that allows content to be submitted to the site. Modules and scripts may programmatically submit nodes using the usual form API pattern.

Code

function node_form($node, $form_values = NULL) {
  global $user;
  $node = (object) $node;
  node_object_prepare($node);

  // 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',
  ) as $key) {
    $form[$key] = array(
      '#type' => 'value',
      '#value' => $node->{$key},
    );
  }

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

  // Get the node-specific bits.
  if ($extra = node_invoke($node, 'form', $form_values)) {
    $form = array_merge_recursive($form, $extra);
  }
  if (!isset($form['title']['#weight'])) {
    $form['title']['#weight'] = -5;
  }
  $node_options = variable_get('node_options_' . $node->type, array(
    'status',
    'promote',
  ));

  // If this is a new node, fill in the default values.
  if (!isset($node->nid)) {
    foreach (array(
      'status',
      'promote',
      'sticky',
    ) as $key) {
      $node->{$key} = in_array($key, $node_options);
    }
    global $user;
    $node->uid = $user->uid;
  }

  // Always use the default revision setting.
  $node->revision = in_array('revision', $node_options);
  $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 ($node->revision || user_access('administer nodes')) {
    $form['log'] = array(
      '#type' => 'textarea',
      '#title' => t('Log message'),
      '#rows' => 2,
      '#weight' => 20,
      '#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,
  );
  $form['options']['revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => $node->revision,
  );

  // 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['preview'] = array(
    '#type' => 'button',
    '#value' => t('Preview'),
    '#weight' => 40,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#weight' => 45,
  );
  if ($node->nid && node_access('delete', $node)) {
    $form['delete'] = array(
      '#type' => 'button',
      '#value' => t('Delete'),
      '#weight' => 50,
    );
  }
  $form['#after_build'] = array(
    'node_form_add_preview',
  );

  // Ensure that node_validate() will always get called.
  $form['#validate']['node_form_validate'] = array();

  // Also, if the module defines its own _validate() routine based on the
  // form_id, include that in the #validate array, as well.
  $node_validate = $node->type . '_node_form_validate';
  if (function_exists($node_validate)) {
    $form['#validate'][$node_validate] = array();
  }
  $form['#base'] = 'node_form';
  return $form;
}