View source
<?php
namespace Drupal\ui_patterns\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\ui_patterns\Definition\PatternDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PatternBase extends PluginBase implements PatternInterface, ContainerFactoryPluginInterface {
protected $root;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->root = $root;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('app.root'), $container
->get('module_handler'));
}
public function getThemeImplementation() {
$definition = $this
->getPluginDefinition();
$item = [];
$item += $this
->processVariables($definition);
$item += $this
->processUseProperty($definition);
return [
$definition['theme hook'] => $item,
];
}
public function getLibraryDefinitions() {
$libraries = [];
$definition = $this
->getPluginDefinition();
$items = array_filter($definition['libraries'], function ($library) {
return is_array($library);
});
if (!empty($definition['base path'])) {
$base_path = str_replace($this->root, '', $definition['base path']);
$this
->processLibraries($items, $base_path);
}
$id = $definition['id'];
array_walk($items, function ($value) use (&$libraries, $id) {
$libraries[$id . '.' . key($value)] = reset($value);
});
return $libraries;
}
protected function processLibraries(&$libraries, $base_path, $parent = '') {
if (!is_string($libraries)) {
$parents = [
'js',
'base',
'layout',
'component',
'state',
'theme',
];
$_libraries = $libraries;
foreach ($_libraries as $name => $values) {
$is_asset = in_array($parent, $parents, TRUE);
$is_external = isset($values['type']) && $values['type'] == 'external';
if ($is_asset && !$is_external) {
$libraries[$base_path . DIRECTORY_SEPARATOR . $name] = $values;
unset($libraries[$name]);
}
elseif (!$is_asset) {
$this
->processLibraries($libraries[$name], $base_path, $name);
}
}
}
}
protected function processUseProperty(PatternDefinition $definition) {
$return = [];
if ($definition
->hasUse()) {
$return = [
'path' => $this->moduleHandler
->getModule('ui_patterns')
->getPath() . '/templates',
'template' => 'patterns-use-wrapper',
];
}
return $return;
}
protected function processVariables(PatternDefinition $definition) {
$return = [];
foreach ($definition
->getFields() as $field) {
$return['variables'][$field
->getName()] = NULL;
}
$return['variables']['attributes'] = [];
$return['variables']['context'] = [];
$return['variables']['variant'] = '';
$return['variables']['use'] = '';
return $return;
}
}