GridLayoutDiscovery.php in Paragraphs Collection 8
File
src/GridLayoutDiscovery.php
View source
<?php
namespace Drupal\paragraphs_collection;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Discovery\YamlDiscovery;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class GridLayoutDiscovery implements GridLayoutDiscoveryInterface {
use StringTranslationTrait;
protected $gridLayoutsCollection = [];
protected $moduleHandler;
protected $controllerResolver;
protected $cache;
protected $themeHandler;
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, ThemeHandlerInterface $theme_handler) {
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this->cache = $cache_backend;
}
public function getLibraries($layout) {
$collection = $this
->getGridLayouts();
return isset($collection[$layout]['libraries']) ? $collection[$layout]['libraries'] : [];
}
public function getGridLayouts() {
$cid = 'paragraphs_collection_grid_layouts';
if ($this->gridLayoutsCollection) {
return $this->gridLayoutsCollection;
}
else {
if ($cached = $this->cache
->get($cid)) {
$this->gridLayoutsCollection = $cached->data;
}
else {
$yaml_discovery = $this
->getYamlDiscovery();
$this->gridLayoutsCollection = [];
foreach ($yaml_discovery
->findAll() as $provider => $layouts) {
foreach ($layouts as $layout => $definition) {
if (empty($definition['title'])) {
throw new InvalidGridLayoutException('The "title" of "' . $layout . '" must be non-empty.');
}
$definition['title'] = $this
->t($definition['title']);
if (!empty($definition['description'])) {
$definition['description'] = $this
->t($definition['description']);
}
$definition['provider'] = $provider;
$this->gridLayoutsCollection[$layout] = $definition;
}
}
$this->cache
->set($cid, $this->gridLayoutsCollection);
}
}
return $this->gridLayoutsCollection;
}
public function getLayoutOptions() {
$layout_options = [];
$layouts = $this
->getGridLayouts();
foreach ($layouts as $name => $layout) {
$layout_options[$name] = $layout['title'];
}
uasort($layout_options, 'strcasecmp');
return $layout_options;
}
public function getLayout($layout) {
$layouts = $this
->getGridLayouts();
if (isset($layouts[$layout])) {
return $layouts[$layout];
}
return [];
}
protected function getYamlDiscovery() {
return new YamlDiscovery('paragraphs.grid_layouts', $this->moduleHandler
->getModuleDirectories() + $this->themeHandler
->getThemeDirectories());
}
}