You are here

class ColorboxAttachment in Colorbox 8

An implementation of PageAttachmentInterface for the colorbox library.

Hierarchy

Expanded class hierarchy of ColorboxAttachment

1 string reference to 'ColorboxAttachment'
colorbox.services.yml in ./colorbox.services.yml
colorbox.services.yml
1 service uses ColorboxAttachment
colorbox.attachment in ./colorbox.services.yml
Drupal\colorbox\ColorboxAttachment

File

src/ColorboxAttachment.php, line 13

Namespace

Drupal\colorbox
View source
class ColorboxAttachment implements ElementAttachmentInterface {
  use StringTranslationTrait;

  /**
   * The service to determine if colorbox should be activated.
   *
   * @var \Drupal\colorbox\ActivationCheckInterface
   */
  protected $activation;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The colorbox settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $settings;

  /**
   * Create an instance of ColorboxAttachment.
   */
  public function __construct(ActivationCheckInterface $activation, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config) {
    $this->activation = $activation;
    $this->moduleHandler = $module_handler;
    $this->settings = $config
      ->get('colorbox.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function isApplicable() {
    return !InstallerKernel::installationAttempted() && $this->activation
      ->isActive();
  }

  /**
   * {@inheritdoc}
   */
  public function attach(array &$page) {
    if ($this->settings
      ->get('custom.activate')) {
      $js_settings = [
        'transition' => $this->settings
          ->get('custom.transition_type'),
        'speed' => $this->settings
          ->get('custom.transition_speed'),
        'opacity' => $this->settings
          ->get('custom.opacity'),
        'slideshow' => $this->settings
          ->get('custom.slideshow.slideshow') ? TRUE : FALSE,
        'slideshowAuto' => $this->settings
          ->get('custom.slideshow.auto') ? TRUE : FALSE,
        'slideshowSpeed' => $this->settings
          ->get('custom.slideshow.speed'),
        'slideshowStart' => $this->settings
          ->get('custom.slideshow.text_start'),
        'slideshowStop' => $this->settings
          ->get('custom.slideshow.text_stop'),
        'current' => $this->settings
          ->get('custom.text_current'),
        'previous' => $this->settings
          ->get('custom.text_previous'),
        'next' => $this->settings
          ->get('custom.text_next'),
        'close' => $this->settings
          ->get('custom.text_close'),
        'overlayClose' => $this->settings
          ->get('custom.overlayclose') ? TRUE : FALSE,
        'returnFocus' => $this->settings
          ->get('custom.returnfocus') ? TRUE : FALSE,
        'maxWidth' => $this->settings
          ->get('custom.maxwidth'),
        'maxHeight' => $this->settings
          ->get('custom.maxheight'),
        'initialWidth' => $this->settings
          ->get('custom.initialwidth'),
        'initialHeight' => $this->settings
          ->get('custom.initialheight'),
        'fixed' => $this->settings
          ->get('custom.fixed') ? TRUE : FALSE,
        'scrolling' => $this->settings
          ->get('custom.scrolling') ? TRUE : FALSE,
        'mobiledetect' => $this->settings
          ->get('advanced.mobile_detect') ? TRUE : FALSE,
        'mobiledevicewidth' => $this->settings
          ->get('advanced.mobile_device_width'),
      ];
    }
    else {
      $js_settings = [
        'opacity' => '0.85',
        'current' => $this
          ->t('{current} of {total}'),
        'previous' => $this
          ->t('« Prev'),
        'next' => $this
          ->t('Next »'),
        'close' => $this
          ->t('Close'),
        'maxWidth' => '98%',
        'maxHeight' => '98%',
        'fixed' => TRUE,
        'mobiledetect' => $this->settings
          ->get('advanced.mobile_detect') ? TRUE : FALSE,
        'mobiledevicewidth' => $this->settings
          ->get('advanced.mobile_device_width'),
      ];
    }
    $style = $this->settings
      ->get('custom.style');

    // Give other modules the possibility to override Colorbox
    // settings and style.
    $this->moduleHandler
      ->alter('colorbox_settings', $js_settings, $style);

    // Add colorbox js settings.
    $page['#attached']['drupalSettings']['colorbox'] = $js_settings;

    // 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';
    }

    // Add JS and CSS based on selected style.
    if ($style != 'none') {
      $page['#attached']['library'][] = "colorbox/{$style}";
    }
    else {
      $page['#attached']['library'][] = "colorbox/init";
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ColorboxAttachment::$activation protected property The service to determine if colorbox should be activated.
ColorboxAttachment::$moduleHandler protected property The module handler.
ColorboxAttachment::$settings protected property The colorbox settings.
ColorboxAttachment::attach public function Attach information to the page array. Overrides ElementAttachmentInterface::attach
ColorboxAttachment::isApplicable public function Check if the attachment should be added to the current page. Overrides ElementAttachmentInterface::isApplicable
ColorboxAttachment::__construct public function Create an instance of ColorboxAttachment.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.