LibraryPattern.php in UI Patterns 8
File
modules/ui_patterns_library/src/Plugin/UiPatterns/Pattern/LibraryPattern.php
View source
<?php
namespace Drupal\ui_patterns_library\Plugin\UiPatterns\Pattern;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\ui_patterns\Definition\PatternDefinition;
use Drupal\ui_patterns\Plugin\PatternBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LibraryPattern extends PatternBase {
protected $themeHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, $root, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $root, $module_handler);
$this->themeHandler = $theme_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'), $container
->get('theme_handler'));
}
public function getThemeImplementation() {
$item = parent::getThemeImplementation();
$definition = $this
->getPluginDefinition();
$item[$definition['theme hook']] += $this
->processTemplateProperty($definition);
$item[$definition['theme hook']] += $this
->processCustomThemeHookProperty($definition);
return $item;
}
protected function processCustomThemeHookProperty(PatternDefinition $definition) {
$return = [];
if (!$definition
->hasCustomThemeHook() && $this->moduleHandler
->moduleExists($definition
->getProvider())) {
$module = $this->moduleHandler
->getModule($definition
->getProvider());
$return['path'] = $module
->getPath() . '/templates';
if ($this
->templateExists($definition
->getBasePath(), $definition
->getTemplate())) {
$return['path'] = str_replace($this->root, '', $definition
->getBasePath());
}
}
return $return;
}
protected function templateExists($directory, $template) {
return file_exists($directory . DIRECTORY_SEPARATOR . $template . '.html.twig');
}
protected function processTemplateProperty(PatternDefinition $definition) {
$return = [];
if ($definition
->hasTemplate()) {
$return = [
'template' => $definition
->getTemplate(),
];
}
return $return;
}
}