Skip to content
Snippets Groups Projects
Commit 111293a2 authored by git's avatar git Committed by Paris Liakos
Browse files

Issue #2737585 by vaplas, dman: Not working on cached pages

parent 205fcbaf
No related merge requests found
......@@ -16,6 +16,11 @@
var $galleries = $('.photoswipe-gallery', context);
if ($galleries.length) {
// if body haven't special container for show photoswipe gallery append it.
if('container' in settings.photoswipe && !$('.pswp').length){
$('body').append(settings.photoswipe.container);
}
// loop through all gallery elements and bind events
$galleries.each( function (index) {
var $gallery = $(this);
......
......@@ -56,15 +56,3 @@ function photoswipe_page_attachments(array &$attachments) {
\Drupal::service('photoswipe.assets_manager')->attach($attachments);
}
}
/**
* Implements hook_page_bottom().
*/
function photoswipe_page_bottom(array &$page_bottom) {
// Load the Container HTML at the bottom of the page
if (\Drupal::service('photoswipe.assets_manager')->isAttached()) {
$page_bottom['photoswipe'] = array(
'#theme' => 'photoswipe_container',
);
}
}
services:
photoswipe.assets_manager:
class: Drupal\photoswipe\PhotoswipeAssetsManager
arguments: ['@config.factory']
arguments: ['@config.factory', '@renderer']
......@@ -8,6 +8,7 @@
namespace Drupal\photoswipe;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\RendererInterface;
/**
* Photoswipe asset manager.
......@@ -16,6 +17,8 @@ class PhotoswipeAssetsManager implements PhotoswipeAssetsManagerInterface {
/**
* Whether the assets were attached somewhere in this request or not.
*
* @var bool
*/
protected $attached;
......@@ -26,11 +29,24 @@ class PhotoswipeAssetsManager implements PhotoswipeAssetsManagerInterface {
*/
protected $config;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Creates a \Drupal\photoswipe\PhotoswipeAssetsManager.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* The config factory.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(ConfigFactoryInterface $config) {
public function __construct(ConfigFactoryInterface $config, RendererInterface $renderer) {
$this->config = $config->get('photoswipe.settings');
$this->renderer = $renderer;
}
/**
......@@ -39,14 +55,18 @@ class PhotoswipeAssetsManager implements PhotoswipeAssetsManagerInterface {
public function attach(array &$element) {
// We only need to load only once per pace.
if (!$this->attached) {
// Add the library of Photoswipe assets
// Add the library of Photoswipe assets.
$element['#attached']['library'][] = 'photoswipe/photoswipe';
// Load initialization file
// Load initialization file.
$element['#attached']['library'][] = 'photoswipe/photoswipe.init';
// Add photoswipe js settings.
$element['#attached']['drupalSettings']['photoswipe']['options'] = $this->config->get('options');
// Add photoswipe container with class="pswp".
$template = ["#theme" => 'photoswipe_container'];
$element['#attached']['drupalSettings']['photoswipe']['container'] = $this->renderer->render($template);
$this->attached = TRUE;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment