Download & Extend

Register Libraries in hook_library()

Project:Libraries API
Version:7.x-2.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:postponed

Issue Summary

Implement libraries_library(), so that modules that work with Libraries API can use their libraries (via drupal_add_library()) without having to implement their own hook_library().

<?php
/**
* Implement hook_libraries_info().
*/
function tinymce_libraries_info() {
 
$libraries['tinymce'] = array(
   
'files' => array(
     
'js' => 'tinymce.js',
     
'css' => 'tinymce.css',
    ),
  );
}

// Now to add TinyMCE's JS/CSS, you can do the following...
drupal_add_library('libraries', 'tinymce');
$form['myelement']['#attached'][] = array('libraries', 'tinymce');
?>

Comments

#1

The problem is that we can only register libraries that are installed. So we would have to detect all libraries in libraries_library(). That would currently be a performance hit, because we only cache libraries_load() currently.
Of course we could cache libraries_detect(), but I don't think that is necessary.
It is neither tested nor documented, but in theory we already support '#attached' anyway. So instead of your example, you can do:

<?php
/**
* Implement hook_libraries_info().
*/
function tinymce_libraries_info() {
 
$libraries['tinymce'] = array(
   
'files' => array(
     
'js' => 'tinymce.js',
     
'css' => 'tinymce.css',
    ),
  );
}

// Now to add TinyMCE's JS/CSS, you can do the following...
libraries_load('tinymce');
$form['myelement']['#attached']['libraries_load'][] = array('tinymce');
?>

#2

Status:active» postponed

Before attempting to do this, I want to see how generic something like this can be in the first place.

Based on the initial code for http://drupal.org/project/jquery, I somewhat doubt there's anything generic here.

Lastly, (I know I'm repeating myself ;) bear in mind that hook_library() is fundamentally different to Libraries API. The potential overlap with hook_library() only exists for 1:1 library-to-module relationships.