How do you include the functions in an update_N hook?

Comments

q0rban’s picture

Title: How do you include the functions in an update_N hook? » Make it dead simple to include the API, and add documentation
Category: support » feature

Hmm, this probably is a bit confusing, isn't it?

Since this is most often used in conjunction with a wrapper module that is implementing the API, I have it inside that module's directory and then use module_load_include().

I think it would make sense to have a module file though, so you could just run a function to include the needed files. What do you think?

mrfelton’s picture

Yes, Im using the functions from within several modules update hooks, so ideally I need to just be able to load it using module_load_include(). I guess you jkust need to create a .info and a .module file for that to work?

Better still... consider integrating with install_profile_api. I like your functions, and I like the ones in install_profile_api - I use both, but I can't see the need for both projects to exists. They should be combined. Install profile API already has a good wrapper function for including required functions.

q0rban’s picture

> Install profile API already has a good wrapper function for including required functions.

I'd rather do something similar to ctools where you just call something like update_api_include('user'); and you get all the functions for users. I'll think about it some more. If you have any ideas, certainly let me know. :)

> Better still... consider integrating with install_profile_api.

I'm totally down with merging, I just haven't gotten any response on #498616: Merge update_api and install_profile_api. I don't really expect a response any time soon, as most of the maintainers are probably working on D7.

mrfelton’s picture

> I'd rather do something similar to ctools where you just call something like update_api_include('user'); and you get all the functions for users. I'll think about it some more. If you have any ideas, certainly let me know. :)

That is basically how the install_profile_api() wrapper works... You simply call:

// Get update api helper functions.
module_load_include('module', 'install_profile_api');
install_include(array('content'));

you pass the names of any supported modules that you want the functions from.

function install_include($modules) {
  // We intentionally avoid drupal_get_path() here, as it relies on the system
  // database table. Avoiding drupal_get_path() allows this function to be
  // called even when the database has not been initialized.
  $path = dirname(__FILE__);

  // Automatically include the required core modules, even if not specified.
  $modules = array_unique(array_merge($modules, drupal_required_modules()));

  foreach ($modules as $module) {
    if (file_exists("$path/contrib/$module.inc")) {
      include_once "$path/contrib/$module.inc";
    }
    elseif (file_exists("$path/core/$module.inc")) {
      include_once "$path/core/$module.inc";
    }
  }
}

I don't know much about ctools (yet) but the method sounds similar from what you described...

q0rban’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.