Version 1
[yaffs-website] / web / modules / contrib / blazy / src / BlazyManagerBase.php
1 <?php
2
3 namespace Drupal\blazy;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Render\RendererInterface;
8 use Drupal\Core\Config\ConfigFactoryInterface;
9 use Drupal\Core\Cache\Cache;
10 use Drupal\Core\Cache\CacheBackendInterface;
11 use Drupal\Component\Utility\Html;
12 use Drupal\Component\Utility\NestedArray;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Implements BlazyManagerInterface.
17  */
18 abstract class BlazyManagerBase implements BlazyManagerInterface {
19
20   /**
21    * The entity type manager service.
22    *
23    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24    */
25   protected $entityTypeManager;
26
27   /**
28    * The module handler service.
29    *
30    * @var \Drupal\Core\Extension\ModuleHandlerInterface
31    */
32   protected $moduleHandler;
33
34   /**
35    * The renderer.
36    *
37    * @var \Drupal\Core\Render\RendererInterface
38    */
39   protected $renderer;
40
41   /**
42    * The config factory.
43    *
44    * @var \Drupal\Core\Config\ConfigFactoryInterface
45    */
46   protected $configFactory;
47
48   /**
49    * The cache backend.
50    *
51    * @var \Drupal\Core\Cache\CacheBackendInterface
52    */
53   protected $cache;
54
55   /**
56    * Constructs a BlazyManager object.
57    */
58   public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache) {
59     $this->entityTypeManager = $entity_type_manager;
60     $this->moduleHandler     = $module_handler;
61     $this->renderer          = $renderer;
62     $this->configFactory     = $config_factory;
63     $this->cache             = $cache;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public static function create(ContainerInterface $container) {
70     return new static(
71       $container->get('entity_type.manager'),
72       $container->get('module_handler'),
73       $container->get('renderer'),
74       $container->get('config.factory'),
75       $container->get('cache.default')
76     );
77   }
78
79   /**
80    * Returns the entity type manager.
81    */
82   public function getEntityTypeManager() {
83     return $this->entityTypeManager;
84   }
85
86   /**
87    * Returns the module handler.
88    */
89   public function getModuleHandler() {
90     return $this->moduleHandler;
91   }
92
93   /**
94    * Returns the renderer.
95    */
96   public function getRenderer() {
97     return $this->renderer;
98   }
99
100   /**
101    * Returns the cache.
102    */
103   public function getCache() {
104     return $this->cache;
105   }
106
107   /**
108    * Returns any config, or keyed by the $setting_name.
109    */
110   public function configLoad($setting_name = '', $settings = 'blazy.settings') {
111     $config  = $this->configFactory->get($settings);
112     $configs = $config->get();
113     unset($configs['_core']);
114     return empty($setting_name) ? $configs : $config->get($setting_name);
115   }
116
117   /**
118    * Returns a shortcut for loading a config entity: image_style, slick, etc.
119    */
120   public function entityLoad($id, $entity_type = 'image_style') {
121     return $this->entityTypeManager->getStorage($entity_type)->load($id);
122   }
123
124   /**
125    * Returns a shortcut for loading multiple configuration entities.
126    */
127   public function entityLoadMultiple($entity_type = 'image_style', $ids = NULL) {
128     return $this->entityTypeManager->getStorage($entity_type)->loadMultiple($ids);
129   }
130
131   /**
132    * Returns array of needed assets suitable for #attached property.
133    */
134   public function attach($attach = []) {
135     $load   = [];
136     $dummy  = [];
137     $switch = empty($attach['media_switch']) ? '' : $attach['media_switch'];
138
139     if ($switch && $switch != 'content') {
140       $attach[$switch] = $switch;
141
142       if (in_array($switch, $this->getLightboxes())) {
143         $load['library'][] = 'blazy/lightbox';
144       }
145     }
146
147     if (!empty($attach['colorbox'])) {
148       \Drupal::service('colorbox.attachment')->attach($dummy);
149       $load = isset($dummy['#attached']) ? NestedArray::mergeDeep($load, $dummy['#attached']) : $load;
150       $load['library'][] = 'blazy/colorbox';
151       unset($dummy);
152     }
153
154     // Only load grid xor column, but not both.
155     $attach['column'] = !empty($attach['style']) && $attach['style'] == 'column';
156     if (!empty($attach['column'])) {
157       $attach['grid'] = FALSE;
158     }
159     foreach (['column', 'grid', 'media', 'photobox', 'ratio'] as $component) {
160       if (!empty($attach[$component])) {
161         $load['library'][] = 'blazy/' . $component;
162       }
163     }
164
165     // Core Blazy libraries.
166     if (!empty($attach['blazy'])) {
167       $load['library'][] = 'blazy/load';
168       $load['drupalSettings']['blazy'] = $this->configLoad()['blazy'];
169     }
170
171     $this->moduleHandler->alter('blazy_attach', $load, $attach);
172     return $load;
173   }
174
175   /**
176    * Collects defined skins as registered via hook_MODULE_NAME_skins_info().
177    */
178   public function buildSkins($namespace, $skin_class, $methods = []) {
179     $skins = [];
180     $cid = $namespace . ':skins';
181     if ($cache = $this->cache->get($cid)) {
182       $skins = $cache->data;
183     }
184     else {
185       $classes = $this->moduleHandler->invokeAll($namespace . '_skins_info');
186       $classes = array_merge([$skin_class], $classes);
187       $items   = $skins = [];
188       foreach ($classes as $class) {
189         if (class_exists($class)) {
190           $reflection = new \ReflectionClass($class);
191           if ($reflection->implementsInterface($skin_class . 'Interface')) {
192             $skin = new $class();
193             if (empty($methods) && method_exists($skin, 'skins')) {
194               $items = $skin->skins();
195             }
196             else {
197               foreach ($methods as $method) {
198                 $items[$method] = method_exists($skin, $method) ? $skin->{$method}() : [];
199               }
200             }
201           }
202         }
203         $skins = NestedArray::mergeDeep($skins, $items);
204       }
205
206       $count = isset($items['skins']) ? count($items['skins']) : count($items);
207       $tags  = Cache::buildTags($cid, ['count:' . $count]);
208
209       $this->cache->set($cid, $skins, Cache::PERMANENT, $tags);
210     }
211     return $skins;
212   }
213
214   /**
215    * Gets the supported lightboxes.
216    *
217    * @return array
218    *   The supported lightboxes.
219    */
220   public function getLightboxes() {
221     $photobox = \Drupal::root() . '/libraries/photobox/photobox/jquery.photobox.js';
222
223     $lightboxes = [];
224     foreach (['colorbox', 'photobox'] as $lightbox) {
225       $supported = function_exists($lightbox . '_theme');
226       if ($lightbox == 'photobox' && is_file($photobox)) {
227         $supported = TRUE;
228       }
229       if ($supported) {
230         $lightboxes[] = $lightbox;
231       }
232     }
233
234     $this->moduleHandler->alter('blazy_lightboxes', $lightboxes);
235     return $lightboxes;
236   }
237
238   /**
239    * Returns the trusted HTML ID common for Blazy, GridStack, Mason, Slick.
240    *
241    * @deprecated: Removed prior to release for Blazy::getHtmlId().
242    */
243   public static function getHtmlId($string = 'blazy', $id = '') {
244     $blazy_id = &drupal_static('blazy_id', 0);
245
246     // Do not use dynamic Html::getUniqueId, otherwise broken AJAX.
247     return empty($id) ? Html::getId($string . '-' . ++$blazy_id) : $id;
248   }
249
250 }