Community Documentation

hook_form

5 node.php hook_form(&$node, $form_values)
6 node.php hook_form(&$node, $form_state)
7 node.api.php hook_form($node, &$form_state)
8 node.api.php hook_form($node, &$form_state)

Display a node editing form.

This hook, implemented by node modules, is called to retrieve the form that is displayed when one attempts to "create/edit" an item. This form is displayed at the URI http://www.example.com/?q=node/<add|edit>/nodetype.

Parameters

&$node: The node being added or edited.

$form_state: The form state array. Changes made to this variable will have no effect.

Return value

An array containing the form elements to be displayed in the node edit form.

The submit and preview buttons, taxonomy controls, and administrative accoutrements are displayed automatically by node.module. This hook needs to return the node title, the body text area, and fields specific to the node type.

For a detailed usage example, see node_example.module.

Related topics

▾ 90 functions implement hook_form()

block_add_block_form in modules/block/block.admin.inc
Menu callback: display the custom block addition form.
block_admin_display_form in modules/block/block.admin.inc
Generate main blocks administration form.
block_box_form in modules/block/block.module
Define the custom block form.
blog_form in modules/blog/blog.module
Implementation of hook_form().
book_outline_form in modules/book/book.pages.inc
Build the form to handle all book outline operations via the outline tab.
book_remove_form in modules/book/book.pages.inc
Menu callback; builds a form to confirm removal of a node from the book.
color_scheme_form in modules/color/color.module
Form callback. Returns the configuration form.
comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.
comment_unpublish_by_keyword_action_form in modules/comment/comment.module
Form builder; Prepare a form for blacklisted keywords.
confirm_form in modules/system/system.module
Generates a form array for a confirmation form.
dblog_filter_form in modules/dblog/dblog.admin.inc
Return form for dblog administration filters.
drupal_get_form in includes/form.inc
Retrieves a form from a constructor function, or from the cache if the form was built in a previous page-load. The form is then passed on for processing, after and rendered for display if necessary.
drupal_prepare_form in includes/form.inc
Prepares a structured form array by adding required elements, executing any hook_form_alter functions, and optionally inserting a validation token to prevent tampering.
drupal_process_form in includes/form.inc
This function is the heart of form API. The form gets built, validated and in appropriate cases, submitted.
drupal_rebuild_form in includes/form.inc
Retrieves a form, caches it and processes it with an empty $_POST.
drupal_redirect_form in includes/form.inc
Redirect the user to a URL after a form has been processed.
drupal_render_form in includes/form.inc
Renders a structured form array into themed HTML.
drupal_retrieve_form in includes/form.inc
Retrieves the structured array that defines a given form.
drupal_validate_form in includes/form.inc
Validates user-submitted form data from the $form_state using the validate functions defined in a structured form array.
example_form in developer/example.profile
Form API array definition for the example form.
filter_admin_format_form in modules/filter/filter.admin.inc
Generate a filter format form.
filter_form in modules/filter/filter.module
Generates a selector for choosing a format in a form.
forum_form in modules/forum/forum.module
Implementation of hook_form().
install_configure_form in ./install.php
Form API array definition for site configuration.
install_select_locale_form in ./install.php
Form API array definition for language selection.
install_select_profile_form in ./install.php
Form API array definition for the profile selection form.
install_settings_form in ./install.php
Form API array definition for install_settings.
locale_languages_configure_form in includes/locale.inc
Setting for language negotiation options
locale_languages_custom_form in includes/locale.inc
Custom language addition form.
locale_languages_delete_form in includes/locale.inc
User interface for the language deletion confirmation screen.
locale_languages_edit_form in includes/locale.inc
Editing screen for a particular language.
locale_languages_overview_form in includes/locale.inc
User interface for the language overview screen.
locale_languages_predefined_form in includes/locale.inc
Predefined language setup form.
locale_translate_delete_form in includes/locale.inc
User interface for the string deletion confirmation screen.
locale_translate_edit_form in includes/locale.inc
User interface for string editing.
locale_translate_export_pot_form in includes/locale.inc
Translation template export form.
locale_translate_export_po_form in includes/locale.inc
Form to export PO files for the languages provided.
locale_translate_import_form in includes/locale.inc
User interface for the translation import screen.
locale_translate_seek_form in includes/locale.inc
User interface for the string search screen.
menu_item_delete_form in modules/menu/menu.admin.inc
Build a confirm form for deletion of a single menu link.
menu_overview_form in modules/menu/menu.admin.inc
Form for editing an entire menu tree at once.
node_assign_owner_action_form in modules/node/node.module
node_content_form in modules/node/node.module
Implementation of hook_form().
node_filter_form in modules/node/node.admin.inc
Return form for node administration filters.
node_form in modules/node/node.pages.inc
Generate the node add/edit form array.
node_type_form in modules/node/content_types.inc
Generates the node type editing form.
node_unpublish_by_keyword_action_form in modules/node/node.module
openid_redirect_form in modules/openid/openid.inc
openid_user_delete_form in modules/openid/openid.pages.inc
Present a confirmation form to delete the specified OpenID identity from the system.
path_admin_filter_form in modules/path/path.admin.inc
Return a form to filter URL aliases.
path_admin_form in modules/path/path.admin.inc
Return a form for editing or creating an individual URL alias.
poll_cancel_form in modules/poll/poll.module
Builds the cancel form for a poll.
poll_form in modules/poll/poll.module
Implementation of hook_form().
profile_field_form in modules/profile/profile.admin.inc
Menu callback: Generate a form to add/edit a user profile field.
search_form in modules/search/search.module
Render a search form.
system_actions_delete_form in modules/system/system.module
Create the form for confirmation of deleting an action.
system_actions_manage_form in modules/system/system.module
Define the form for the actions overview page.
system_goto_action_form in modules/system/system.module
Implementation of a configurable Drupal action. Redirect user to a URL.
system_message_action_form in modules/system/system.module
system_modules_confirm_form in modules/system/system.admin.inc
Display confirmation form for dependencies.
system_modules_uninstall_confirm_form in modules/system/system.admin.inc
Confirm uninstall of selected modules.
system_send_email_action_form in modules/system/system.module
Return a form definition so the Send email action can be configured.
system_settings_form in modules/system/system.module
Add default buttons to a form and set its prefix.
system_themes_form in modules/system/system.admin.inc
Menu callback; displays a listing of all themes.
system_theme_select_form in modules/system/system.module
Returns a fieldset containing the theme select form.
taxonomy_form in modules/taxonomy/taxonomy.module
Generate a form element for selecting terms from a vocabulary.
template_preprocess_block_admin_display_form in modules/block/block.admin.inc
Process variables for block-admin-display.tpl.php.
template_preprocess_search_block_form in modules/search/search.module
Process variables for search-block-form.tpl.php.
template_preprocess_search_theme_form in modules/search/search.module
Process variables for search-theme-form.tpl.php.
theme_block_admin_display_form in developer/theme.php
Implemented using the block-admin-display-form.tpl.php template.
theme_color_scheme_form in modules/color/color.module
Theme color form.
theme_form in includes/form.inc
Format a form.
theme_locale_languages_overview_form in includes/locale.inc
Theme the language overview form.
theme_menu_overview_form in modules/menu/menu.admin.inc
Theme the menu overview form into a table.
theme_node_filter_form in modules/node/node.admin.inc
Theme node administration filter form.
theme_node_form in modules/node/node.pages.inc
Present a node submission form.
theme_search_block_form in developer/theme.php
Implemented using the search-block-form.tpl.php template.
theme_search_theme_form in developer/theme.php
Implemented using the search-theme-form.tpl.php template.
theme_system_themes_form in modules/system/system.admin.inc
Theme function for the system themes form.
theme_system_theme_select_form in modules/system/system.admin.inc
Theme the theme select form.
theme_user_filter_form in modules/user/user.admin.inc
Theme user administration filter form.
trigger_assign_form in modules/trigger/trigger.admin.inc
Create the form definition for assigning an action to a hook-op combination.
update_script_selection_form in ./update.php
user_admin_access_form in modules/user/user.admin.inc
Form builder; Configure access rules.
user_edit_form in modules/user/user.module
user_filter_form in modules/user/user.admin.inc
Form builder; Return form for user administration filters.
user_profile_form in modules/user/user.pages.inc
Form builder; edit a user account or one of their profile categories.
_menu_overview_tree_form in modules/menu/menu.admin.inc
Recursive helper function for menu_overview_form().
_poll_choice_form in modules/poll/poll.module
_upload_form in modules/upload/upload.module

