Skip to content
Snippets Groups Projects
Commit 974925f5 authored by Antonio De Marco's avatar Antonio De Marco
Browse files

Refactor UiPatternBase::getThemeImplementation() #91

parent 1946b65e
No related branches found
No related tags found
No related merge requests found
......@@ -23,25 +23,17 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class LibraryPattern extends UiPatternBase {
/**
* The theme handler.
* Theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* UiPatternsManager constructor.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, TypedDataManager $typed_data_manager, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $root, $typed_data_manager);
$this->moduleHandler = $module_handler;
parent::__construct($configuration, $plugin_id, $plugin_definition, $root, $typed_data_manager, $module_handler);
$this->themeHandler = $theme_handler;
}
......@@ -64,23 +56,11 @@ class LibraryPattern extends UiPatternBase {
* {@inheritdoc}
*/
public function getThemeImplementation() {
$item = [];
$item = parent::getThemeImplementation();
$definition = $this->getPluginDefinition();
foreach ($definition['fields'] as $field) {
$item['variables'][$field['name']] = NULL;
}
$item['variables']['attributes'] = [];
$item['variables']['context'] = [];
$item['variables']['use'] = '';
$item += $this->processUseProperty($definition);
$item += $this->processCustomThemeHookProperty($definition);
$item += $this->processTemplateProperty($definition);
return [
$definition['theme hook'] => $item,
];
$item[$definition['theme hook']] += $this->processCustomThemeHookProperty($definition);
$item[$definition['theme hook']] += $this->processTemplateProperty($definition);
return $item;
}
/**
......@@ -119,24 +99,4 @@ class LibraryPattern extends UiPatternBase {
return $return;
}
/**
* Process 'use' definition property.
*
* @param array $definition
* Pattern definition array.
*
* @return array
* Processed hook definition portion.
*/
protected function processUseProperty(array $definition) {
$return = [];
if (!empty($definition['use'])) {
$return = [
'path' => $this->moduleHandler->getModule('ui_patterns')->getPath() . '/templates',
'template' => 'patterns-use-wrapper',
];
}
return $return;
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\ui_patterns;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\TypedData\TypedDataManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -33,13 +34,21 @@ abstract class UiPatternBase extends PluginBase implements UiPatternInterface, C
*/
protected $typedDataManager;
/**
* Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* UiPatternsManager constructor.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, TypedDataManager $typed_data_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, TypedDataManager $typed_data_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->root = $root;
$this->typedDataManager = $typed_data_manager;
$this->moduleHandler = $module_handler;
}
/**
......@@ -51,7 +60,8 @@ abstract class UiPatternBase extends PluginBase implements UiPatternInterface, C
$plugin_id,
$plugin_definition,
$container->get('app.root'),
$container->get('typed_data_manager')
$container->get('typed_data_manager'),
$container->get('module_handler')
);
}
......@@ -192,6 +202,19 @@ abstract class UiPatternBase extends PluginBase implements UiPatternInterface, C
return $value;
}
/**
* {@inheritdoc}
*/
public function getThemeImplementation() {
$definition = $this->getPluginDefinition();
$item = [];
$item += $this->processVariables($definition);
$item += $this->processUseProperty($definition);
return [
$definition['theme hook'] => $item,
];
}
/**
* {@inheritdoc}
*/
......@@ -247,4 +270,44 @@ abstract class UiPatternBase extends PluginBase implements UiPatternInterface, C
}
}
/**
* Process 'use' definition property.
*
* @param array $definition
* Pattern definition array.
*
* @return array
* Processed hook definition portion.
*/
protected function processUseProperty(array $definition) {
$return = [];
if (!empty($definition['use'])) {
$return = [
'path' => $this->moduleHandler->getModule('ui_patterns')->getPath() . '/templates',
'template' => 'patterns-use-wrapper',
];
}
return $return;
}
/**
* Process theme variables.
*
* @param array $definition
* Pattern definition array.
*
* @return array
* Processed hook definition portion.
*/
protected function processVariables(array $definition) {
$return = [];
foreach ($definition['fields'] as $field) {
$return['variables'][$field['name']] = NULL;
}
$return['variables']['attributes'] = [];
$return['variables']['context'] = [];
$return['variables']['use'] = '';
return $return;
}
}
......@@ -75,19 +75,23 @@ class UiPatternBaseTest extends AbstractUiPatternsTest {
/**
* Get UiPatternBase mock.
*
* @param array $configuration
* Plugin configuration.
* @param array $plugin_definition
* Plugin definition.
* @param array $methods
* List of methods to mock.
*
* @return \PHPUnit_Framework_MockObject_MockObject
* Mock object.
*/
protected function getUiPatternBaseMock(array $configuration = [], array $methods = []) {
$root = \Drupal::service('app.root');
$typed_data_manager = \Drupal::service('typed_data_manager');
$arguments = [[], 'plugin_id', $configuration, $root, $typed_data_manager];
return $this->getMockForAbstractClass(UiPatternBase::class, $arguments, '', TRUE, TRUE, TRUE, $methods);
protected function getUiPatternBaseMock(array $plugin_definition = [], array $methods = []) {
return $this->getMockForAbstractClass(UiPatternBase::class, [
[],
'plugin_id',
$plugin_definition,
\Drupal::service('app.root'),
\Drupal::service('typed_data_manager'),
\Drupal::service('module_handler'),
], '', TRUE, TRUE, TRUE, $methods);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment