3

Reading the 2 articles below, I'm creating a new theme.

In the example below, I don't understand what is the purpose of the version property of the global-styling library :

global-styling:
  version: 1.x
  css:
    theme:
      css/layout.css: {}
      css/style.css: {}
      css/colors.css: {}
      css/print.css: { media: print }

Is it a reference to the site version with which this library is compatible ?

Is it the version of the library itself ? And in this case, when will it be used and for what ?

4
  • I usually write VERSION
    – Eyal
    Commented Aug 22, 2017 at 10:36
  • Ok. But what that is mean if you write VERSION ?
    – Eria
    Commented Aug 22, 2017 at 10:55
  • I think that VERSION is replaced with the module/theme version when the module is downloaded from drupal.org
    – Eyal
    Commented Aug 22, 2017 at 10:56
  • 1
    @Eyal VERSION is used only from Drupal core modules. Third-party modules should not use it.
    – apaderno
    Commented Aug 22, 2017 at 11:03

1 Answer 1

3

For Javascript files, version would allow modules that implement hook_library_info_alter() to check if the version of the JavaScript file is older than the one the module is trying to add, for example with code similar to the following. (It's the same code the documentation page reports as example.)

  // Update Farbtastic to version 2.0.
  if ($extension == 'core' && isset($libraries['jquery.farbtastic'])) {
    // Verify existing version is older than the one we are updating to.
    if (version_compare($libraries['jquery.farbtastic']['version'], '2.0', '<')) {
      // Update the existing Farbtastic to version 2.0.
      $libraries['jquery.farbtastic']['version'] = '2.0';
      // To accurately replace library files, the order of files and the options
      // of each file have to be retained; e.g., like this:
      $old_path = 'assets/vendor/farbtastic';
      // Since the replaced library files are no longer located in a directory
      // relative to the original extension, specify an absolute path (relative
      // to DRUPAL_ROOT / base_path()) to the new location.
      $new_path = '/' . drupal_get_path('module', 'farbtastic_update') . '/js';
      $new_js = [];
      $replacements = [
        $old_path . '/farbtastic.js' => $new_path . '/farbtastic-2.0.js',
      ];
      foreach ($libraries['jquery.farbtastic']['js'] as $source => $options) {
        if (isset($replacements[$source])) {
          $new_js[$replacements[$source]] = $options;
        }
        else {
          $new_js[$source] = $options;
        }
      }
      $libraries['jquery.farbtastic']['js'] = $new_js;
    }
  }

Technically, the same type of code could be used to check which CSS file version is being used, before replacing it with a different one. I can imagine doing that for an external CSS file, for example when a specific version used from a library causes problem with other libraries. I haven't seen a module using similar code, though.

Otherwise, version has only identification purposes, much more helpful when a module implements hook_library_info_build() to dynamically load different versions of a CSS file.

3
  • So I understand the version is usefull only if you write some custom code in a hook to use it. But, by default, it must be used in some way, is it ?
    – Eria
    Commented Aug 24, 2017 at 8:01
  • You can use it to check what version of the CSS/JavaScript files are being used. That's what I meant by identification purposes.
    – apaderno
    Commented Aug 24, 2017 at 9:05
  • Is it displayed somewhere on the admin UI ? Or do you have to open the info file everytime ?
    – Eria
    Commented Aug 24, 2017 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.