File

developer/hooks/node.php, line 245
These hooks are defined by node modules, modules that define a new kind of node.

Code

<?php
function hook_form(&$node, $form_state) {
  $type = node_get_types('type', $node);

  $form['title'] = array(
    '#type' => 'textfield', 
    '#title' => check_plain($type->title_label), 
    '#required' => TRUE, 
    '#default_value' => $node->title,
  );

  $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);

  $form['field1'] = array(
    '#type' => 'textfield', 
    '#title' => t('Custom field'), 
    '#default_value' => $node->field1, 
    '#maxlength' => 127,
  );
  $form['selectbox'] = array(
    '#type' => 'select', 
    '#title' => t('Select box'), 
    '#default_value' => $node->selectbox, 
    '#options' => array(
      1 => 'Option A', 
      2 => 'Option B', 
      3 => 'Option C',
    ), 
    '#description' => t('Please choose an option.'),
  );

  return $form;
}
?>

Comments

forms_api_reference

For a strange reason I never can't find this page wich is really useful.
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

Administrative accoutrements?

It did make me laugh.

But as long as we're talking api documentation here, accuracy is important.

As we're not talking additional items of clothing, could this be changed from:

The submit and preview buttons, taxonomy controls, and administrative accoutrements are displayed automatically by node.module.

to something like:

The submit and preview buttons, taxonomy controls, workflow options, etc. are displayed automatically by node.module.

I don't think the accuracy is

I don't think the accuracy is affected :)

If you are petitioning for the inclusion of 'workflow options' and 'etc', those could certainly be added:

The submit and preview buttons, taxonomy controls, workflow options, administrative accoutrements, etc. are displayed automatically by node.module.

node content forms

while reading examples and books, it seemed that when you have a module defined node type such as:

    $node = array(
        'mynodetype' => array(
            'name' => t('MyType'),
            'module' => 'mymodule',
            'description' => t('My very own node type'),
            'has_title' => TRUE,
            'title_label' => t('And I Will Call Him ...'),
            'has_body' => TRUE,
            'body_label' => t('George'),
        )
    );

that the node form when creating new content would display the defined title and body labels - but they were still showing "Title" and "Body".

in order for my nice labels to show up i had to define hook_form() as

function mymodule_form($node, $form_state) {
    return node_content_form($node, $form_state);
}

Why this hook generates not

Why this hook generates not unique form ID? For all of them it is "node-form". And how can I change it?

Login or register to post comments