1

I am trying to implement hook_library in my custom module. To help narrow down the problem, I wrote a dummy module with just the following two functions: hook_views_api and hook_library. I put a dsm statement in each of the hooks, to test whether the hook was getting executed. The hook_views_api hook is getting executed but not the hook_library hook. Anyone see what I am doing wrong?

I have tried clearing the cache. I have tried uninstalling and reinstalling the module.

Here is the code:

// hook_views_api
function mymodule_views_api() {
  $path = drupal_get_path('module', 'mymodule') . '/views';
  dsm ("inside mymodule_views_api");
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'mymodule') . '/views',
  );
} 

// hook_library
function mymodule_library(){
  dsm ("inside mymodule_library");
  return array();
}
  • Have you tried using watchdog() instead of dsm()? – David Thomas Jan 9 '13 at 21:01
  • I just tried using watchdog() instead, and I am having the same problem: the hook_library watchdog message does not show up in the log, i.e. it still seems hook_library is not getting executed. – user606696 Jan 9 '13 at 21:33
  • I am now wondering ... perhaps hook_library does not get executed until it is needed, i.e. until the first call to drupal_add_library. I will test this and report back ... – user606696 Jan 9 '13 at 21:35
  • OK, remember to clear drupal cache, e.g admin > performance > clear all caches – David Thomas Jan 9 '13 at 21:46
  • 1
    Problem solved! I added a call to drupal_add_library(), and hook_library now gets executed. So it seems hook_library is not executed until it is needed. – user606696 Jan 9 '13 at 22:13
1

The only function that invokes hook_library() is drupal_get_library(), which is then called by drupal_add_library(), the function that modules call to add a library during a page request. Until one of those functions is not called, hook_library() is not invoked.

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.