diff --git a/colorbox.api.php b/colorbox.api.php index 4fda7ee..47e37cd 100644 --- a/colorbox.api.php +++ b/colorbox.api.php @@ -31,3 +31,19 @@ function hook_colorbox_settings_alter(&$settings, &$style) { $style = 'mystyle'; } } + +/** + * Provides the ability to add new colorbox styles. + * + * The key for you styles should be in the format of module_name/library_name, + * where library_name is the name of your stylesheet in your libraries.yml file. + * + * If the style isn't defined properly, it will not be listed or added. + * + * Implements hook_colorbox_styles(). + */ +function hook_colorbox_styles() { + return [ + 'mymodule/library_name' => 'Style Name', + ]; +} diff --git a/colorbox.install b/colorbox.install index 50c69cd..4d48ce4 100644 --- a/colorbox.install +++ b/colorbox.install @@ -37,3 +37,15 @@ function colorbox_requirements($phase) { ], ]; } + +/** + * Change the colorbox.settings.style variable to be module/library format. + */ +function colorbox_update_8001() { + $config = \Drupal::configFactory()->getEditable('colorbox.settings'); + $colorbox_style = $config->get('custom.style'); + // Add colorbox prefix if needed, change none to empty string. + $new_style = $colorbox_style != 'none' ? 'colorbox/' . $colorbox_style : ''; + $config->set('custom.style', $new_style); + $config->save(); +} diff --git a/colorbox.theme.inc b/colorbox.theme.inc index 2e58fe6..2aa06f1 100644 --- a/colorbox.theme.inc +++ b/colorbox.theme.inc @@ -83,7 +83,7 @@ function template_preprocess_colorbox_formatter(&$variables) { // Shorten the caption for the example styles or when caption // shortening is active. $config = \Drupal::config('colorbox.settings'); - $colorbox_style = $config->get('colorbox_style'); + $colorbox_style = $config->get('custom.style'); $trim_length = $config->get('colorbox_caption_trim_length'); if (((strpos($colorbox_style, 'colorbox/example') !== FALSE) || $config->get('colorbox_caption_trim')) && (Unicode::strlen($caption) > $trim_length)) { $caption = Unicode::substr($caption, 0, $trim_length - 5) . '...'; diff --git a/src/ColorboxAttachment.php b/src/ColorboxAttachment.php index 81a9a9f..5742528 100644 --- a/src/ColorboxAttachment.php +++ b/src/ColorboxAttachment.php @@ -112,9 +112,21 @@ class ColorboxAttachment implements ElementAttachmentInterface { $page['#attached']['library'][] = 'colorbox/colorbox-dev'; } - // Add JS and CSS based on selected style. - if ($style != 'none') { - $page['#attached']['library'][] = "colorbox/$style"; + // Add JS and CSS based on selected style library, but only if correct format. + if (!empty($style)) { + // If not using a colorbox library, add colorbox init. + if (strpos($style, 'colorbox/') === FALSE) { + $page['#attached']['library'][] = "colorbox/init"; + } + + $parts = explode('/', $style); + if (count($parts) == 2) { + $page['#attached']['library'][] = $style; + } + // Log warning if style is invalid. + else { + \Drupal::logger('colorbox')->notice($this->t('Attempt to load invalid style %name. Styles need to be defined in format module/library_name.', array('%name' => $style))); + } } else { $page['#attached']['library'][] = "colorbox/init"; diff --git a/src/Form/ColorboxSettingsForm.php b/src/Form/ColorboxSettingsForm.php index ed54747..6709559 100644 --- a/src/Form/ColorboxSettingsForm.php +++ b/src/Form/ColorboxSettingsForm.php @@ -74,17 +74,30 @@ class ColorboxSettingsForm extends ConfigFormBase { '#title' => $this->t('Styles and options'), '#open' => TRUE, ]; + $colorbox_styles = [ - 'default' => $this->t('Default'), - 'plain' => $this->t('Plain (mainly for images)'), - 'stockholmsyndrome' => $this->t('Stockholm Syndrome'), - 'example1' => $this->t('Example 1'), - 'example2' => $this->t('Example 2'), - 'example3' => $this->t('Example 3'), - 'example4' => $this->t('Example 4'), - 'example5' => $this->t('Example 5'), - 'none' => $this->t('None'), - ]; + 'colorbox/default' => $this->t('Default'), + 'colorbox/plain' => $this->t('Plain (mainly for images)'), + 'colorbox/stockholmsyndrome' => $this->t('Stockholm Syndrome'), + 'colorbox/example1' => $this->t('Example 1'), + 'colorbox/example2' => $this->t('Example 2'), + 'colorbox/example3' => $this->t('Example 3'), + 'colorbox/example4' => $this->t('Example 4'), + 'colorbox/example5' => $this->t('Example 5'), + '' => $this->t('None'), + ]; + // Add any custom styles. + $custom_styles = \Drupal::moduleHandler()->invokeAll('colorbox_styles'); + if (!empty($custom_styles) && is_array($custom_styles)) { + foreach ($custom_styles as $library => $name) { + // If not in module/library format, remove it. + $parts = explode('/', $library); + if (count($parts) != 2) { + unset($custom_styles[$library]); + } + } + $colorbox_styles += $custom_styles; + } $form['colorbox_custom_settings']['colorbox_style'] = [ '#type' => 'select', '#title' => $this->t('Style'),