Understanding the Drupal Way - Alters

Submitted by Marc on Fri, 12/11/2015 - 12:27pm

In Drupal an alter is intended to provide a way to modify an object during a point in the execution of a certain event or process. Alters are defined within individual modules. Those modules then give the opportunity for other modules to "alter" an object by calling drupal_alter at the appropriate time.

For example, the core field module calls drupal_alter on 'field_attach_view' inside of the field_view_field function. The core rdf module implements that alter by creating a function that is named rdf_field_attach_view_alter. The purpose of this function is to "Append term mappings on displayed taxonomy links." The function looks for a taxonomy term reference element that is being displayed as a link and then adds some RDF data to that element's attributes.

This has probably gotten a bit too technical and abstract to make complete sense. To tie the information back into what I stated the purpose of an alter is, the "object" that is being modified is a field that is going to be displayed. The modification that is happening is to provide a bit of metadata about the field so that things like search engines or aggregators can make better sense of that field.

This process was accomplished through the use of an alter function. Since the field module defined the alter function it was possible for the rdf module to add its metadata to the field without having to hack into the field module.

If you would like to learn more about Drupal you can take my "Drupal for Beginners" class for just $5.