2

I'm looking to override format_size(), but I'm not sure if this is possible in a module.
If I implement myModule_format_size() or in a theme myTheme_format_size() will the hook be called?

1

No. Not all functions invokes a hook in other modules. As we can see in http://api.drupal.org/api/drupal/includes%21common.inc/function/format_size/7 there is no module_invoke_all is called.

Here is how we define module_invoke_all

Invokes a hook in all enabled modules that implement it.

function module_invoke_all($hook) {
  $args = func_get_args();
  // Remove $hook from the arguments.
  unset($args[0]);
  $return = array();
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;
    if (function_exists($function)) {
      $result = call_user_func_array($function, $args);
      if (isset($result) && is_array($result)) {
        $return = array_merge_recursive($return, $result);
      }
      elseif (isset($result)) {
        $return[] = $result;
      }
    }
  }

  return $return;
}
2
  • Thank you. So in theory, there is no way to edit the function above without "hacking core"??
    – user5681
    Feb 18 '13 at 3:50
  • Yes right now there is no way to extend that function. drupal.org/best-practices/do-not-hack-core
    – niksmac
    Feb 18 '13 at 3:51
1

In Drupal, a hook is not a function that is called instead of a function defined from Drupal; it is a function that Drupal, or any third-party module, calls through module_invoke_all(), or module_invoke().
In your case, mymodule_format_size() will never called instead of format_size(), but it will be eventually called as hook_format_size() implementation (whatever purpose it has), if a module invokes that hook.

Since it is not possible to expand format_size(), which doesn't invoke any hook, contrary to format_username(), the only way to change the code executed from format_size() is to act on the functions calling it. In other words:

  • If format_size() is used in a form submission handler, then you could alter that form to remove that form submission handler, and replace it with your own handler, which doesn't call format_size()

  • If format_size() is used from a template file that is used from a theme function implemented in a module, you could override the template file in a theme

  • If format_size() is used from a theme function, you could override the theme function (in a module or in a theme) with a version of the theme function that doesn't call format_size()

Your Answer

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