I'm working on a site I inherited that has a functioning hook_views_data_alter() implementation. I can see things in the Views editor that it has added. However, I cannot figure out how on earth to get that function to be called again so I can change the data. I am running xdebug with a breakpoint in the function body, and nothing I do, from editing a view, to adding a relationship, to clearing the cache or rebuilding the registry, trips that breakpoint. Not even putting module_invoke_all('views_data_alter'); in index.php. What do I need to do to get Drupal to run that hook again?

  • 1
    Do you have caching enabled for this view (under advanced tab)? In which module/template does this hooks_views_data_alter function reside? May 26 '16 at 2:40
  • 1
    Those hooks implementations are normally put in a MYMODULE.views.inc file; module_invoke_all('views_data_alter'); doesn't do anything because the file containing the hook needs to be first loaded.
    – apaderno
    May 26 '16 at 7:13
  • @longboardnode: No, no caching on the view. Yes, the hooks are in my custom module's .views.inc file, and I know that file is being read (xdebug tells me that)... I just can't figure out what action I need to do for the functions to actually be executed. Weird! May 26 '16 at 16:06
  • @longboardnode, no dice with _views_fetch_data_build(). I also emptied those two tables; no luck there either. One really important question is, what front-end action would ordinarily cause those functions to execute? If all were working right, what URL would I have to hit to invoke them? Maybe I'm just not doing the right action. May 27 '16 at 0:57
  • @JohnAlexander actually I just deleted my comment because I think Kiamlaluno's comment is more accurate. How is your views.inc file loaded? May 27 '16 at 0:58

In case anyone else is making the same mistake I was, here's the answer.

  1. First of all, hook_views_data() will not ever, ever be called if you put it in yourmodule.module. It has to be in yourmodule.views.inc.
  2. You should probably implement hook_views_api() to tell Drupal where your yourmodule.views.inc file resides. Do so like this:

function yourmodule_views_api($module = NULL, $api = NULL) { return array( 'api' => '3.0', 'path' => drupal_get_path('module', 'yourmodule') . '/includes/views', ); }

This tells Drupal that yourmodule.views.inc can be found at /yourmodule/includes/views/yourmodule.views.inc. If you don't use hook_views_api(), put the .inc file in the same folder as your .module file.

Once you have this set up, yourmodule_views_data() will be called when you clear the caches.

  • 1
    The Draggableviews module contains an example of hook_views_data_alter being called within a .module file. The breakpoint can be tripped when rebuilding the cache through the UI. Clearing it through drush, as I was doing, executed the code but didn't trip the breakpoint (with my xdebug setup anyhow).
    – manafire
    May 6 '19 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.