0

I try to implement hook_menu() in a module and the code inside of this hook depends on another hook implemented in the same module Example:

function mymodule_menu() {
   $streamwrapper = file_stream_wrapper_get_instance_by_scheme('myscheme');
   if($streamwrapper !== false) {
      $directory = $streamwrapper->getDirectoryPath();
       .....
   }
}

and another hook function in the same (mymodule) module

function mymodule_stream_wrappers() {
  retunr array( 'myscheme' => array(...)
}

Apparently, hook_stream_wrappers() is called after hook_menu(), and file_stream_wrapper_get_instance_by_scheme() returns false during module installation process.

So, the whole idea works because Drupla will call hook_menu eventually and by that time stream wrapper will be in place and ready, but it seems too unreliable, IMHO.

Anyway, is it possible to control hooks order somehow? Or is there a better way to achieve desired result - retrieve stream wrapper in hook_menu after is has been known by the system?

2 Answers 2

3

Your problem is actually the opposite of what you're thinking. A call to file_stream_wrapper_get_instance_by_scheme triggers hook_stream_wrappers() if it hasn't been called already.

However, the problem is that the stream wrapper hook was already called before your module was enabled and the information from that is statically cached. So you need to clear the static cache to get your own stream wrapper:

drupal_static_reset('file_get_stream_wrappers');
4
  • Thanks Berdir! Right to the point - the problem is because of cache, but the only way to do what I want (without Drupal patching) is to call drupal_static_reset right before I call for my wrapper. Look like file_get_stream_wrappers is implemented:
    – Michael
    Commented Mar 15, 2012 at 14:01
  • I miss my chance to edit previous comment sorry... file_get_stream_wrappers() checks entire cache, regardless of schema and calls the modules hooks if it is not set. Means that any cached wrapper disables hooks for all of the rest. The cache is almost for sure is populated by "public" and "private" and "Clear all caches" in admin does not help. And if I call drupal_static_reset() I will set other modules in the same trouble.
    – Michael
    Commented Mar 15, 2012 at 14:16
  • This is just a static cache which means that it rebuilt on every request. So this really should only happen while your module is being installed. So it should be safe to call drupal_static_reset() (you could do it in your hook_enable() to prevent it from running on every subsequent menu rebuild). Most static caches in drupal are pretty dump and need to be explicitly cleared if know that there is more up to date information.
    – Berdir
    Commented Mar 15, 2012 at 15:18
  • It really happens during install only. Thanks for the hook_enable() idea!
    – Michael
    Commented Mar 15, 2012 at 15:37
0

According the documentation, the function file_stream_wrapper_get_instance_by_scheme call another function that call the hook hook_stream_wrappers.

Logically your code should work.

I see here that you've a mistake in your return

function mymodule_stream_wrappers() {
  retunr array( 'myscheme' => array(...)
}

it may the cause of your problem.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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