class GridLayoutDiscovery in Paragraphs Collection 8
Provides common helper methods for style discovery.
@todo Add documentation in the api file.
Hierarchy
- class \Drupal\paragraphs_collection\GridLayoutDiscovery implements GridLayoutDiscoveryInterface uses StringTranslationTrait
Expanded class hierarchy of GridLayoutDiscovery
1 string reference to 'GridLayoutDiscovery'
1 service uses GridLayoutDiscovery
File
- src/
GridLayoutDiscovery.php, line 16
Namespace
Drupal\paragraphs_collectionView source
class GridLayoutDiscovery implements GridLayoutDiscoveryInterface {
use StringTranslationTrait;
/**
* Collection of styles with its definition.
*
* @var array
*/
protected $gridLayoutsCollection = [];
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The controller resolver.
*
* @var \Drupal\Core\Controller\ControllerResolverInterface
*/
protected $controllerResolver;
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* Constructs a new YamlStyleDiscovery.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
*/
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, ThemeHandlerInterface $theme_handler) {
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this->cache = $cache_backend;
}
/**
* {@inheritdoc}
*/
public function getLibraries($layout) {
$collection = $this
->getGridLayouts();
return isset($collection[$layout]['libraries']) ? $collection[$layout]['libraries'] : [];
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function getLayout($layout) {
$layouts = $this
->getGridLayouts();
if (isset($layouts[$layout])) {
return $layouts[$layout];
}
return [];
}
/**
* Gets the intitiated YAML discovery.
*
* @return \Drupal\Core\Discovery\YamlDiscovery
* The YAML discovery object.
*/
protected function getYamlDiscovery() {
return new YamlDiscovery('paragraphs.grid_layouts', $this->moduleHandler
->getModuleDirectories() + $this->themeHandler
->getThemeDirectories());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GridLayoutDiscovery:: |
protected | property | The cache backend. | |
GridLayoutDiscovery:: |
protected | property | The controller resolver. | |
GridLayoutDiscovery:: |
protected | property | Collection of styles with its definition. | |
GridLayoutDiscovery:: |
protected | property | The module handler. | |
GridLayoutDiscovery:: |
protected | property | The theme handler. | |
GridLayoutDiscovery:: |
public | function |
Get defined grid layouts. Overrides GridLayoutDiscoveryInterface:: |
|
GridLayoutDiscovery:: |
public | function |
Get layout by layout name. Overrides GridLayoutDiscoveryInterface:: |
|
GridLayoutDiscovery:: |
public | function |
Gets sorted grid layout titles keyed by their machine names. Overrides GridLayoutDiscoveryInterface:: |
|
GridLayoutDiscovery:: |
public | function |
Get a list of library names for the given layout. Overrides GridLayoutDiscoveryInterface:: |
|
GridLayoutDiscovery:: |
protected | function | Gets the intitiated YAML discovery. | |
GridLayoutDiscovery:: |
public | function | Constructs a new YamlStyleDiscovery. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |