You are here

public function PhotoswipeAssetsManager::attach in PhotoSwipe 8

Same name and namespace in other branches
  1. 8.2 src/PhotoswipeAssetsManager.php \Drupal\photoswipe\PhotoswipeAssetsManager::attach()
  2. 3.x src/PhotoswipeAssetsManager.php \Drupal\photoswipe\PhotoswipeAssetsManager::attach()

Attach photoswipe assets.

Parameters

array $element: The render array to attach photoswipe assets to.

Overrides PhotoswipeAssetsManagerInterface::attach

File

src/PhotoswipeAssetsManager.php, line 50

Class

PhotoswipeAssetsManager
Photoswipe asset manager.

Namespace

Drupal\photoswipe

Code

public function attach(array &$element) {

  // Add the library of Photoswipe assets.
  $element['#attached']['library'][] = 'photoswipe/photoswipe';

  // Load initialization file.
  $element['#attached']['library'][] = 'photoswipe/photoswipe.init';

  // Add photoswipe js settings.
  $options = $this->config
    ->get('options');

  // Allow other modules to alter / extend the options to pass to photoswipe
  // JavaScript.
  \Drupal::moduleHandler()
    ->alter('photoswipe_js_options', $options);
  $element['#attached']['drupalSettings']['photoswipe']['options'] = $options;

  // Add photoswipe container with class="pswp".
  $template = [
    "#theme" => 'photoswipe_container',
  ];
  $element['#attached']['drupalSettings']['photoswipe']['container'] = $this->renderer
    ->renderPlain($template);
  $this->attached = TRUE;
}