The module_list function caches a list of modules that are currently enabled. What it doesn't do is give us information about these modules. Many times, we want to get information about the module, like its name. To retrieve the human readable name of the module, we are forced to rescan the .info.

In the admin/user/access, for example, we need to display module names so that the user can pick which permissions are available to their site, per module. To get this name, the page re-reads the .info file of each module. This seems kind of silly since that information is already stored in the database's {system} table.

I propose we cache the {system}->info field within module_list, so that this information is easily accessible. This will allow us to display module information without hitting the file system and parsing the .info file again. All the information will just be given to us through a new associative arrayed module_list, and a new function called module_info, which gives us just that information.

/**
 * Retrieve information regarding the given module.
 *
 * @param $module
 *   (Optional) The module of which to obtain information for.
 * @return
 *   An associative array of information regarding the module.
 */
function module_info($module) {
  $module_list = module_list();
  return isset($module_list[$module]) ? $module_list[$module] : NULL;
}
CommentFileSizeAuthor
#3 338002.patch5.67 KBRobLoach
module_list_info.patch6.09 KBRobLoach
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

RobLoach’s picture

Another place where this would be helpful is in #328357: Allow module .info files to add CSS/JS where we want to allow module.info files' scripts[] and styles[] automatically add JavaScript and CSS to the page because then it would cache that information for us.

Status: Needs review » Needs work

The last submitted patch failed testing.

RobLoach’s picture

FileSize
5.67 KB

This seems much more complicated then it needs to be....

skilip’s picture

AFAIK the .info files are only read when the registry is rebuilt. Afterwards both module and theme info are fetched from the system table. I do not see how this can be improved tpesking in terms of performance.

RobLoach’s picture

Something like this?

/**
* Retrieve information regarding the given module.
*
* @param $module
*   (optional) The module of which to obtain information for. When not
*   provided, will retrieve the information of all enabled modules.
* @return
*   An associative array of information regarding the module. FALSE if the
*   module does not exist.
*/
function module_info($module = NULL) {
  static $module_info = array();
  if (!isset($module)) {
    $data = db_query("SELECT name, info FROM {system} WHERE status = 1")->fetchAllAssoc('name');
    foreach ($data as $name => $info) {
      $module_info[$name] = unserialize($info->info);
    }
    return $module_info;
  }
  else if (!isset($module_info[$module])) {
    $info = db_query("SELECT info FROM {system} WHERE name = :name", array(
      ':name' => $module,
    ))->fetchAssoc();
    $module_info[$module] = $info ? unserialize($info['info']) : FALSE;
  }
  return $module_info[$module];
}
David_Rothstein’s picture

We already have http://api.drupal.org/api/function/system_get_info/7 (hm, maybe the issue that added that originally should really have been a duplicate of this one?) and an issue for adding better caching to it at #887870: Make system_list() return full module records so maybe this should be marked a duplicate at this point?

RobLoach’s picture

Status: Needs work » Closed (duplicate)