View source
<?php
namespace Drupal\blazy;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class BlazyManagerBase implements BlazyManagerInterface {
use DependencySerializationTrait;
use StringTranslationTrait;
protected $root;
protected $entityRepository;
protected $entityTypeManager;
protected $moduleHandler;
protected $renderer;
protected $configFactory;
protected $cache;
protected $languageManager;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache) {
$this->entityRepository = $entity_repository;
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->renderer = $renderer;
$this->configFactory = $config_factory;
$this->cache = $cache;
}
public static function create(ContainerInterface $container) {
$instance = new static($container
->get('entity.repository'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $container
->get('renderer'), $container
->get('config.factory'), $container
->get('cache.default'));
$instance
->setRoot($container
->get('app.root'));
$instance
->setLanguageManager($container
->get('language_manager'));
return $instance;
}
public function root() {
return $this->root;
}
public function setRoot($root) {
$this->root = $root;
return $this;
}
public function languageManager() {
return $this->languageManager;
}
public function setLanguageManager($language_manager) {
$this->languageManager = $language_manager;
return $this;
}
public function getEntityRepository() {
return $this->entityRepository;
}
public function getEntityTypeManager() {
return $this->entityTypeManager;
}
public function getModuleHandler() {
return $this->moduleHandler;
}
public function getRenderer() {
return $this->renderer;
}
public function getConfigFactory() {
return $this->configFactory;
}
public function getCache() {
return $this->cache;
}
public function configLoad($setting_name = '', $settings = 'blazy.settings') {
$config = $this->configFactory
->get($settings);
$configs = $config
->get();
unset($configs['_core']);
return empty($setting_name) ? $configs : $config
->get($setting_name);
}
public function entityLoad($id, $entity_type = 'image_style') {
return $this->entityTypeManager
->getStorage($entity_type)
->load($id);
}
public function entityLoadMultiple($entity_type = 'image_style', $ids = NULL) {
return $this->entityTypeManager
->getStorage($entity_type)
->loadMultiple($ids);
}
public function attach(array $attach = []) {
$load = [];
$switch = empty($attach['media_switch']) ? '' : $attach['media_switch'];
if ($switch && $switch != 'content') {
$attach[$switch] = $switch;
if (in_array($switch, $this
->getLightboxes())) {
$load['library'][] = 'blazy/lightbox';
if (!empty($attach['colorbox'])) {
BlazyAlter::attachColorbox($load, $attach);
}
}
}
if (!empty($attach['style'])) {
$attach[$attach['style']] = $attach['style'];
}
if (!empty($attach['fx']) && $attach['fx'] == 'blur') {
$load['library'][] = 'blazy/fx.blur';
}
$components = [
'column',
'filter',
'grid',
'media',
'photobox',
'ratio',
];
foreach ($components as $component) {
if (!empty($attach[$component])) {
$load['library'][] = 'blazy/' . $component;
}
}
if (empty($attach['_unblazy'])) {
$load['library'][] = 'blazy/load';
$load['drupalSettings']['blazy'] = $this
->configLoad('blazy');
$load['drupalSettings']['blazyIo'] = $this
->getIoSettings($attach);
}
if (!empty($attach['use_ajax'])) {
$load['library'][] = 'blazy/bio.ajax';
}
$this->moduleHandler
->alter('blazy_attach', $load, $attach);
return $load;
}
public function getIoSettings(array $attach = []) {
$io = [];
$thold = trim($this
->configLoad('io.threshold')) ?: '0';
$number = strpos($thold, '.') !== FALSE ? (double) $thold : (int) $thold;
$thold = strpos($thold, ',') !== FALSE ? array_map('trim', explode(',', $thold)) : [
$number,
];
foreach ([
'enabled',
'disconnect',
'rootMargin',
'threshold',
] as $key) {
$default = $key == 'rootMargin' ? '0px' : FALSE;
$value = $key == 'threshold' ? $thold : $this
->configLoad('io.' . $key);
$io[$key] = isset($attach['io.' . $key]) ? $attach['io.' . $key] : ($value ?: $default);
}
return (object) $io;
}
public function getCommonSettings(array &$settings) {
$config = array_intersect_key($this
->configLoad(), BlazyDefault::uiSettings());
$config['fx'] = isset($config['fx']) ? $config['fx'] : '';
$config['fx'] = empty($settings['fx']) ? $config['fx'] : $settings['fx'];
$settings = array_merge($settings, $config);
$settings['fx'] = isset($settings['_fx']) ? $settings['_fx'] : $settings['fx'];
$settings['media_switch'] = $switch = empty($settings['media_switch']) ? '' : $settings['media_switch'];
$settings['iframe_domain'] = $this
->configLoad('iframe_domain', 'media.settings');
$settings['is_preview'] = Blazy::isPreview();
$settings['lightbox'] = $switch && in_array($switch, $this
->getLightboxes()) ? $switch : FALSE;
$settings['namespace'] = empty($settings['namespace']) ? 'blazy' : $settings['namespace'];
$settings['route_name'] = Blazy::routeMatch() ? Blazy::routeMatch()
->getRouteName() : '';
$settings['_resimage'] = $this->moduleHandler
->moduleExists('responsive_image');
$settings['resimage'] = $settings['_resimage'] && !empty($settings['responsive_image_style']);
$settings['resimage'] = $settings['resimage'] ? $this
->entityLoad($settings['responsive_image_style'], 'responsive_image_style') : FALSE;
$settings['current_language'] = $this->languageManager
->getCurrentLanguage()
->getId();
if ($switch) {
$settings[$switch] = empty($settings[$switch]) ? $switch : $settings[$switch];
}
}
public function getLightboxes() {
$lightboxes = [];
foreach ([
'colorbox',
'photobox',
] as $lightbox) {
if (function_exists($lightbox . '_theme')) {
$lightboxes[] = $lightbox;
}
}
if (is_file($this->root . '/libraries/photobox/photobox/jquery.photobox.js')) {
$lightboxes[] = 'photobox';
}
$this->moduleHandler
->alter('blazy_lightboxes', $lightboxes);
return array_unique($lightboxes);
}
public function getImageEffects() {
$effects[] = 'blur';
$this->moduleHandler
->alter('blazy_image_effects', $effects);
$effects = array_unique($effects);
return array_combine($effects, $effects);
}
public function isBlazy(array &$settings, array $item = []) {
$item_id = isset($settings['item_id']) ? $settings['item_id'] : 'x';
$content = isset($item[$item_id]) ? $item[$item_id] : $item;
$image = isset($item['item']) ? $item['item'] : NULL;
$settings['_item'] = $image;
if (isset($item['settings'])) {
$this
->isBlazyFormatter($settings, $item);
}
if (is_array($content) && isset($content['#view']) && ($view = $content['#view'])) {
if ($blazy_field = BlazyViews::viewsField($view)) {
$settings = array_merge(array_filter($blazy_field
->mergedViewsSettings()), array_filter($settings));
}
}
unset($settings['first_image']);
}
protected function isBlazyFormatter(array &$settings, array $item = []) {
$blazy = $item['settings'];
if (!empty($blazy['blazy_data'])) {
$settings['blazy_data'] = empty($settings['blazy_data']) ? $blazy['blazy_data'] : array_merge($settings['blazy_data'], $blazy['blazy_data']);
$settings['_dimensions'] = !empty($settings['blazy_data']['dimensions']);
}
$cherries = BlazyDefault::cherrySettings() + [
'uri' => '',
];
foreach ($cherries as $key => $value) {
$fallback = isset($settings[$key]) ? $settings[$key] : $value;
$settings[$key] = isset($blazy[$key]) && empty($fallback) ? $blazy[$key] : $fallback;
}
$settings['_uri'] = empty($settings['_uri']) ? $settings['uri'] : $settings['_uri'];
unset($settings['uri']);
}
public function getCacheMetadata(array $build = []) {
$settings = isset($build['settings']) ? $build['settings'] : $build;
$namespace = isset($settings['namespace']) ? $settings['namespace'] : 'blazy';
$max_age = $this
->configLoad('cache.page.max_age', 'system.performance');
$max_age = empty($settings['cache']) ? $max_age : $settings['cache'];
$id = isset($settings['id']) ? $settings['id'] : Blazy::getHtmlId($namespace);
$suffixes[] = empty($settings['count']) ? count(array_filter($settings)) : $settings['count'];
$cache['tags'] = Cache::buildTags($namespace . ':' . $id, $suffixes, '.');
$cache['contexts'] = [
'languages',
];
$cache['max-age'] = $max_age;
$cache['keys'] = isset($settings['cache_metadata']['keys']) ? $settings['cache_metadata']['keys'] : [
$id,
];
if (!empty($settings['cache_tags'])) {
$cache['tags'] = Cache::mergeTags($cache['tags'], $settings['cache_tags']);
}
return $cache;
}
protected function setAttachments(array &$element, array $settings, array $attachments = []) {
$cache = $this
->getCacheMetadata($settings);
$attached = $this
->attach($settings);
$attachments = empty($attachments) ? $attached : NestedArray::mergeDeep($attached, $attachments);
$element['#attached'] = empty($element['#attached']) ? $attachments : NestedArray::mergeDeep($element['#attached'], $attachments);
$element['#cache'] = empty($element['#cache']) ? $cache : NestedArray::mergeDeep($element['#cache'], $cache);
}
public function setResponsiveImageDimensions(array &$settings = [], $initial = TRUE) {
$srcset = [];
foreach ($this
->getResponsiveImageStyles($settings['resimage'])['styles'] as $style) {
$styled = array_merge($settings, BlazyUtil::transformDimensions($style, $settings, $initial));
$srcset[$styled['width']] = round($styled['height'] / $styled['width'] * 100, 2);
}
ksort($srcset);
$settings['blazy_data']['dimensions'] = $srcset;
$settings['padding_bottom'] = end($srcset);
$settings['_dimensions'] = TRUE;
}
public function getResponsiveImageStyles($responsive) {
$cache_tags = $responsive
->getCacheTags();
$image_styles = $this
->entityLoadMultiple('image_style', $responsive
->getImageStyleIds());
foreach ($image_styles as $image_style) {
$cache_tags = Cache::mergeTags($cache_tags, $image_style
->getCacheTags());
}
return [
'caches' => $cache_tags,
'styles' => $image_styles,
];
}
public function buildSkins($namespace, $skin_class, $methods = []) {
return [];
}
}