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\NestedArray;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Implements BlazyManagerInterface.
17 abstract class BlazyManagerBase implements BlazyManagerInterface {
20 * The entity type manager service.
22 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24 protected $entityTypeManager;
27 * The module handler service.
29 * @var \Drupal\Core\Extension\ModuleHandlerInterface
31 protected $moduleHandler;
36 * @var \Drupal\Core\Render\RendererInterface
43 * @var \Drupal\Core\Config\ConfigFactoryInterface
45 protected $configFactory;
50 * @var \Drupal\Core\Cache\CacheBackendInterface
55 * The supported lightboxes.
59 protected $lightboxes = [];
62 * Constructs a BlazyManager object.
64 public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache) {
65 $this->entityTypeManager = $entity_type_manager;
66 $this->moduleHandler = $module_handler;
67 $this->renderer = $renderer;
68 $this->configFactory = $config_factory;
69 $this->cache = $cache;
75 public static function create(ContainerInterface $container) {
77 $container->get('entity_type.manager'),
78 $container->get('module_handler'),
79 $container->get('renderer'),
80 $container->get('config.factory'),
81 $container->get('cache.default')
86 * Returns the entity type manager.
88 public function getEntityTypeManager() {
89 return $this->entityTypeManager;
93 * Returns the module handler.
95 public function getModuleHandler() {
96 return $this->moduleHandler;
100 * Returns the renderer.
102 public function getRenderer() {
103 return $this->renderer;
107 * Returns the config factory.
109 public function getConfigFactory() {
110 return $this->configFactory;
116 public function getCache() {
121 * Returns any config, or keyed by the $setting_name.
123 public function configLoad($setting_name = '', $settings = 'blazy.settings') {
124 $config = $this->configFactory->get($settings);
125 $configs = $config->get();
126 unset($configs['_core']);
127 return empty($setting_name) ? $configs : $config->get($setting_name);
131 * Returns a shortcut for loading a config entity: image_style, slick, etc.
133 public function entityLoad($id, $entity_type = 'image_style') {
134 return $this->entityTypeManager->getStorage($entity_type)->load($id);
138 * Returns a shortcut for loading multiple configuration entities.
140 public function entityLoadMultiple($entity_type = 'image_style', $ids = NULL) {
141 return $this->entityTypeManager->getStorage($entity_type)->loadMultiple($ids);
145 * Returns array of needed assets suitable for #attached property.
147 public function attach($attach = []) {
150 $switch = empty($attach['media_switch']) ? '' : $attach['media_switch'];
152 if ($switch && $switch != 'content') {
153 $attach[$switch] = $switch;
155 if (in_array($switch, $this->getLightboxes())) {
156 $load['library'][] = 'blazy/lightbox';
160 // Only load grid xor column, but not both.
161 $attach['column'] = !empty($attach['style']) && $attach['style'] == 'column';
162 if (!empty($attach['column'])) {
163 $attach['grid'] = FALSE;
165 foreach (['column', 'grid', 'media', 'photobox', 'ratio'] as $component) {
166 if (!empty($attach[$component])) {
167 $load['library'][] = 'blazy/' . $component;
171 // Core Blazy libraries.
172 if (!empty($attach['blazy'])) {
173 $load['library'][] = 'blazy/load';
174 $load['drupalSettings']['blazy'] = $this->configLoad()['blazy'];
177 $this->moduleHandler->alter('blazy_attach', $load, $attach);
182 * Collects defined skins as registered via hook_MODULE_NAME_skins_info().
184 public function buildSkins($namespace, $skin_class, $methods = []) {
185 $cid = $namespace . ':skins';
186 $cache = $this->cache->get($cid);
192 $classes = $this->moduleHandler->invokeAll($namespace . '_skins_info');
193 $classes = array_merge([$skin_class], $classes);
194 $items = $skins = [];
195 foreach ($classes as $class) {
196 if (class_exists($class)) {
197 $reflection = new \ReflectionClass($class);
198 if ($reflection->implementsInterface($skin_class . 'Interface')) {
199 $skin = new $class();
200 if (empty($methods) && method_exists($skin, 'skins')) {
201 $items = $skin->skins();
204 foreach ($methods as $method) {
205 $items[$method] = method_exists($skin, $method) ? $skin->{$method}() : [];
210 $skins = NestedArray::mergeDeep($skins, $items);
213 $count = isset($items['skins']) ? count($items['skins']) : count($items);
214 $tags = Cache::buildTags($cid, ['count:' . $count]);
216 $this->cache->set($cid, $skins, Cache::PERMANENT, $tags);
222 * Gets the supported lightboxes.
225 * The supported lightboxes.
227 public function getLightboxes() {
228 $boxes = $this->lightboxes + ['colorbox', 'photobox'];
231 foreach (array_unique($boxes) as $lightbox) {
232 if (function_exists($lightbox . '_theme')) {
233 $lightboxes[] = $lightbox;
237 $this->moduleHandler->alter('blazy_lightboxes', $lightboxes);
238 return array_unique($lightboxes);
242 * Sets the lightboxes.
244 * @param string $lightbox
245 * The lightbox name, expected to be the module name.
247 public function setLightboxes($lightbox) {
248 $this->lightboxes[] = $lightbox;