-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathLibraryPattern.php
120 lines (108 loc) · 3.62 KB
/
LibraryPattern.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?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;
/**
* The UI Pattern plugin.
*
* ID is set to "yaml" for backward compatibility reasons.
*
* @UiPattern(
* id = "yaml",
* label = @Translation("Library Pattern"),
* description = @Translation("Pattern defined using a YAML file."),
* deriver = "\Drupal\ui_patterns_library\Plugin\Deriver\LibraryDeriver"
* )
*/
class LibraryPattern extends PatternBase {
/**
* Theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* UiPatternsManager constructor.
*/
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;
}
/**
* {@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'),
$container->get('theme_handler')
);
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Process 'custom hook theme' definition property.
*
* @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
* Pattern definition array.
*
* @return array
* Processed hook definition portion.
*/
protected function processCustomThemeHookProperty(PatternDefinition $definition) {
/** @var \Drupal\Core\Extension\Extension $module */
$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 . DIRECTORY_SEPARATOR, '', $definition->getBasePath());
}
}
return $return;
}
/**
* Weather template exists in given directory.
*
* @param string $directory
* Directory full path.
* @param string $template
* Template name, without default Twig extension.
*
* @return bool
* Weather template exists in given directory.
*/
protected function templateExists($directory, $template) {
return file_exists($directory . DIRECTORY_SEPARATOR . $template . '.html.twig');
}
/**
* Process 'template' definition property.
*
* @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
* Pattern definition array.
*
* @return array
* Processed hook definition portion.
*/
protected function processTemplateProperty(PatternDefinition $definition) {
$return = [];
if ($definition->hasTemplate()) {
$return = ['template' => $definition->getTemplate()];
}
return $return;
}
}