DynamicLayoutDeriver.php in Dynamic Layouts 8
File
src/Plugin/Derivative/DynamicLayoutDeriver.php
View source
<?php
namespace Drupal\dynamic_layouts\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Layout\LayoutDefinition;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DynamicLayoutDeriver extends DeriverBase implements ContainerDeriverInterface {
protected $basePluginId;
protected $entityTypeManager;
public function __construct($base_plugin_id, EntityTypeManagerInterface $entityTypeManager) {
$this->basePluginId = $base_plugin_id;
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($base_plugin_id, $container
->get('entity_type.manager'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$config_entities = $this->entityTypeManager
->getStorage('dynamic_layout')
->loadMultiple();
if ($config_entities) {
foreach ($config_entities as $entity) {
$regions = $entity
->getLayoutRegions();
$icon_map = $entity
->getIconMap();
$module_path = drupal_get_path('module', 'dynamic_layouts') . '/templates';
$this->derivatives[$entity
->id()] = new LayoutDefinition([
'label' => $entity
->label(),
'category' => $entity
->getCategory(),
'class' => 'Drupal\\dynamic_layouts\\Plugin\\Layout\\DynamicLayout',
'regions' => $regions,
'template' => 'dynamic-layout-frontend',
'path' => $module_path,
'icon_map' => $icon_map,
'config_dependencies' => [
$entity
->getConfigDependencyKey() => [
$entity
->getConfigDependencyName(),
],
],
]);
}
}
return $this->derivatives;
}
}