Skip to content
Snippets Groups Projects

Issue# 3392479 Preparing version 2

Closed Oleksii Haidabura requested to merge 3392479-roadmap-from-1.x into 1.x
1 file
+ 113
99
Compare changes
  • Side-by-side
  • Inline
@@ -2,6 +2,7 @@
namespace Drupal\component_connector;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Utility\NestedArray;
use Drupal\component_connector\Plugin\Layout\ComponentConnectorLayout;
use Drupal\Core\Config\ConfigFactoryInterface;
@@ -96,7 +97,22 @@ class ComponentConnectorManager {
}
$mask = '/^.*\.' . $type . '\.yml$/';
$theme = $this->themeHandler->getTheme($this->themeName);
return $this->fileSystem->scanDirectory($theme->getPath(), $mask, ['key' => 'name']);
$file_cache = FileCacheFactory::get('component_connector:' . $type);
$files = $this->fileSystem->scanDirectory($theme->getPath(), $mask, ['key' => 'uri']);
$all = [];
// Try to load from the file cache first.
foreach ($file_cache->getMultiple(array_keys($files)) as $file => $data) {
$all[$file] = $data;
unset($files[$file]);
}
if ($files) {
foreach ($files as $id => $file) {
$data = Yaml::decode(file_get_contents($file->uri)) ?: [];
$all[$id] = $data;
$file_cache->set($id, $data);
}
}
return $all;
}
/**
@@ -107,65 +123,73 @@ class ComponentConnectorManager {
if (!$this->themeName || $this->themeName != $active_theme_name) {
return;
}
foreach ($this->getDefinitions() as $file) {
$content = file_get_contents($file->uri);
foreach (Yaml::decode($content) as $definition) {
// Replace hooks.
$new_hook_theme = [];
if (isset($definition['hook theme'])) {
$path_parts = pathinfo($file->uri);
$template = str_replace('.theme', '', $path_parts['filename']);
$new_hook_theme['template'] = $template;
$new_hook_theme['path'] = $path_parts['dirname'];
$this->attachLibraries($definition, $new_hook_theme);
if (isset($definition['base hook']) && isset($theme_registry[$definition['base hook']])) {
$theme_registry[$definition['hook theme']] = NestedArray::mergeDeep($theme_registry[$definition['base hook']], $new_hook_theme);
$theme_registry[$definition['hook theme']]['base hook'] = $definition['base hook'];
}
elseif (isset($theme_registry[$definition['hook theme']])) {
// Replace hook theme.
$theme_registry[$definition['hook theme']] = NestedArray::mergeDeep($theme_registry[$definition['hook theme']], $new_hook_theme);
foreach ($this->getDefinitions() as $id => $definition) {
$definition = reset($definition);
// Replace hooks.
$new_hook_theme = [];
if (isset($definition['hook theme'])) {
$path_parts = pathinfo($id);
$template = str_replace('.theme', '', $path_parts['filename']);
$new_hook_theme['template'] = $template;
$new_hook_theme['path'] = $path_parts['dirname'];
$this->attachLibraries($definition, $new_hook_theme, $path_parts);
// Check for additional preprocess functions.
// Legacy to remove. See https://www.drupal.org/project/drupal/issues/2060773 for details.
if (function_exists($this->themeName . '_preprocess_' . $definition['hook theme'])) {
$new_hook_theme['preprocess functions'][] = $this->themeName . '_preprocess_' . $definition['hook theme'];
}
if (isset($definition['base hook']) && isset($theme_registry[$definition['base hook']])) {
if ($definition['base hook'] == 'layout') {
$new_hook_theme['preprocess functions'][] = [
'\Drupal\component_connector\ComponentConnectorManager',
'preprocessLayout',
];
}
else {
// Check and add variables if any.
if (isset($definition['fields'])) {
$new_hook_theme['variables'] = array_fill_keys(array_keys($definition['fields']), NULL);
if (isset($definition['settings'])) {
$new_hook_theme['variables'] += array_fill_keys(array_keys($definition['settings']), NULL);
}
$theme_registry[$definition['hook theme']] = NestedArray::mergeDeep($theme_registry[$definition['base hook']], $new_hook_theme);
$theme_registry[$definition['hook theme']]['base hook'] = $definition['base hook'];
}
elseif (isset($theme_registry[$definition['hook theme']])) {
// Replace hook theme.
$theme_registry[$definition['hook theme']] = NestedArray::mergeDeep($theme_registry[$definition['hook theme']], $new_hook_theme);
}
else {
// Check and add variables if any.
if (isset($definition['fields'])) {
$new_hook_theme['variables'] = array_fill_keys(array_keys($definition['fields']), NULL);
if (isset($definition['settings'])) {
$new_hook_theme['variables'] += array_fill_keys(array_keys($definition['settings']), NULL);
}
// Register new hook theme.
$new_hook_theme['type'] = 'theme_engine';
$theme_registry[$definition['hook theme']] = $new_hook_theme;
}
if (isset($theme_registry[$definition['hook theme']]['render element'])) {
$this->attachVariables($definition, $theme_registry[$definition['hook theme']]);
}
// Register new hook theme.
$new_hook_theme['type'] = 'theme_engine';
$theme_registry[$definition['hook theme']] = $new_hook_theme;
}
if (isset($theme_registry[$definition['hook theme']]['render element'])) {
$this->attachVariables($definition, $theme_registry[$definition['hook theme']]);
}
}
}
foreach ($this->getDefinitions('suggestion') as $file) {
foreach ($this->getDefinitions('suggestion') as $id => $definition) {
// Process template suggestion and map to component html.twig.
$content = file_get_contents($file->uri);
foreach (Yaml::decode($content) as $definition) {
if (isset($definition['base hook']) && !isset($definition['hook theme'])
&& isset($theme_registry[$definition['base hook']])) {
$path_parts = pathinfo($file->uri);
$template = str_replace('.suggestion', '', $path_parts['filename']);
$theme_registry[$definition['base hook']]['template'] = $template;
$theme_registry[$definition['base hook']]['path'] = $path_parts['dirname'];
$theme_registry[$definition['base hook']]['type'] = 'theme_engine';
$this->attachLibraries($definition, $theme_registry[$definition['base hook']]);
$this->attachVariables($definition, $theme_registry[$definition['base hook']]);
if (isset($theme_registry[$definition['base hook']]['variables'])) {
// Merge variables if any.
if (isset($definition['fields'])) {
$theme_registry[$definition['base hook']]['variables'] += array_fill_keys(array_keys($definition['fields']), NULL);
if (isset($definition['settings'])) {
$theme_registry[$definition['base hook']]['variables'] += array_fill_keys(array_keys($definition['settings']), NULL);
}
$definition = reset($definition);
if (isset($definition['base hook']) && !isset($definition['hook theme'])
&& isset($theme_registry[$definition['base hook']])) {
$path_parts = pathinfo($id);
$template = str_replace('.suggestion', '', $path_parts['filename']);
$theme_registry[$definition['base hook']]['template'] = $template;
$theme_registry[$definition['base hook']]['path'] = $path_parts['dirname'];
$theme_registry[$definition['base hook']]['type'] = 'theme_engine';
$this->attachLibraries($definition, $theme_registry[$definition['base hook']], $path_parts);
$this->attachVariables($definition, $theme_registry[$definition['base hook']]);
if (isset($theme_registry[$definition['base hook']]['variables'])) {
// Merge variables if any.
if (isset($definition['fields'])) {
$theme_registry[$definition['base hook']]['variables'] += array_fill_keys(array_keys($definition['fields']), NULL);
if (isset($definition['settings'])) {
$theme_registry[$definition['base hook']]['variables'] += array_fill_keys(array_keys($definition['settings']), NULL);
}
}
}
}
@@ -180,27 +204,32 @@ class ComponentConnectorManager {
* @param array $new_hook_theme
* An new hook theme array.
*/
private function attachLibraries(array $definition, array &$new_hook_theme) {
private function attachLibraries(array $definition, array &$new_hook_theme, $path_parts) {
$active_theme_name = $this->themeManager->getActiveTheme()->getName();
$libraries = [];
if (isset($definition['libraries'])) {
// Attach libraries.
foreach ($definition['libraries'] as $library) {
if (is_array($library)) {
$new_hook_theme['attached']['library'][] = $active_theme_name . '/' . key($library);
$libraries[] = $active_theme_name . '/' . key($library);
}
else {
$new_hook_theme['attached']['library'][] = $library;
$libraries[] = $library;
}
}
$new_hook_theme['preprocess functions'][] = [
'\Drupal\component_connector\ComponentConnectorManager',
'preprocessLibraries',
];
}
if (isset($definition['base hook']) && $definition['base hook'] == 'layout') {
// Check and attach libraries detected automatically.
$name = str_replace('.theme', '', $path_parts['filename']);
$asset = $path_parts["dirname"] . '/' . $name;
if (file_exists($asset . '.css') || file_exists($asset . '.js')) {
$libraries[] = $active_theme_name . '/' . $name;
}
if (!empty($libraries)) {
$libraries = array_unique($libraries);
$new_hook_theme['attached']['library'] = $libraries;
$new_hook_theme['preprocess functions'][] = [
'\Drupal\component_connector\ComponentConnectorManager',
'preprocessLayout',
'preprocessLibraries',
];
}
}
@@ -303,37 +332,25 @@ class ComponentConnectorManager {
public function buildLibraries(): array {
$libraries = [];
// Process components libraries.
foreach (array_merge($this->getDefinitions(), $this->getDefinitions('suggestion')) as $file) {
$content = file_get_contents($file->uri);
$path_parts = pathinfo($file->uri);
foreach (Yaml::decode($content) as $definition) {
if (isset($definition['libraries'])) {
foreach ($definition['libraries'] as $library) {
if (is_array($library)) {
$data = reset($library);
foreach (['js', 'css'] as $type) {
if (isset($data[$type])) {
if ($type == 'css' && !empty($data[$type])) {
foreach ($data[$type] as $key => $css) {
foreach ($data[$type][$key] as $subkey => $subcss) {
$data[$type][$key]['/' . $path_parts['dirname'] . '/' . $subkey] = $data[$type][$key][$subkey];
unset($data[$type][$key][$subkey]);
}
}
}
else {
foreach ($data[$type] as $key => $js) {
$data[$type]['/' . $path_parts['dirname'] . '/' . $key] = $data[$type][$key];
unset($data[$type][$key]);
}
}
}
}
// Add path prefix.
$libraries[key($library)] = $data;
}
}
}
foreach (array_merge($this->getDefinitions(), $this->getDefinitions('suggestion')) as $id => $definition) {
$definition = reset($definition);
$path_parts = pathinfo($id);
// Find and declare libraries automatically.
$name = str_replace(['.theme','.suggestion'], '', $path_parts['filename']);
$asset = $path_parts["dirname"] . '/' . $name;
$data = [];
if (file_exists($asset . '.css')) {
$data['css'] = [
'component' => [
'/' . $asset . '.css' => [],
],
];
}
if (file_exists($asset . '.js')) {
$data['js'] = ['/' . $asset . '.js' => [],];
}
if (!empty($data)) {
$libraries[$name] = $data;
}
}
return $libraries;
@@ -347,19 +364,16 @@ class ComponentConnectorManager {
*/
public function alterLayouts(array &$definitions) {
$this->themeRegistry->initDefault();
foreach ($this->getDefinitions() as $file) {
$content = file_get_contents($file->uri);
$path_parts = pathinfo($file->uri);
$pattern_definition = Yaml::decode($content);
$pattern_name = key($pattern_definition);
$pattern_definition = $pattern_definition[$pattern_name];
foreach ($this->getDefinitions() as $id => $definition) {
$pattern_name = key($definition);
$pattern_definition = $definition[$pattern_name];
if (isset($pattern_definition['base hook']) && $pattern_definition['base hook'] == 'layout') {
// Register a layout.
$definition = [
'label' => $pattern_definition['label'],
'category' => 'Layouts',
'class' => ComponentConnectorLayout::class,
'path' => $path_parts['dirname'],
'path' => pathinfo($id)['dirname'],
'theme_hook' => $pattern_definition['hook theme'],
'provider' => 'component_connector',
];
Loading