3 namespace Drupal\blazy;
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;
16 * Implements BlazyManagerInterface.
18 abstract class BlazyManagerBase implements BlazyManagerInterface {
21 * The entity type manager service.
23 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
25 protected $entityTypeManager;
28 * The module handler service.
30 * @var \Drupal\Core\Extension\ModuleHandlerInterface
32 protected $moduleHandler;
37 * @var \Drupal\Core\Render\RendererInterface
44 * @var \Drupal\Core\Config\ConfigFactoryInterface
46 protected $configFactory;
51 * @var \Drupal\Core\Cache\CacheBackendInterface
56 * Constructs a BlazyManager object.
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;
69 public static function create(ContainerInterface $container) {
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')
80 * Returns the entity type manager.
82 public function getEntityTypeManager() {
83 return $this->entityTypeManager;
87 * Returns the module handler.
89 public function getModuleHandler() {
90 return $this->moduleHandler;
94 * Returns the renderer.
96 public function getRenderer() {
97 return $this->renderer;
103 public function getCache() {
108 * Returns any config, or keyed by the $setting_name.
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);
118 * Returns a shortcut for loading a config entity: image_style, slick, etc.
120 public function entityLoad($id, $entity_type = 'image_style') {
121 return $this->entityTypeManager->getStorage($entity_type)->load($id);
125 * Returns a shortcut for loading multiple configuration entities.
127 public function entityLoadMultiple($entity_type = 'image_style', $ids = NULL) {
128 return $this->entityTypeManager->getStorage($entity_type)->loadMultiple($ids);
132 * Returns array of needed assets suitable for #attached property.
134 public function attach($attach = []) {
137 $switch = empty($attach['media_switch']) ? '' : $attach['media_switch'];
139 if ($switch && $switch != 'content') {
140 $attach[$switch] = $switch;
142 if (in_array($switch, $this->getLightboxes())) {
143 $load['library'][] = 'blazy/lightbox';
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';
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;
159 foreach (['column', 'grid', 'media', 'photobox', 'ratio'] as $component) {
160 if (!empty($attach[$component])) {
161 $load['library'][] = 'blazy/' . $component;
165 // Core Blazy libraries.
166 if (!empty($attach['blazy'])) {
167 $load['library'][] = 'blazy/load';
168 $load['drupalSettings']['blazy'] = $this->configLoad()['blazy'];
171 $this->moduleHandler->alter('blazy_attach', $load, $attach);
176 * Collects defined skins as registered via hook_MODULE_NAME_skins_info().
178 public function buildSkins($namespace, $skin_class, $methods = []) {
180 $cid = $namespace . ':skins';
181 if ($cache = $this->cache->get($cid)) {
182 $skins = $cache->data;
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();
197 foreach ($methods as $method) {
198 $items[$method] = method_exists($skin, $method) ? $skin->{$method}() : [];
203 $skins = NestedArray::mergeDeep($skins, $items);
206 $count = isset($items['skins']) ? count($items['skins']) : count($items);
207 $tags = Cache::buildTags($cid, ['count:' . $count]);
209 $this->cache->set($cid, $skins, Cache::PERMANENT, $tags);
215 * Gets the supported lightboxes.
218 * The supported lightboxes.
220 public function getLightboxes() {
221 $photobox = \Drupal::root() . '/libraries/photobox/photobox/jquery.photobox.js';
224 foreach (['colorbox', 'photobox'] as $lightbox) {
225 $supported = function_exists($lightbox . '_theme');
226 if ($lightbox == 'photobox' && is_file($photobox)) {
230 $lightboxes[] = $lightbox;
234 $this->moduleHandler->alter('blazy_lightboxes', $lightboxes);
239 * Returns the trusted HTML ID common for Blazy, GridStack, Mason, Slick.
241 * @deprecated: Removed prior to release for Blazy::getHtmlId().
243 public static function getHtmlId($string = 'blazy', $id = '') {
244 $blazy_id = &drupal_static('blazy_id', 0);
246 // Do not use dynamic Html::getUniqueId, otherwise broken AJAX.
247 return empty($id) ? Html::getId($string . '-' . ++$blazy_id) : $id;