node_form

Versions
4.6
node_form($edit)
4.7
node_form($node)
5
node_form($node, $form_values = NULL)
6
node_form(&$form_state, $node)
7
node_form($form, &$form_state, $node)

Generate the node editing form.

▾ 3 functions call node_form()

node_add in modules/node.module
Present a node submission form or a set of links to such forms.
node_edit in modules/node.module
Present a node editing form.
node_preview in modules/node.module
Generate a node preview, including a form for further edits.

Code

modules/node.module, line 1308

<?php
function node_form($edit) {
  // Validate the node if we don't already know the errors.
  if (!$edit->validated) {
    $edit = node_validate($edit);
  }

  // Prepend extra node form elements.
  $form = implode('', node_invoke_nodeapi($edit, 'form pre'));

  // Get the node-specific bits.
  // We can't use node_invoke() because $param must be passed by reference.
  $function = node_get_module_name($edit) .'_form';
  $param = array();
  if (function_exists($function)) {
    $form .= $function($edit, $param);
  }

  // Append extra node form elements.
  $form .= implode('', node_invoke_nodeapi($edit, 'form post'));

  $output .= '<div class="node-form">';

  // Add hidden 'op' variable, which specifies the default operation (Preview).
  $output .= '<input type="hidden" name="op" value="'. check_plain(t('Preview')) ."\" />\n";

  // Add the admin-specific parts.
  if (user_access('administer nodes')) {
    $output .= '<div class="admin">';

    $author = form_textfield(t('Authored by'), 'name', $edit->name, 20, 60);
    $author .= form_textfield(t('Authored on'), 'date', $edit->date, 20, 25, NULL, NULL, TRUE);

    $output .= '<div class="authored">';
    $output .= form_group(t('Authoring information'), $author);
    $output .= "</div>\n";

    $node_options = variable_get('node_options_'. $edit->type, array('status', 'promote'));
    $options .= form_checkbox(t('Published'), 'status', 1, isset($edit->status) ? $edit->status : in_array('status', $node_options));
    $options .= form_checkbox(t('In moderation queue'), 'moderate', 1, isset($edit->moderate) ? $edit->moderate : in_array('moderate', $node_options));
    $options .= form_checkbox(t('Promoted to front page'), 'promote', 1, isset($edit->promote) ? $edit->promote : in_array('promote', $node_options));
    $options .= form_checkbox(t('Sticky at top of lists'), 'sticky', 1, isset($edit->sticky) ? $edit->sticky : in_array('sticky', $node_options));
    $options .= form_checkbox(t('Create new revision'), 'revision', 1, isset($edit->revision) ? $edit->revision : in_array('revision', $node_options));

    $output .= '<div class="options">';
    $output .= form_group(t('Options'), $options);
    $output .= "</div>\n";

    $extras .= implode('</div><div class="extra">', node_invoke_nodeapi($edit, 'form admin'));
    $output .= $extras ? '<div class="extra">'. $extras .'</div></div>' : '</div>';
  }

  // Add the default fields.
  $output .= '<div class="standard">';
  $output .= form_textfield(t('Title'), 'title', $edit->title, 60, 128, NULL, NULL, TRUE);

  // Add the node-type-specific fields.
  $output .= $form;

  // Add the hidden fields.
  if ($edit->nid) {
    $output .= form_hidden('nid', $edit->nid);
  }

  if (isset($edit->uid)) {
    // The use of isset() is mandatory in the context of user IDs, because
    // user ID 0 denotes the anonymous user.
    $output .= form_hidden('uid', $edit->uid);
  }

  if ($edit->created) {
    $output .= form_hidden('created', $edit->created);
  }

  if ($edit->changed) {
    $output .= form_hidden('changed', $edit->changed);
  }

  $output .= form_hidden('type', $edit->type);

  // Add the buttons.
  $output .= form_submit(t('Preview'));

  if ($edit->type && (($_POST['op'] == t('Preview') && !form_get_errors()) || !variable_get('node_preview', 0))) {
    $output .= form_submit(t('Submit'));
  }

  if ($edit->nid && node_access('delete', $edit)) {
    $output .= form_submit(t('Delete'));
  }

  $output .= '</div></div>';

  $extra = node_invoke_nodeapi($edit, 'form param');
  foreach ($extra as $key => $value) {
    if (is_array($value)) {
      if (isset($param[$key])) {
        $param[$key] = array_merge($param[$key], $value);
      }
      else {
        $param[$key] = $value;
      }
    }
    else {
      $param[$key] = $value;
    }
  }

  $attributes = array('id' => 'node-form');
  if (is_array($param['options'])) {
    $attributes = array_merge($param['options'], $attributes);
  }
  return form($output, ($param['method'] ? $param['method'] : 'post'), $param['action'], $attributes);
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.