-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathLibraryPattern.php
141 lines (127 loc) · 4.07 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?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) {
if (!$definition->hasCustomThemeHook()) {
if ($this->templateExists($definition->getBasePath(), $definition->getTemplate())) {
return ['path' => str_replace($this->root, '', $definition->getBasePath())];
}
$provider = $definition->getProvider();
$extension = $this->getProviderExtension($provider);
if ($extension) {
return ['path' => $extension->getPath() . '/templates'];
}
}
return [];
}
/**
* Get the extension (module or theme) for the given provider.
*
* @param string $provider
* The name of the provider.
*
* @return \Drupal\Core\Extension\Extension|null
* The extension or NULL if none found.
*/
protected function getProviderExtension($provider) {
if ($this->moduleHandler->moduleExists($provider)) {
return $this->moduleHandler->getModule($provider);
}
elseif ($this->themeHandler->themeExists($provider)) {
return $this->themeHandler->getTheme($provider);
}
return NULL;
}
/**
* 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;
}
}