function hook_form

Display a node editing form.

This hook, implemented by node modules, is called to retrieve the form that is displayed to create or edit a node. This form is displayed at path node/add/[node type] or node/[node ID]/edit.

The submit and preview buttons, administrative and display controls, and sections added by other modules (such as path settings, menu settings, comment settings, and fields managed by the Field UI module) are displayed automatically by the node module. This hook just needs to return the node title and form editing fields specific to the node type.

Parameters

Drupal\node\Node $node: The node being added or edited.

$form_state: The form state array.

Return value

An array containing the title and any custom form elements to be displayed in the node editing form.

Related topics

225 functions implement hook_form()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

Access::operator_form in drupal/core/modules/node/lib/Drupal/node/Plugin/views/filter/Access.php
Options form subform for setting the operator.
action_admin_delete_form in drupal/core/modules/action/action.admin.inc
Creates the form for confirmation of deleting an action.
action_admin_manage_form in drupal/core/modules/action/action.admin.inc
Define the form for the actions overview page.
action_goto_action_form in drupal/core/modules/action/action.module
Constructs the settings form for action_goto_action().
action_message_action_form in drupal/core/modules/action/action.module
Constructs the settings form for action_message_action().

... See full list

File

drupal/core/modules/node/node.api.php, line 1127
Hooks provided by the Node module.

Code

function hook_form(Drupal\node\Node $node, &$form_state) {
  $type = node_type_load($node->type);
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#default_value' => !empty($node->title) ? $node->title : '',
    '#required' => TRUE,
    '#weight' => -5,
  );
  $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('Choose an option.'),
  );
  return $form;
}