1

If the implementation of hook_install() of my module needs to use a class defined in the module file, should the code first load the module file with drupal_load()?

I would think that when the hook is invoked, the Drupal registry has not been yet updated to contain any reference to the PHP classes defined in the modules used by my module, and which are referred in the .info file. I looked at Drupal code, but I could not find any proof for what I think Drupal does.

Should I use drupal_load() or the file will be loaded automatically when I try to create an instance of the PHP class defined in the module file? Is it different with hook_update_N()?

2 Answers 2

4

There is no need to do that: It's already loaded. That is done from module_enable() which uses the following code.

if ($existing->status == 0) {
  // Load the module's code.
  drupal_load('module', $module);
  module_load_install($module);
  // …
}

That code is executed before the code that invokes the hook_install() implementation done by the module.

// Now install the module if necessary.
if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) {
  drupal_install_schema($module);

  // Set the schema version to the number of the last update provided
  // by the module.
  $versions = drupal_get_schema_versions($module);
  $version = $versions ? max($versions) : SCHEMA_INSTALLED;

  // If the module has no current updates, but has some that were
  // previously removed, set the version to the value of
  // hook_update_last_removed().
  if ($last_removed = module_invoke($module, 'update_last_removed')) {
    $version = max($version, $last_removed);
  }
  drupal_set_installed_schema_version($module, $version);

  // Allow the module to perform install tasks.
  module_invoke($module, 'install');

  // Record the fact that it was installed.
  $modules_installed[] = $module;
  watchdog('system', '%module module installed.', array(
    '%module' => $module,
  ), WATCHDOG_INFO);
}
0

Or you can just:

require_once dirname(__FILE__) . '/mymodule.module';

Anywhere inside of your .install file (top is better).

This is not the "Drupal way" but it will work seamlessly whatever happens.

The proper method would be:

drupal_load('module', 'mymodule');

Inside all hook_install() and hook_update_N() implementations that needs the class. But I'm not sure it will work at install time.

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.