Altering Drupal's Behavior

Chris Johnson, VP of Engineering
#Development | Posted

Drupal's desire to offer flexibility to developers and the substantial availability of hooks, plugins, modules and other methods to allow access to points for this flexibility can often make figuring out the right way to change something a challenge. A colleague of mine often says that when he finds what he's trying to do very difficult that is a sign that he's trying to do it using the wrong access point.

On a recent project another developer from Phase2 needed to provide an alternate views sort handler for each field of a particular type, in this case, video embed. For anyone who has worked with views at this level before, the hooks: hook_views_data and hook_views_data_alter immediately spring to mind as possible access points for doing this substitution. This eventually led to the question, "How can I tell what the base table name is for all of the fields of type video embed?"

An answer to this question is the SQL statement

  concat('field_data_', field_name)
  field_config fc
  join field_config_instance fci using(field_name)
  fc.type = 'video_embed_field'[/pgsql]

an alternate answer is to use the function field_info_fields to retrieve information about all of the fields and then build a list from there.

Both of these solutions, however, seem overly complicated for the desired result so we began to hunt for a better way. Knowing that views gets its data from hook_views_data and hook_views_data_alter I first looked to see if either of the functions field_views_data or field_views_data_alter existed. It turns out both do so I started looking for instances of module_invoke, module_invoke_all and drupal_alter. In field_views_data I found my answer, a drupal_alter call creating a hook_field_views_data_alter that could be used.

Once switched to the correct hook, it became trivial to identify which items being described to views were from the video embed module and adjust them accordingly.

So, if you find yourself fighting to make a change to Drupal, it may be because you are working at the wrong level. Take a step back, use hooks you know about to give you a starting point for where the data you want to change is initially defined and use the source code to see if there is a better access point to use for the change you want to make.

Though this example was for hooks involving the contributed views module, Drupal's core also offers a large number of hooks. Take a look at to get a sense of what is available.

Chris Johnson

Chris Johnson

VP of Engineering