abstract class PatternBase in UI Patterns 8
Class PatternBase.
@package Drupal\ui_patterns\Plugin
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\ui_patterns\Plugin\PatternBase implements ContainerFactoryPluginInterface, PatternInterface
Expanded class hierarchy of PatternBase
2 files declare their use of PatternBase
- LibraryPattern.php in modules/
ui_patterns_library/ src/ Plugin/ UiPatterns/ Pattern/ LibraryPattern.php - PatternBaseTest.php in tests/
src/ Kernel/ Plugin/ PatternBaseTest.php
File
- src/
Plugin/ PatternBase.php, line 16
Namespace
Drupal\ui_patterns\PluginView source
abstract class PatternBase extends PluginBase implements PatternInterface, ContainerFactoryPluginInterface {
/**
* The app root.
*
* @var string
*/
protected $root;
/**
* Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* UiPatternsManager constructor.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function getThemeImplementation() {
$definition = $this
->getPluginDefinition();
$item = [];
$item += $this
->processVariables($definition);
$item += $this
->processUseProperty($definition);
return [
$definition['theme hook'] => $item,
];
}
/**
* {@inheritdoc}
*/
public function getLibraryDefinitions() {
// @codingStandardsIgnoreStart
$libraries = [];
$definition = $this
->getPluginDefinition();
// Get only locally defined libraries.
$items = array_filter($definition['libraries'], function ($library) {
return is_array($library);
});
// Attach pattern base path to assets.
if (!empty($definition['base path'])) {
$base_path = str_replace($this->root, '', $definition['base path']);
$this
->processLibraries($items, $base_path);
}
// Produce final libraries array.
$id = $definition['id'];
array_walk($items, function ($value) use (&$libraries, $id) {
$libraries[$id . '.' . key($value)] = reset($value);
});
// @codingStandardsIgnoreEnd
return $libraries;
}
/**
* Process libraries.
*
* @param array|string $libraries
* List of dependencies or "dependencies:" root property.
* @param string $base_path
* Pattern base path.
* @param string $parent
* Item parent set in previous recursive iteration, if any.
*/
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);
}
}
}
}
/**
* Process 'use' definition property.
*
* @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
* Pattern definition array.
*
* @return array
* Processed hook definition portion.
*/
protected function processUseProperty(PatternDefinition $definition) {
$return = [];
if ($definition
->hasUse()) {
$return = [
'path' => $this->moduleHandler
->getModule('ui_patterns')
->getPath() . '/templates',
'template' => 'patterns-use-wrapper',
];
}
return $return;
}
/**
* Process theme variables.
*
* @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
* Pattern definition array.
*
* @return array
* Processed hook definition portion.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PatternBase:: |
protected | property | Module handler. | |
PatternBase:: |
protected | property | The app root. | |
PatternBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
PatternBase:: |
public | function |
Get library definitions for current pattern. Overrides PatternInterface:: |
|
PatternBase:: |
public | function |
Get theme implementation for current pattern. Overrides PatternInterface:: |
1 |
PatternBase:: |
protected | function | Process libraries. | |
PatternBase:: |
protected | function | Process 'use' definition property. | |
PatternBase:: |
protected | function | Process theme variables. | |
PatternBase:: |
public | function |
UiPatternsManager constructor. Overrides PluginBase:: |
1 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |