background image

Content tagged with: form_alter

Eric's picture

To improve usability, I wanted to add a note to the bottom of my form to show that an asterisk indicates a required form field. In this case, I am modifying the user registration form, but the code can be simplified to work with any form API.

<?php
function MYMODULE_form_alter($form_id, &$form) {
 
// ...code...
 
if ($form_id == 'user_register') {
   
$form['requiredNote'] = array(
     
'#value' => "
        <div class='form-item'>
          <label>
            <span class='form-required'>*</span> Indicates Required Field
          </label>
        </div>"
,
     
'#weight' => 10,
    );
  }
 
// ...code...
}
?>

Eric's picture

In certain situations, you might want to modify a node's form and functionality. In this example, I'll explain how to remove the title field from a node's form and add your own title later.

<?php
// using hook_form_alter to remove the node title input field...
function MYMODULE_form_alter($form_id, &$form) {
 
// ...code....
 
if ($form_id == 'MYNODETYPE_node_form' && arg(0)!='admin') {
    unset(
$form['title']);
  }
 
// ...code...
}

// using hook_nodeapi to add the title when the node form is submitted...
function MYMODULE_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
 
// ...code...
 
if ($node->type == 'MYNODETYPE' && $op == 'submit') {
   
// get a list of node types
   
$nodeTypes = node_get_types();
   
// set the node's title
   
$node->title = $nodeTypes[$node->type]->name;
  }
 
// ...code...
}
?>