libraries.api.php 20 KB

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