Skip to content Skip to navigation

Altering Custom Elements in Forms

Drupal provides a powerful framework for creating custom elements for use in forms. One example of a custom element is the Link field. Suppose you want to change the default label on a Link field to read "Link text." How do you alter it?

 In his blogpost Custom Drupal Elements for Forms, Silvio J. Gutierrez provides a great explanation how to define a custom form element. Altering a custom form element follows in a similar manner.

To alter a custom form element:

  1. Register a process callback

  2. Create a process callback.

What’s a callback?

In Drupal a callback is a function that allows you to define how some type of processing happens. You pass this function name as a parameter to a Drupal API function and this function is then called at the appropriate time.

How do I register my callback?

When creating a custom form element you would use the #process attribute in hook_element_info() to register a callback. Similarly, when altering a custom form element you use the #process attribute in hook_element_info_alter() to register your callback. In this example, I want to make changes to Link fields. My code is as follows: 

* Implements hook_element_info_alter().
function my_module_element_info_alter(&$type) {
  if (isset($type['link_field'])) {
    $type['link_field']['#process'][] = 'my_module_link_field_process';

How do I create my process callback?

The signature for a callback function for altering a custom form element is the same as if you were creating the element. See hook_element_info() for more information. In this case want to change the label for the Link field title. My code is as follows:


* Process Callback Function
* Callback to process the My Module link field.
function my_module_link_field_process($element, $form_state, $complete_form) {
  if (isset($element['#field_name'])) {
    switch ($element['#field_name']) {
      case 'field_s_my_link':
        $element['title']['#title'] = t('Link text');
  return $element;


Using these steps I was able to add help text and alter all sorts of custom form elements. Next time you have a custom element that needs even more customizing, give this a try, and let me know how it goes!


My thanks to my colleague Greg Garvey who pointed me to this solution!

Other resources:

See this discussion from the Link issue queue.