pattern_library-8.x-2.x-dev/src/Plugin/Deriver/PatternLibraryLayoutDeriver.php
src/Plugin/Deriver/PatternLibraryLayoutDeriver.php
<?php
namespace Drupal\pattern_library\Plugin\Deriver;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\pattern_library\Plugin\Layout\PatternLayoutDefinition;
use Drupal\pattern_library\Plugin\Layout\PatternLibraryLayout;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Creates a layout based on pattern definitions.
*/
class PatternLibraryLayoutDeriver extends DeriverBase implements ContainerDeriverInterface {
/**
* @var PluginManagerInterface
*/
protected $patternLibraryManager;
/**
* Class constructor.
*
* @param $base_plugin_id
* @param PluginManagerInterface $pattern_library_manager
*/
public function __construct($base_plugin_id, PluginManagerInterface $pattern_library_manager) {
$this->patternLibraryManager = $pattern_library_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$base_plugin_id,
$container->get('plugin.manager.pattern_library')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
/** @var \Drupal\pattern_library\Plugin\Pattern $pattern **/
foreach ($this->patternLibraryManager->getDefinitionInstances() as $plugin_id => $pattern) {
$definition = [
'id' => $plugin_id,
'path' => 'templates',
'class' => PatternLibraryLayout::class,
'label' => $pattern->getLabel(),
'icon' => $pattern->getIcon(),
'icon_map' => $pattern->getIconMap(),
'regions' => $pattern->getLayoutRegions(),
'category' => 'Pattern Library',
'provider' => 'pattern_library',
'pattern_definition' => $pattern
];
if ($pattern->hasLibraries()) {
$definition['library'] = "pattern_library/{$pattern->libraryKey()}";
}
$this->derivatives[$plugin_id] = new PatternLayoutDefinition($definition);
}
return $this->derivatives;
}
}
