Hi,

is it possible to build a field in the UI, where a link to a CDN could be inserted, from where the colorbox library is fetched? This field could be prefilled as a fallback if the library isn't downloaded to /libraries directory.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Eric Heydrich created an issue. See original summary.

renatog’s picture

Assigned: Unassigned » renatog
renatog’s picture

Status: Active » Fixed
Issue tags: +ciandt-contrib

Hi Eric how are you?

At this time not exists this feature, but is really interesting. If you need can report a Feature Request with it for us.

If you can send us a patch.

If you can't create patch, just create a Feature Request with your idea. It will help a lot and us work for create.

Thank you very much Eric.

Good Work.

Regards.

Eric Heydrich’s picture

Status: Fixed » Active

Hi Renato,

I tried something, but can't achieve to provide the CDN-Link through the UI. So I done something different:

1. I added a checkbox and configuration in colorbox settings for choosing between CDN or Local Files
2. I added a library colorbox-cdn to colorbox.libraries.yml, which points to the CDN URL
3. I added a condition to ColorboxAttachment.php which checks if CDN source is chosen and to provide library from colorbox-cdn or colorbox or colorbox-dev
4. I added default config library_source: 0 in config/install/colorbox.settings.yml to always provide CDN link as default after installation
5. I changed the Error message on Status Report (maybe this could then be just a warning)

ColorboxSettingsForm.php

in buildForm()-function

$form['colorbox_advanced_settings']['colorbox_library_source'] = [
      '#type' => 'radios',
      '#title' => $this->t('Library Source'),
      '#options' => [0 => $this->t('CDN'), 1 => $this->t('Local directory')],
      '#default_value' => $config->get('advanced.library_source'),
      '#description' => $this->t('Use a CDN or download the files to libraries directory.'),
    ];

in submitForm()-function

->set('advanced.library_source', $form_state->getValue('colorbox_library_source'))

ColorboxAttachment.php

in attach()-function

if ($this->settings->get('advanced.library_source') == 0){
        $page['#attached']['library'][] = 'colorbox/colorbox-cdn';
    }
    else{
        // Add and initialise the Colorbox plugin.
        if ($this->settings->get('advanced.compression_type') == 'minified') {
            $page['#attached']['library'][] = 'colorbox/colorbox';
        }
        else {
            $page['#attached']['library'][] = 'colorbox/colorbox-dev';
        }
    }

colorbox.libraries.yml

colorbox-cdn:
  remote: http://github.com/jackmoore/colorbox
  version: VERSION
  license:
    name: MIT
    gpl-compatible: false
  js:
    https://cdnjs.cloudflare.com/ajax/libs/jquery.colorbox/1.6.4/jquery.colorbox-min.js: { external: true, minified: true}
  dependencies:
    - core/jquery
    - core/jquery.once

config/install/colorbox.settings.yml

advanced:
  library_source: 0

colorbox.install

function colorbox_requirements($phase) {
  if ($phase != 'runtime') {
    return array();
  }

  $library = \Drupal::service('library.discovery')->getLibraryByName('colorbox', 'colorbox');
  $library_exists = file_exists(DRUPAL_ROOT . '/' . $library['js'][0]['data']);

  return [
    'colorbox_library_downloaded' => [
      'title' => t('Colorbox library'),
      'value' => $library_exists ? t('Installed') : t('Not installed'),
      'description' => $library_exists ? '' : t('The Colorbox library needs to be <a href="@url">downloaded</a> and extracted into the /libraries/colorbox folder in your Drupal installation directory. You can also use the CDN setting for fetching the library from an other server.', ['@url' => 'https://github.com/jackmoore/colorbox/archive/master.zip']),
      'severity' => $library_exists ? REQUIREMENT_OK : REQUIREMENT_ERROR,
    ],
  ];
}

Would like to hear your opinion about the changes.

Anonymous’s picture

Assigned: renatog »
Anonymous’s picture

Thanks for the suggestions Eric.

I added the Library Source setting in Advanced Settings.

However I ended up using a slightly different implementation of the library, cleaning some things up to not have different library definions based on settings, but just alter the js source based on the options, allowing other modules to include colorbox/colorbox and have the module's settings still applied.

Neslee Canil Pinto’s picture

Status: Needs review » Closed (works as designed)