libraries.api.php 22 KB

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