Migrating bootstrap_library module to Drupal 8.
Everything fine but external CDN libraries.
Follow documentation found but simply not working.
Tried also to alter libraries, neither working with external libraries.
Look all over Drupal core for similar code, but no success.
Help!!

CommentFileSizeAuthor
#19 bootstrap_library.patch2.33 KBdroplet
#16 library.patch1001 bytescilefen

Comments

hatuhay created an issue. See original summary.

dawehner’s picture

Do you point providing a code example?

hatuhay’s picture

Complete code on: https://www.drupal.org/project/bootstrap_library

bootstrap_library.libraries.yml

<?php
bootstrap:
  remote: http://getbootstrap.com/
  license:
    name: MIT
    gpl-compatible: false
  js:
    /libraries/bootstrap/js/bootstrap.min.js: { minified: true }
  css:
    theme:
      /libraries/bootstrap/css/bootstrap.min.css: {}
      /libraries/bootstrap/css/bootstrap-theme.min.css: {}
  dependencies:
    - core/jquery

bootstrap-dev:
  remote: http://getbootstrap.com/
  license:
    name: MIT
    gpl-compatible: false
  js:
    /libraries/bootstrap/js/bootstrap.js: { }
  css:
    theme:
      /libraries/bootstrap/css/bootstrap.css: {}
      /libraries/bootstrap/css/bootstrap-theme.css: {}
  dependencies:
    - core/jquery

bootstrap-cdn:
  remote: http://getbootstrap.com/
  license:
    name: MIT
    gpl-compatible: false
  js:
    //maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js: { type: external, minified: true }
  css:
    theme:
      //maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css: { type: external }
  dependencies:
    - core/jquery

?>

bootstrap_library.module

<?php
/**
 * Implements hook_page_attachments().
 */
function bootstrap_library_page_attachments(array &$page) {
  // Use Libraries API to load the js & css files into header

  if (drupal_installation_attempted()) {
    return; // Don't add the JavaScript and CSS during installation.
  }
  static $already_added = FALSE;
  if ($already_added) {
    return; // Don't add the JavaScript and CSS multiple times.
  }
  if (!_bootstrap_library_check_theme() || !_bootstrap_library_check_url()) {
    return; // Don't add the JavaScript and CSS on specified paths or themes.
  }
  $config = \Drupal::config('bootstrap_library.settings');
  $cdn = ($config->get('cdn.bootstrap'));
  if ($cdn) {
    $page['#attached']['library'][] = 'bootstrap_library/bootstrap-cdn';
  }
  else {
    $variant = ($config->get('minimized.options')) ? 'minified' : 'source';
    // Add and initialise bootstrap.
    if ($variant == 'minified') {
      $page['#attached']['library'][] = 'bootstrap_library/bootstrap';
    }
    else {
      $page['#attached']['library'][] = 'bootstrap_library/bootstrap-dev';
    }
  }
  $already_added = TRUE;
}

/**
 * Implements hook_library_info_alter().
 */
function bootstrap_library_library_info_alter(array &$libraries, $module) {
  if ($module === 'bootstrap_library' && isset($libraries['bootstrap'])) {
    $config = \Drupal::config('bootstrap_library.settings');
    $cdn = ($config->get('cdn.bootstrap'));
    if ($cdn) {
      $cdn = ($config->get('cdn.bootstrap'));
      $data = ($config->get('cdn.options'));
      $cdn_options =  json_decode( $data );
      $list = _bootstrap_library_object_to_array($cdn_options->bootstrap);  
      $libraries['bootstrap']['js'][0] = $list[$cdn]['js'];
    }
  }
}

?>
wim leers’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: -hook_page_attachments

simply not working is far too vague. Please provide proper steps to reproduce.

 static $already_added = FALSE;
  if ($already_added) {
    return; // Don't add the JavaScript and CSS multiple times.
  }

Also, this makes no sense at all. This is from a Drupal 7 era where we depended on global state. Just delete this.

  if (!_bootstrap_library_check_theme() || !_bootstrap_library_check_url()) {
    return; // Don't add the JavaScript and CSS on specified paths or themes.
  }

