Projet

Général

Profil

Paste
Télécharger (22,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / libraries / libraries.api.php @ 1e39edcb

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
 *   - download file url: (optional) The URL where the latest version of the
20
 *     library can be downloaded. In case such a static URL exists the library
21
 *     can be downloaded automatically via Drush. Run
22
 *     'drush help libraries-download' in the command-line for more information.
23
 *   - path: (optional) A relative path from the directory of the library to the
24
 *     actual library. Only required if the extracted download package contains
25
 *     the actual library files in a sub-directory.
26
 *   - library path: (optional) The absolute path to the library directory. This
27
 *     should not be declared normally, as it is automatically detected, to
28
 *     allow for multiple possible library locations. A valid use-case is an
29
 *     external library, in which case the full URL to the library should be
30
 *     specified here.
31
 *   - version: (optional) The version of the library. This should not be
32
 *     declared normally, as it is automatically detected (see 'version
33
 *     callback' below) to allow for version changes of libraries without code
34
 *     changes of implementing modules and to support different versions of a
35
 *     library simultaneously (though only one version can be installed per
36
 *     site). A valid use-case is an external library whose version cannot be
37
 *     determined programmatically. Either 'version' or 'version callback' (or
38
 *     'version arguments' in case libraries_get_version() is being used as a
39
 *     version callback) must be declared.
40
 *   - version callback: (optional) The name of a function that detects and
41
 *     returns the full version string of the library. The first argument is
42
 *     always $library, an array containing all library information as described
43
 *     here. There are two ways to declare the version callback's additional
44
 *     arguments, either as a single $options parameter or as multiple
45
 *     parameters, which correspond to the two ways to specify the argument
46
 *     values (see 'version arguments'). Defaults to libraries_get_version().
47
 *     Unless 'version' is declared or libraries_get_version() is being used as
48
 *     a version callback, 'version callback' must be declared. In the latter
49
 *     case, however, 'version arguments' must be declared in the specified way.
50
 *   - version arguments: (optional) A list of arguments to pass to the version
51
 *     callback. Version arguments can be declared either as an associative
52
 *     array whose keys are the argument names or as an indexed array without
53
 *     specifying keys. If declared as an associative array, the arguments get
54
 *     passed to the version callback as a single $options parameter whose keys
55
 *     are the argument names (i.e. $options is identical to the specified
56
 *     array). If declared as an indexed array, the array values get passed to
57
 *     the version callback as separate arguments in the order they were
58
 *     declared. The default version callback libraries_get_version() expects a
59
 *     single, associative array with named keys:
60
 *     - file: The filename to parse for the version, relative to the path
61
 *       speficied as the 'library path' property (see above). For example:
62
 *       'docs/changelog.txt'.
63
 *     - pattern: A string containing a regular expression (PCRE) to match the
64
 *       library version. For example: '@version\s+([0-9a-zA-Z\.-]+)@'. Note
65
 *       that the returned version is not the match of the entire pattern (i.e.
66
 *       '@version 1.2.3' in the above example) but the match of the first
67
 *       sub-pattern (i.e. '1.2.3' in the above example).
68
 *     - lines: (optional) The maximum number of lines to search the pattern in.
69
 *       Defaults to 20.
70
 *     - cols: (optional) The maximum number of characters per line to take into
71
 *       account. Defaults to 200. In case of minified or compressed files, this
72
 *       prevents reading the entire file into memory.
73
 *     Defaults to an empty array. 'version arguments' must be specified unless
74
 *     'version' is declared or the specified 'version callback' does not
75
 *     require any arguments. The latter might be the case with a
76
 *     library-specific version callback, for example.
77
 *   - files: An associative array of library files to load. Supported keys are:
78
 *     - js: A list of JavaScript files to load, using the same syntax as Drupal
79
 *       core's hook_library().
80
 *     - css: A list of CSS files to load, using the same syntax as Drupal
81
 *       core's hook_library().
82
 *     - php: A list of PHP files to load.
83
 *   - dependencies: An array of libraries this library depends on. Similar to
84
 *     declaring module dependencies, the dependency declaration may contain
85
 *     information on the supported version. Examples of supported declarations:
86
 *     @code
87
 *     $libraries['dependencies'] = array(
88
 *       // Load the 'example' library, regardless of the version available:
89
 *       'example',
90
 *       // Only load the 'example' library, if version 1.2 is available:
91
 *       'example (1.2)',
92
 *       // Only load a version later than 1.3-beta2 of the 'example' library:
93
 *       'example (>1.3-beta2)'
94
 *       // Only load a version equal to or later than 1.3-beta3:
95
 *       'example (>=1.3-beta3)',
96
 *       // Only load a version earlier than 1.5:
97
 *       'example (<1.5)',
98
 *       // Only load a version equal to or earlier than 1.4:
99
 *       'example (<=1.4)',
100
 *       // Combinations of the above are allowed as well:
101
 *       'example (>=1.3-beta2, <1.5)',
102
 *     );
103
 *     @endcode
104
 *   - variants: (optional) An associative array of available library variants.
105
 *     For example, the top-level 'files' property may refer to a default
106
 *     variant that is compressed. If the library also ships with a minified and
107
 *     uncompressed/source variant, those can be defined here. Each key should
108
 *     describe the variant type, e.g. 'minified' or 'source'. Each value is an
109
 *     associative array of top-level properties that are entirely overridden by
110
 *     the variant, most often just 'files'. Additionally, each variant can
111
 *     contain following properties:
112
 *     - variant callback: (optional) The name of a function that detects the
113
 *       variant and returns TRUE or FALSE, depending on whether the variant is
114
 *       available or not. The first argument is always $library, an array
115
 *       containing all library information as described here. The second
116
 *       argument is always a string containing the variant name. There are two
117
 *       ways to declare the variant callback's additional arguments, either as a
118
 *       single $options parameter or as multiple parameters, which correspond
119
 *       to the two ways to specify the argument values (see 'variant
120
 *       arguments'). If omitted, the variant is expected to always be
121
 *       available.
122
 *     - variant arguments: A list of arguments to pass to the variant callback.
123
 *       Variant arguments can be declared either as an associative array whose
124
 *       keys are the argument names or as an indexed array without specifying
125
 *       keys. If declared as an associative array, the arguments get passed to
126
 *       the variant callback as a single $options parameter whose keys are the
127
 *       argument names (i.e. $options is identical to the specified array). If
128
 *       declared as an indexed array, the array values get passed to the
129
 *       variant callback as separate arguments in the order they were declared.
130
 *     Variants can be version-specific (see 'versions').
131
 *   - versions: (optional) An associative array of supported library versions.
132
 *     Naturally, libraries evolve over time and so do their APIs. In case a
133
 *     library changes between versions, different 'files' may need to be
134
 *     loaded, different 'variants' may become available, or Drupal modules need
135
 *     to load different integration files adapted to the new version. Each key
136
 *     is a version *string* (PHP does not support floats as keys). Each value
137
 *     is an associative array of top-level properties that are entirely
138
 *     overridden by the version.
139
 *   - integration files: (optional) An associative array whose keys are module
140
 *     names and whose values are sets of files to load for the module, using
141
 *     the same notion as the top-level 'files' property. Each specified file
142
 *     should contain the path to the file relative to the module it belongs to.
143
 *   - callbacks: An associative array whose keys are callback groups and whose
144
 *     values are arrays of callbacks to apply to the library in that group.
145
 *     Each callback receives the following arguments:
146
 *     - $library: An array of library information belonging to the top-level
147
 *       library, a specific version, a specific variant or a specific variant
148
 *       of a specific version. Because library information such as the 'files'
149
 *       property (see above) can be declared in all these different locations
150
 *       of the library array, but a callback may have to act on all these
151
 *       different parts of the library, it is called recursively for each
152
 *       library with a certain part of the libraries array passed as $library
153
 *       each time.
154
 *     - $version: If the $library array belongs to a certain version (see
155
 *       above), a string containing the version. This argument may be empty, so
156
 *       NULL should be specified as the default value.
157
 *     - $variant: If the $library array belongs to a certain variant (see
158
 *       above), a string containing the variant name. This argument may be
159
 *       empty, so NULL should be specified as the default value.
160
 *     Valid callback groups are:
161
 *     - info: Callbacks registered in this group are applied after the library
162
 *       information has been retrieved via hook_libraries_info() or info files.
163
 *       At this point the following additional information is available:
164
 *       - $library['info type']: How the library information was obtained. Can
165
 *         be 'info file', 'module', or 'theme', depending on whether the
166
 *         library information was obtained from an info file, an enabled module
167
 *         or an enabled theme, respectively.
168
 *       Additionally, one of the following three keys is available, depending
169
 *       on the value of $library['info type'].
170
 *       - $library['info file']: In case the library information was obtained
171
 *         from an info file, the URI of the info file.
172
 *       - $library['module']: In case the library was obtained from an enabled
173
 *         module, the name of the providing module.
174
 *       - $library['theme']: In case the library was obtained from an enabled
175
 *         theme, the name of the providing theme.
176
 *     - pre-detect: Callbacks registered in this group are applied after the
177
 *       library path has been determined and before the version callback is
178
 *       invoked. At this point the following additional information is
179
 *       available:
180
 *       - $library['library path']: The path on the file system to the library.
181
 *     - post-detect: Callbacks registered in this group are applied after the
182
 *       library has been successfully detected. At this point the library
183
 *       contains the version-specific information, if specified, and following
184
 *       additional information is available:
185
 *       - $library['installed']: A boolean indicating whether the library is
186
 *         installed or not.
187
 *       - $library['version']: If it could be detected, a string containing the
188
 *         version of the library.
189
 *       - $library['variants'][$variant]['installed']: For each specified
190
 *         variant, a boolean indicating whether the variant is installed or
191
 *         not.
192
 *       Note that in this group the 'versions' property is no longer available.
193
 *     - pre-dependencies-load: Callbacks registered in this group are applied
194
 *       directly before the library's dependencies are loaded. At this point
195
 *       the library contains variant-specific information, if specified. Note
196
 *       that in this group the 'variants' property is no longer available.
197
 *     - pre-load: Callbacks registered in this group are applied directly after
198
 *       the library's dependencies are loaded and before the library itself is
199
 *       loaded.
200
 *     - post-load: Callbacks registered in this group are applied directly
201
 *       after this library is loaded. At this point, the library contains a
202
 *       'loaded' key, which contains the number of files that were loaded.
203
 *   Additional top-level properties can be registered as needed.
204
 *
205
 * @see hook_library()
206
 */
207
function hook_libraries_info() {
208
  // The following is a full explanation of all properties. See below for more
209
  // concrete example implementations.
210

    
211
  // This array key lets Libraries API search for 'sites/all/libraries/example'
212
  // directory, which should contain the entire, original extracted library.
213
  $libraries['example'] = array(
214
    // Only used in administrative UI of Libraries API.
215
    'name' => 'Example library',
216
    'vendor url' => 'http://example.com',
217
    'download url' => 'http://example.com/download',
218
    // It is important that this URL does not include the actual version to
219
    // download. Not all libraries provide such a static URL.
220
    'download file url' => 'http://example.com/latest.tar.gz',
221
    // Optional: If, after extraction, the actual library files are contained in
222
    // 'sites/all/libraries/example/lib', specify the relative path here.
223
    'path' => 'lib',
224
    // Optional: Define a custom version detection callback, if required.
225
    'version callback' => 'mymodule_get_version',
226
    // Specify arguments for the version callback. By default,
227
    // libraries_get_version() takes a named argument array:
228
    'version arguments' => array(
229
      'file' => 'docs/CHANGELOG.txt',
230
      'pattern' => '@version\s+([0-9a-zA-Z\.-]+)@',
231
      'lines' => 5,
232
      'cols' => 20,
233
    ),
234
    // Default list of files of the library to load. Important: Only specify
235
    // third-party files belonging to the library here, not integration files of
236
    // your module.
237
    'files' => array(
238
      // 'js' and 'css' follow the syntax of hook_library(), but file paths are
239
      // relative to the library path.
240
      'js' => array(
241
        'exlib.js',
242
        'gadgets/foo.js',
243
      ),
244
      'css' => array(
245
        'lib_style.css',
246
        'skin/example.css',
247
      ),
248
      // For PHP libraries, specify include files here, still relative to the
249
      // library path.
250
      'php' => array(
251
        'exlib.php',
252
        'exlib.inc',
253
      ),
254
    ),
255
    // Optional: Specify alternative variants of the library, if available.
256
    'variants' => array(
257
      // All properties defined for 'minified' override top-level properties.
258
      'minified' => array(
259
        'files' => array(
260
          'js' => array(
261
            'exlib.min.js',
262
            'gadgets/foo.min.js',
263
          ),
264
          'css' => array(
265
            'lib_style.css',
266
            'skin/example.css',
267
          ),
268
        ),
269
        'variant callback' => 'mymodule_check_variant',
270
        'variant arguments' => array(
271
          'variant' => 'minified',
272
        ),
273
      ),
274
    ),
275
    // Optional, but usually required: Override top-level properties for later
276
    // versions of the library. The properties of the minimum version that is
277
    // matched override the top-level properties. Note:
278
    // - When registering 'versions', it usually does not make sense to register
279
    //   'files', 'variants', and 'integration files' on the top-level, as most
280
    //   of those likely need to be different per version and there are no
281
    //   defaults.
282
    // - The array keys have to be strings, as PHP does not support floats for
283
    //   array keys.
284
    'versions' => array(
285
      '2' => array(
286
        'files' => array(
287
          'js' => array('exlib.js'),
288
          'css' => array('exlib_style.css'),
289
        ),
290
      ),
291
      '3.0' => array(
292
        'files' => array(
293
          'js' => array('exlib.js'),
294
          'css' => array('lib_style.css'),
295
        ),
296
      ),
297
      '3.2' => array(
298
        'files' => array(
299
          'js' => array(
300
            'exlib.js',
301
            'gadgets/foo.js',
302
          ),
303
          'css' => array(
304
            'lib_style.css',
305
            'skin/example.css',
306
          ),
307
        ),
308
      ),
309
    ),
310
    // Optional: Register files to auto-load for your module. All files must be
311
    // keyed by module, and follow the syntax of the 'files' property.
312
    'integration files' => array(
313
      'mymodule' => array(
314
        'js' => array('ex_lib.inc'),
315
      ),
316
    ),
317
    // Optionally register callbacks to apply to the library during different
318
    // stages of its lifetime ('callback groups').
319
    'callbacks' => array(
320
      // Used to alter the info associated with the library.
321
      'info' => array(
322
        'mymodule_example_libraries_info_callback',
323
      ),
324
      // Called before detecting the given library.
325
      'pre-detect' => array(
326
        'mymodule_example_libraries_predetect_callback',
327
      ),
328
      // Called after detecting the library.
329
      'post-detect' => array(
330
        'mymodule_example_libraries_postdetect_callback',
331
      ),
332
      // Called before the library's dependencies are loaded.
333
      'pre-dependencies-load' => array(
334
        'mymodule_example_libraries_pre_dependencies_load_callback',
335
      ),
336
      // Called before the library is loaded.
337
      'pre-load' => array(
338
        'mymodule_example_libraries_preload_callback',
339
      ),
340
      // Called after the library is loaded.
341
      'post-load' => array(
342
        'mymodule_example_libraries_postload_callback',
343
      ),
344
    ),
345
  );
346

    
347
  // A very simple library. No changing APIs (hence, no versions), no variants.
348
  // Expected to be extracted into 'sites/all/libraries/simple'.
349
  $libraries['simple'] = array(
350
    'name' => 'Simple library',
351
    'vendor url' => 'http://example.com/simple',
352
    'download url' => 'http://example.com/simple',
353
    // The download file URL can also point to a single file (instead of an
354
    // archive).
355
    'download file url' => 'http://example.com/latest/simple.js',
356
    'version arguments' => array(
357
      'file' => 'readme.txt',
358
      // Best practice: Document the actual version strings for later reference.
359
      // 1.x: Version 1.0
360
      'pattern' => '/Version (\d+)/',
361
      'lines' => 5,
362
    ),
363
    'files' => array(
364
      'js' => array('simple.js'),
365
    ),
366
  );
367

    
368
  // A library that (naturally) evolves over time with API changes.
369
  $libraries['tinymce'] = array(
370
    'name' => 'TinyMCE',
371
    'vendor url' => 'http://tinymce.moxiecode.com',
372
    'download url' => 'http://tinymce.moxiecode.com/download.php',
373
    'path' => 'jscripts/tiny_mce',
374
    // The regular expression catches two parts (the major and the minor
375
    // version), which libraries_get_version() doesn't allow.
376
    'version callback' => 'tinymce_get_version',
377
    'version arguments' => array(
378
      // It can be easier to parse the first characters of a minified file
379
      // instead of doing a multi-line pattern matching in a source file. See
380
      // 'lines' and 'cols' below.
381
      'file' => 'jscripts/tiny_mce/tiny_mce.js',
382
      // Best practice: Document the actual version strings for later reference.
383
      // 2.x: this.majorVersion="2";this.minorVersion="1.3"
384
      // 3.x: majorVersion:'3',minorVersion:'2.0.1'
385
      'pattern' => '@majorVersion[=:]["\'](\d).+?minorVersion[=:]["\']([\d\.]+)@',
386
      'lines' => 1,
387
      'cols' => 100,
388
    ),
389
    'versions' => array(
390
      '2.1' => array(
391
        'files' => array(
392
          'js' => array('tiny_mce.js'),
393
        ),
394
        'variants' => array(
395
          'source' => array(
396
            'files' => array(
397
              'js' => array('tiny_mce_src.js'),
398
            ),
399
          ),
400
        ),
401
        'integration files' => array(
402
          'wysiwyg' => array(
403
            'js' => array('editors/js/tinymce-2.js'),
404
            'css' => array('editors/js/tinymce-2.css'),
405
          ),
406
        ),
407
      ),
408
      // Definition used if 3.1 or above is detected.
409
      '3.1' => array(
410
        // Does not support JS aggregation.
411
        'files' => array(
412
          'js' => array(
413
            'tiny_mce.js' => array('preprocess' => FALSE),
414
          ),
415
        ),
416
        'variants' => array(
417
          // New variant leveraging jQuery. Not stable yet; therefore not the
418
          // default variant.
419
          'jquery' => array(
420
            'files' => array(
421
              'js' => array(
422
                'tiny_mce_jquery.js' => array('preprocess' => FALSE),
423
              ),
424
            ),
425
          ),
426
          'source' => array(
427
            'files' => array(
428
              'js' => array(
429
                'tiny_mce_src.js' => array('preprocess' => FALSE),
430
              ),
431
            ),
432
          ),
433
        ),
434
        'integration files' => array(
435
          'wysiwyg' => array(
436
            'js' => array('editors/js/tinymce-3.js'),
437
            'css' => array('editors/js/tinymce-3.css'),
438
          ),
439
        ),
440
      ),
441
    ),
442
  );
443
  return $libraries;
444
}
445

    
446
/**
447
 * Alter the library information before detection and caching takes place.
448
 *
449
 * The library definitions are passed by reference. A common use-case is adding
450
 * a module's integration files to the library array, so that the files are
451
 * loaded whenever the library is. As noted above, it is important to declare
452
 * integration files inside of an array, whose key is the module name.
453
 *
454
 * @see hook_libraries_info()
455
 */
456
function hook_libraries_info_alter(&$libraries) {
457
  $files = array(
458
    'php' => array('example_module.php_spellchecker.inc'),
459
  );
460
  $libraries['php_spellchecker']['integration files']['example_module'] = $files;
461
}
462

    
463
/**
464
 * Specify paths to look for library info files.
465
 *
466
 * Libraries API looks in the following directories for library info files by
467
 * default:
468
 * - libraries
469
 * - profiles/$profile/libraries
470
 * - sites/all/libraries
471
 * - sites/$site/libraries
472
 * This hook allows you to specify additional locations to look for library info
473
 * files. This should only be used for modules that declare many libraries.
474
 * Modules that only implement a few libraries should implement
475
 * hook_libraries_info().
476
 *
477
 * @return
478
 *   An array of paths.
479
 */
480
function hook_libraries_info_file_paths() {
481
  // Taken from the Libraries test module, which needs to specify the path to
482
  // the test library.
483
  return array(drupal_get_path('module', 'libraries_test') . '/example');
484
}