Issue #1938638 by Pol: Fix typo in libraries.api.php.
[project/libraries.git] / libraries.api.php
1 <?php
2
3 /**
4 * @file
5 * Documents API functions for Libraries module.
6 */
7
8 /**
9 * Return information about external libraries.
10 *
11 * @return
12 * An associative array whose keys are internal names of libraries and whose
13 * values are describing each library. Each key is the directory name below
14 * the 'libraries' directory, in which the library may be found. Each value is
15 * an associative array containing:
16 * - name: The official, human-readable name of the library.
17 * - vendor url: The URL of the homepage of the library.
18 * - download url: The URL of a web page on which the library can be obtained.
19 * - path: (optional) A relative path from the directory of the library to the
20 * actual library. Only required if the extracted download package contains
21 * the actual library files in a sub-directory.
22 * - library path: (optional) The absolute path to the library directory. This
23 * should not be declared normally, as it is automatically detected, to
24 * allow for multiple possible library locations. A valid use-case is an
25 * external library, in which case the full URL to the library should be
26 * specified here.
27 * - version: (optional) The version of the library. This should not be
28 * declared normally, as it is automatically detected (see 'version
29 * callback' below) to allow for version changes of libraries without code
30 * changes of implementing modules and to support different versions of a
31 * library simultaneously (though only one version can be installed per
32 * site). A valid use-case is an external library whose version cannot be
33 * determined programatically.
34 * - version callback: (optional) The name of a function that detects and
35 * returns the full version string of the library. The first argument is
36 * always $library, an array containing all library information as described
37 * here. There are two ways to declare the version callback's additional
38 * arguments, either as a single $options parameter or as multiple
39 * parameters, which correspond to the two ways to specify the argument
40 * values (see 'version arguments'). Defaults to libraries_get_version().
41 * - version arguments: A list of arguments to pass to the version callback.
42 * Version arguments can be declared either as an associative array whose
43 * keys are the argument names or as an indexed array without specifying
44 * keys. If declared as an associative array, the arguments get passed to
45 * the version callback as a single $options parameter whose keys are the
46 * argument names (i.e. $options is identical to the specified array). If
47 * declared as an indexed array, the array values get passed to the version
48 * callback as seperate arguments in the order they were declared. The
49 * default version callback libraries_get_version() expects a single,
50 * associative array with named keys:
51 * - file: The filename to parse for the version, relative to the library
52 * path. For example: 'docs/changelog.txt'.
53 * - pattern: A string containing a regular expression (PCRE) to match the
54 * library version. For example: '@version\s+([0-9a-zA-Z\.-]+)@'. Note
55 * that the returned version is not the match of the entire pattern (i.e.
56 * '@version 1.2.3' in the above example) but the match of the first
57 * sub-pattern (i.e. '1.2.3' in the above example).
58 * - lines: (optional) The maximum number of lines to search the pattern in.
59 * Defaults to 20.
60 * - cols: (optional) The maximum number of characters per line to take into
61 * account. Defaults to 200. In case of minified or compressed files, this
62 * prevents reading the entire file into memory.
63 * - files: An associative array of library files to load. Supported keys are:
64 * - js: A list of JavaScript files to load, using the same syntax as Drupal
65 * core's hook_library().
66 * - css: A list of CSS files to load, using the same syntax as Drupal
67 * core's hook_library().
68 * - php: A list of PHP files to load.
69 * - dependencies: An array of libraries this library depends on. Similar to
70 * declaring module dependencies, the dependency declaration may contain
71 * information on the supported version. Examples of supported declarations:
72 * @code
73 * $libraries['dependencies'] = array(
74 * // Load the 'example' library, regardless of the version available:
75 * 'example',
76 * // Only load the 'example' library, if version 1.2 is available:
77 * 'example (1.2)',
78 * // Only load a version later than 1.3-beta2 of the 'example' library:
79 * 'example (>1.3-beta2)'
80 * // Only load a version equal to or later than 1.3-beta3:
81 * 'example (>=1.3-beta3)',
82 * // Only load a version earlier than 1.5:
83 * 'example (<1.5)',
84 * // Only load a version equal to or earlier than 1.4:
85 * 'example (<=1.4)',
86 * // Combinations of the above are allowed as well:
87 * 'example (>=1.3-beta2, <1.5)',
88 * );
89 * @endcode
90 * - variants: (optional) An associative array of available library variants.
91 * For example, the top-level 'files' property may refer to a default
92 * variant that is compressed. If the library also ships with a minified and
93 * uncompressed/source variant, those can be defined here. Each key should
94 * describe the variant type, e.g. 'minified' or 'source'. Each value is an
95 * associative array of top-level properties that are entirely overridden by
96 * the variant, most often just 'files'. Additionally, each variant can
97 * contain following properties:
98 * - variant callback: (optional) The name of a function that detects the
99 * variant and returns TRUE or FALSE, depending on whether the variant is
100 * available or not. The first argument is always $library, an array
101 * containing all library information as described here. The second
102 * argument is always a string containing the variant name. There are two
103 * ways to declare the variant callback's additinal arguments, either as a
104 * single $options parameter or as multiple parameters, which correspond
105 * to the two ways to specify the argument values (see 'variant
106 * arguments'). If ommitted, the variant is expected to always be
107 * available.
108 * - variant arguments: A list of arguments to pass to the variant callback.
109 * Variant arguments can be declared either as an associative array whose
110 * keys are the argument names or as an indexed array without specifying
111 * keys. If declared as an associative array, the arguments get passed to
112 * the variant callback as a single $options parameter whose keys are the
113 * argument names (i.e. $options is identical to the specified array). If
114 * declared as an indexed array, the array values get passed to the
115 * variant callback as seperate arguments in the order they were declared.
116 * Variants can be version-specific (see 'versions').
117 * - versions: (optional) An associative array of supported library versions.
118 * Naturally, libraries evolve over time and so do their APIs. In case a
119 * library changes between versions, different 'files' may need to be
120 * loaded, different 'variants' may become available, or Drupal modules need
121 * to load different integration files adapted to the new version. Each key
122 * is a version *string* (PHP does not support floats as keys). Each value
123 * is an associative array of top-level properties that are entirely
124 * overridden by the version.
125 * - integration files: (optional) An associative array whose keys are module
126 * names and whose values are sets of files to load for the module, using
127 * the same notion as the top-level 'files' property. Each specified file
128 * should contain the path to the file relative to the module it belongs to.
129 * - callbacks: An associative array whose keys are callback groups and whose
130 * values are arrays of callbacks to apply to the library in that group.
131 * Each callback receives the following arguments:
132 * - $library: An array of library information belonging to the top-level
133 * library, a specific version, a specific variant or a specific variant
134 * of a specific version. Because library information such as the 'files'
135 * property (see above) can be declared in all these different locations
136 * of the library array, but a callback may have to act on all these
137 * different parts of the library, it is called recursively for each
138 * library with a certain part of the libraries array passed as $library
139 * each time.
140 * - $version: If the $library array belongs to a certain version (see
141 * above), a string containing the version. This argument may be empty, so
142 * NULL should be specified as the default value.
143 * - $variant: If the $library array belongs to a certain variant (see
144 * above), a string containing the variant name. This argument may be
145 * empty, so NULL should be specified as the default value.
146 * Valid callback groups are:
147 * - info: Callbacks registered in this group are applied after the library
148 * information has been retrieved via hook_libraries_info() or info files.
149 * - pre-detect: Callbacks registered in this group are applied after the
150 * library path has been determined and before the version callback is
151 * invoked. At this point the following additional information is available:
152 * - $library['library path']: The path on the file system to the library.
153 * - post-detect: Callbacks registered in this group are applied after the
154 * library has been successfully detected. At this point the library
155 * contains the version-specific information, if specified, and following
156 * additional information is available:
157 * - $library['installed']: A boolean indicating whether the library is
158 * installed or not.
159 * - $library['version']: If it could be detected, a string containing the
160 * version of the library.
161 * - $library['variants'][$variant]['installed']: For each specified
162 * variant, a boolean indicating whether the variant is installed or
163 * not.
164 * Note that in this group the 'versions' property is no longer available.
165 * - pre-dependencies-load: Callbacks registered in this group are applied
166 * directly before the library's dependencies are loaded. At this point
167 * the library contains variant-specific information, if specified. Note
168 * that in this group the 'variants' property is no longer available.
169 * - pre-load: Callbacks registered in this group are applied directly after
170 * the library's dependencies are loaded and before the library itself is
171 * loaded.
172 * - post-load: Callbacks registered in this group are applied directly
173 * after this library is loaded. At this point, the library contains a
174 * 'loaded' key, which contains the number of files that were loaded.
175 * Additional top-level properties can be registered as needed.
176 *
177 * @see hook_library()
178 */
179 function hook_libraries_info() {
180 // The following is a full explanation of all properties. See below for more
181 // concrete example implementations.
182
183 // This array key lets Libraries API search for 'sites/all/libraries/example'
184 // directory, which should contain the entire, original extracted library.
185 $libraries['example'] = array(
186 // Only used in administrative UI of Libraries API.
187 'name' => 'Example library',
188 'vendor url' => 'http://example.com',
189 'download url' => 'http://example.com/download',
190 // Optional: If, after extraction, the actual library files are contained in
191 // 'sites/all/libraries/example/lib', specify the relative path here.
192 'path' => 'lib',
193 // Optional: Define a custom version detection callback, if required.
194 'version callback' => 'mymodule_get_version',
195 // Specify arguments for the version callback. By default,
196 // libraries_get_version() takes a named argument array:
197 'version arguments' => array(
198 'file' => 'docs/CHANGELOG.txt',
199 'pattern' => '@version\s+([0-9a-zA-Z\.-]+)@',
200 'lines' => 5,
201 'cols' => 20,
202 ),
203 // Default list of files of the library to load. Important: Only specify
204 // third-party files belonging to the library here, not integration files of
205 // your module.
206 'files' => array(
207 // 'js' and 'css' follow the syntax of hook_library(), but file paths are
208 // relative to the library path.
209 'js' => array(
210 'exlib.js',
211 'gadgets/foo.js',
212 ),
213 'css' => array(
214 'lib_style.css',
215 'skin/example.css',
216 ),
217 // For PHP libraries, specify include files here, still relative to the
218 // library path.
219 'php' => array(
220 'exlib.php',
221 'exlib.inc',
222 ),
223 ),
224 // Optional: Specify alternative variants of the library, if available.
225 'variants' => array(
226 // All properties defined for 'minified' override top-level properties.
227 'minified' => array(
228 'files' => array(
229 'js' => array(
230 'exlib.min.js',
231 'gadgets/foo.min.js',
232 ),
233 'css' => array(
234 'lib_style.css',
235 'skin/example.css',
236 ),
237 ),
238 'variant callback' => 'mymodule_check_variant',
239 'variant arguments' => array(
240 'variant' => 'minified',
241 ),
242 ),
243 ),
244 // Optional, but usually required: Override top-level properties for later
245 // versions of the library. The properties of the minimum version that is
246 // matched override the top-level properties. Note:
247 // - When registering 'versions', it usually does not make sense to register
248 // 'files', 'variants', and 'integration files' on the top-level, as most
249 // of those likely need to be different per version and there are no
250 // defaults.
251 // - The array keys have to be strings, as PHP does not support floats for
252 // array keys.
253 'versions' => array(
254 '2' => array(
255 'files' => array(
256 'js' => array('exlib.js'),
257 'css' => array('exlib_style.css'),
258 ),
259 ),
260 '3.0' => array(
261 'files' => array(
262 'js' => array('exlib.js'),
263 'css' => array('lib_style.css'),
264 ),
265 ),
266 '3.2' => array(
267 'files' => array(
268 'js' => array(
269 'exlib.js',
270 'gadgets/foo.js',
271 ),
272 'css' => array(
273 'lib_style.css',
274 'skin/example.css',
275 ),
276 ),
277 ),
278 ),
279 // Optional: Register files to auto-load for your module. All files must be
280 // keyed by module, and follow the syntax of the 'files' property.
281 'integration files' => array(
282 'mymodule' => array(
283 'js' => array('ex_lib.inc'),
284 ),
285 ),
286 // Optionally register callbacks to apply to the library during different
287 // stages of its lifetime ('callback groups').
288 'callbacks' => array(
289 // Used to alter the info associated with the library.
290 'info' => array(
291 'mymodule_example_libraries_info_callback',
292 ),
293 // Called before detecting the given library.
294 'pre-detect' => array(
295 'mymodule_example_libraries_predetect_callback',
296 ),
297 // Called after detecting the library.
298 'post-detect' => array(
299 'mymodule_example_libraries_postdetect_callback',
300 ),
301 // Called before the library's dependencies are loaded.
302 'pre-dependencies-load' => array(
303 'mymodule_example_libraries_pre_dependencies_load_callback',
304 ),
305 // Called before the library is loaded.
306 'pre-load' => array(
307 'mymodule_example_libraries_preload_callback',
308 ),
309 // Called after the library is loaded.
310 'post-load' => array(
311 'mymodule_example_libraries_postload_callback',
312 ),
313 ),
314 );
315
316 // A very simple library. No changing APIs (hence, no versions), no variants.
317 // Expected to be extracted into 'sites/all/libraries/simple'.
318 $libraries['simple'] = array(
319 'name' => 'Simple library',
320 'vendor url' => 'http://example.com/simple',
321 'download url' => 'http://example.com/simple',
322 'version arguments' => array(
323 'file' => 'readme.txt',
324 // Best practice: Document the actual version strings for later reference.
325 // 1.x: Version 1.0
326 'pattern' => '/Version (\d+)/',
327 'lines' => 5,
328 ),
329 'files' => array(
330 'js' => array('simple.js'),
331 ),
332 );
333
334 // A library that (naturally) evolves over time with API changes.
335 $libraries['tinymce'] = array(
336 'name' => 'TinyMCE',
337 'vendor url' => 'http://tinymce.moxiecode.com',
338 'download url' => 'http://tinymce.moxiecode.com/download.php',
339 'path' => 'jscripts/tiny_mce',
340 // The regular expression catches two parts (the major and the minor
341 // version), which libraries_get_version() doesn't allow.
342 'version callback' => 'tinymce_get_version',
343 'version arguments' => array(
344 // It can be easier to parse the first characters of a minified file
345 // instead of doing a multi-line pattern matching in a source file. See
346 // 'lines' and 'cols' below.
347 'file' => 'jscripts/tiny_mce/tiny_mce.js',
348 // Best practice: Document the actual version strings for later reference.
349 // 2.x: this.majorVersion="2";this.minorVersion="1.3"
350 // 3.x: majorVersion:'3',minorVersion:'2.0.1'
351 'pattern' => '@majorVersion[=:]["\'](\d).+?minorVersion[=:]["\']([\d\.]+)@',
352 'lines' => 1,
353 'cols' => 100,
354 ),
355 'versions' => array(
356 '2.1' => array(
357 'files' => array(
358 'js' => array('tiny_mce.js'),
359 ),
360 'variants' => array(
361 'source' => array(
362 'files' => array(
363 'js' => array('tiny_mce_src.js'),
364 ),
365 ),
366 ),
367 'integration files' => array(
368 'wysiwyg' => array(
369 'js' => array('editors/js/tinymce-2.js'),
370 'css' => array('editors/js/tinymce-2.css'),
371 ),
372 ),
373 ),
374 // Definition used if 3.1 or above is detected.
375 '3.1' => array(
376 // Does not support JS aggregation.
377 'files' => array(
378 'js' => array(
379 'tiny_mce.js' => array('preprocess' => FALSE),
380 ),
381 ),
382 'variants' => array(
383 // New variant leveraging jQuery. Not stable yet; therefore not the
384 // default variant.
385 'jquery' => array(
386 'files' => array(
387 'js' => array(
388 'tiny_mce_jquery.js' => array('preprocess' => FALSE),
389 ),
390 ),
391 ),
392 'source' => array(
393 'files' => array(
394 'js' => array(
395 'tiny_mce_src.js' => array('preprocess' => FALSE),
396 ),
397 ),
398 ),
399 ),
400 'integration files' => array(
401 'wysiwyg' => array(
402 'js' => array('editors/js/tinymce-3.js'),
403 'css' => array('editors/js/tinymce-3.css'),
404 ),
405 ),
406 ),
407 ),
408 );
409 return $libraries;
410 }
411
412 /**
413 * Alter the library information before detection and caching takes place.
414 *
415 * The library definitions are passed by reference. A common use-case is adding
416 * a module's integration files to the library array, so that the files are
417 * loaded whenever the library is. As noted above, it is important to declare
418 * integration files inside of an array, whose key is the module name.
419 *
420 * @see hook_libraries_info()
421 */
422 function hook_libraries_info_alter(&$libraries) {
423 $files = array(
424 'php' => array('example_module.php_spellchecker.inc'),
425 );
426 $libraries['php_spellchecker']['integration files']['example_module'] = $files;
427 }
428
429 /**
430 * Specify paths to look for library info files.
431 *
432 * Libraries API looks in the following directories for library info files by
433 * default:
434 * - libraries
435 * - profiles/$profile/libraries
436 * - sites/all/libraries
437 * - sites/$site/libraries
438 * This hook allows you to specify additional locations to look for library info
439 * files. This should only be used for modules that declare many libraries.
440 * Modules that only implement a few libraries should implement
441 * hook_libraries_info().
442 *
443 * @return
444 * An array of paths.
445 */
446 function hook_libraries_paths() {
447 // Taken from the Libraries test module, which needs to specify the path to
448 // the test library.
449 return array(drupal_get_path('module', 'libraries_test') . '/example');
450 }