Code snippet: How to change the label of the "Add more items" button

Last modified: July 31, 2009 - 18:44

Here's a code snippet that you can use to alter the label of the "Add more items" button used for multiple value fields in CCK.

This is a sample module that implements hook_form_alter(), then installs an after_build callback to the form, then scan the form elements recursively in search for "Add more items" buttons.

This method should work for multiple value fields, as well as for multigroups. It should also apply the label change during the AHAH process attached to the buttons.

<?php
/**
* Implementation of hook_form_alter().
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
 
// Alter the node edit form.
  // Note that $form['type']['#value'] contains the node type, so you may want
  // to filter by that as well.
 
if (isset($form['#node']) && isset($form['type']['#value']) && $form_id == $form['type']['#value'] .'_node_form') {
    if (!isset(
$form['#after_build'])) {
     
$form['#after_build'] = array();
    }
   
$form['#after_build'][] = '_mymodule_node_form_after_build';
  }
}

/**
* After build callback for our node form.
*/
function _mymodule_node_form_after_build($form, &$form_state) {
 
_mymodule_node_form_after_build_recursive($form);
  return
$form;
}

/**
* Scan the node form recursively in search for "Add more items" buttons.
*/
function _mymodule_node_form_after_build_recursive(&$elements) {
  foreach (
element_children($elements) as $key) {
    if (isset(
$elements[$key]) && $elements[$key]) {
      if (isset(
$elements[$key][$key .'_add_more'])) {
       
// So far so good, we have now targeted an "Add more items" button.

        // $key is the field name or the multigroup name.
        // The attribute '#type_name' contains the node type.
        // If this is a single field, then the attribute '#field_name' contains the name of the field.
        // If this is a multigroup, then the attribute '#group_name' contains the name of the group.
        // The attribute '#value' contains the button label. You can change this to suit your needs.

        // What follows is just an example where we're using the label of
        // the field or group to alter the button.

       
$type_name = $elements[$key][$key .'_add_more']['#type_name'];

        if (isset(
$elements[$key][$key .'_add_more']['#field_name'])) {

         
// Obtain information about the field.
         
$field = content_fields($key, $type_name);

         
// Change the label of the button.
         
$elements[$key][$key .'_add_more']['#value'] = t('Add another @name', array('@name' => t($field['widget']['label'])));
        }
        elseif (isset(
$elements[$key][$key .'_add_more']['#group_name'])) {

         
// Obtain information about the multigroup.
         
$groups = fieldgroup_groups($type_name);
         
$group = $groups[$key];

         
// Change the label of the button.
         
$elements[$key][$key .'_add_more']['#value'] = t('Add another item to @name', array('@name' => t($group['label'])));

        }

      }
      else {
       
// Recurse through all children elements.
       
_mymodule_node_form_after_build_recursive($elements[$key]);
      }
    }
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.