… if you do this, then you need to associate the appropriate cache tags and cache contexts, so that Drupal knows about these variations. See https://www.drupal.org/developing/api/8/cache/tags + https://www.drupal.org/developing/api/8/cache/contexts.

  $config = \Drupal::config('bootstrap_library.settings');

Similarly here, you need to associate the cacheability metadata of that configuration.

/**
 * Implements hook_library_info_alter().
 */

This should implement hook_library_info_build() instead.

hatuhay’s picture

Thanks for the quick response!!!
To reproduce the problem:
Install bootstrap_library on a clean Drupal 8 installation. Use DEV version http://ftp.drupal.org/files/projects/bootstrap_library-8.x-1.x-dev.tar.gz
Install local bootstrap on /libraries/bootstrap folder
There are three options for loading bootstrap libraries:

  1. Local minimized
  2. Local non minimized
  3. CDN

First two local versions load with no problem.
But CDN version does not load, it is not added to the javascript pool.
Tried two options, neither worked:

  • Load the CDN library declared on bootstrap_library.libraries.yml.
  • Declare the library via hook_library_info_build().
hatuhay’s picture

Status: Postponed (maintainer needs more info) » Active
droplet’s picture

Component: javascript » asset library system

hook_library_info_build doesn't work for me either. But hook_library_info_alter does the job. It declared test case for hook_library_info_build / hook_library_info_alter but doesn't verify test result?

https://github.com/drupal/drupal/blob/8.0.x/core/modules/system/tests/mo...

cilefen’s picture

Here is a working example of hook_library_info_build(): http://cgit.drupalcode.org/mathjax/tree/

At least, I hope it works.

hatuhay’s picture

Declare CDN libraries on libraries.yml and did not work, this is the declaration:

bootstrap-cdn:
  remote: http://getbootstrap.com/
  license:
    name: MIT
    gpl-compatible: false
  js:
    //maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js: { type: external, minified: true }
  css:
    theme:
      //maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css: { type: external }
  dependencies:
    - core/jquery

Is this a bug??

cilefen’s picture

What is the filename of that library file? That declaration will make a library named {whatever module machine name is}/bootstrap-cdn. Is that what you are attaching?

hatuhay’s picture

This is how I am calling the library.
$page['#attached']['library'][] = 'bootstrap_library/bootstrap-cdn';
Is this what you mean?
It works perfect with local libraries, but not for CDN

cilefen’s picture

Yes, it was what I meant, but that seems fine.

Have you tried with https:// instead of protocol-independent //, just to see?

cilefen’s picture

Regarding #5, what actually happens? If you disable JS aggregation, is anything JS file included? Are there any browser console 404 errors?

cilefen’s picture

I just cloned 8.x-1.x and hacked it a little so it loads only from the CDN and it totally works. It's loaded in the HTML.

<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

So I don't think the library file is the problem here.

hatuhay’s picture

JS aggregation is disable to check what is being loaded.
The file is not declared.
Tried also with http: included before, but as I said, file is not declared.
Installed mathjax and it is loading external files, compare codes and look the same.

cilefen’s picture

StatusFileSize
new1001 bytes

You must attach the library in or altering it will have no effect. This is a diff against 8.x-1.x.

hatuhay’s picture

Yes thanks for the patch, corrected what you mention with no success.
But did you get it to work?
I did not.

cilefen’s picture

With that patch in, if I pick a CDN version in the admin interface, the libraries are attached to the page on the CDN. The patch is not intended to really be committed, it is only a test.

droplet’s picture

StatusFileSize
new2.33 KB

Cool, working for me now. I think it should also call clean cache explicitly (or use hook_library_info_alter instead), or any other smart way?

hatuhay’s picture

Status: Active » Closed (fixed)

Yes!!!
Finally working.
Need to do a fresh installation, configuration was glued.
Clear cache several times with drush and no success.
Still an issue here I will address later.
Thanks you all guys.