libraries.api.php 22 KB

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