Allow other functions to alter your data in Drupal 7

Allow other functions to alter your data in Drupal 7

You created a function and maybe you want other modules to be able to go in and modify data as needed. Maybe you’re returning a list of links and you want other modules to add their own links if needed. drupal_alter is one way of accomplishing this.

function moduleone_get_items() {
  $items = array();
  $items[] = l('link 1', 'link/1');
  $items[] = l('link 2', 'link/2');
  drupal_alter(__FUNCTION__, $items);
  return $items;
}

In another module, moduletwo.module, you would do something like this to access $items and modify as needed.

function moduletwo_moduleone_get_items_alter(&$items) {
  $items[] = l('link 3', 'link/3');
}

Simple as that :)

Comments are closed.