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

Add library processing and tests #34

parent d5f0d42b
No related branches found
No related tags found
No related merge requests found
......@@ -24,6 +24,11 @@ class UiPatternsManager extends DefaultPluginManager implements UiPatternsManage
*/
const TWIG_EXTENSION = '.html.twig';
/**
* Pattern prefix.
*/
const PATTERN_PREFIX = 'pattern_';
/**
* The app root.
*
......@@ -64,6 +69,7 @@ class UiPatternsManager extends DefaultPluginManager implements UiPatternsManage
'fields' => [],
'libraries' => [],
'extra' => [],
'base path' => '',
];
/**
......@@ -100,23 +106,13 @@ class UiPatternsManager extends DefaultPluginManager implements UiPatternsManage
$definition['custom theme hook'] = TRUE;
if (empty($definition['theme hook'])) {
$definition['theme hook'] = "pattern_{$plugin_id}";
$definition['theme hook'] = self::PATTERN_PREFIX . $plugin_id;
$definition['custom theme hook'] = FALSE;
}
$definition['theme variables'] = array_fill_keys(array_keys($definition['fields']), NULL);
$definition['theme variables']['attributes'] = [];
$definition['theme variables']['context'] = [];
// @todo: Move this into process class.
// @todo: Consider external libraries in processing base path.
$definition['libraries'] = array_map(function ($value) use ($definition) {
if (is_array($value)) {
$name = array_keys($value);
$value = $definition['provider'] . '/pattern_' . array_shift($name);
}
return $value;
}, $definition['libraries']);
}
/**
......@@ -177,6 +173,49 @@ class UiPatternsManager extends DefaultPluginManager implements UiPatternsManage
return $items;
}
/**
* {@inheritdoc}
*/
public function hookLibraryInfoBuild() {
// @codingStandardsIgnoreStart
$libraries = [];
foreach ($this->getDefinitions() as $definition) {
// Get only locally defined libraries.
$items = array_filter($definition['libraries'], function ($library) {
return is_array($library);
});
// Attach pattern base path to assets.
if (!empty($definition['base path'])) {
$base_path = $definition['base path'];
$process = function (&$items) use (&$process, $base_path) {
foreach ($items as $name => $values) {
$is_asset = stristr($name, '.css') !== FALSE || stristr($name, '.js') !== FALSE;
$is_external = isset($values['type']) && $values['type'] == 'external';
if ($is_asset && !$is_external) {
$items[$base_path . DIRECTORY_SEPARATOR . $name] = $values;
unset($items[$name]);
}
elseif (!$is_asset) {
$process($items[$name]);
}
}
};
$process($items);
}
// Produce final libraries array.
$id = $definition['id'];
array_walk($items, function ($value) use (&$libraries, $id) {
$libraries[$id . '.' . key($value)] = reset($value);
});
}
// @codingStandardsIgnoreEnd
return $libraries;
}
/**
* {@inheritdoc}
*/
......
......@@ -38,6 +38,17 @@ interface UiPatternsManagerInterface extends PluginManagerInterface {
*/
public function hookTheme();
/**
* Get patterns library info.
*
* @return array
* Array of libraries as expected by hook_library_info_build().
*
* @see hook_library_info_build()
* @see ui_patterns_library_info_build()
*/
public function hookLibraryInfoBuild();
/**
* Check whereas the given theme hook is an actual pattern hook.
*
......
......@@ -94,6 +94,24 @@ class UiPatternsManagerTest extends AbstractUiPatternsTest {
}
}
/**
* Test hookLibraryInfoBuild.
*
* @covers ::hookLibraryInfoBuild
*/
public function testHookLibraryInfoBuild() {
$items = Yaml::decode(file_get_contents(dirname(__FILE__) . '/fixtures/libraries.yml'));
foreach ($items as $item) {
$manager = $this->createPartialMock(UiPatternsManager::class, ['getDefinitions']);
$manager->method('getDefinitions')->willReturn([$item['actual']]);
/** @var \Drupal\ui_patterns\UiPatternsManager $manager */
$libraries = $manager->hookLibraryInfoBuild();
assert($libraries, equals($item['expected']));
}
}
/**
* Pattern definitions data provider.
*
......
#-
# actual:
# id: pattern_name
# base path: /pattern/base/path
# libraries:
# - drupal/library_one
# - drupal/library_two
# - library_one:
# css:
# component:
# library_one.css: {}
# library_two.css: {}
# library_three.css: {}
# theme:
# library_one.css: {}
# library_two.css: {}
# js:
# library_one.js: {}
# - library_two:
# css:
# component:
# library_one.css: {}
# js:
# library_two.js: {}
# expected:
# pattern_name.library_one:
# css:
# component:
# /pattern/base/path/library_one.css: {}
# /pattern/base/path/library_two.css: {}
# /pattern/base/path/library_three.css: {}
# theme:
# /pattern/base/path/library_one.css: {}
# /pattern/base/path/library_two.css: {}
# js:
# /pattern/base/path/library_one.js: {}
# pattern_name.library_two:
# css:
# component:
# /pattern/base/path/library_one.css: {}
# js:
# /pattern/base/path/library_two.js: {}
-
actual:
id: pattern_name
base path: /pattern/base/path
libraries:
- library_one:
css:
component:
library_one.css: {}
http://example.com/external.min.css: { type: external, minified: true }
library_two.css: {}
theme:
http://example.com/external.min.css: { type: external, minified: true }
js:
library_one.js: {}
http://example.com/external.min.js: { type: external, minified: true }
library_two.js: {}
expected:
pattern_name.library_one:
css:
component:
/pattern/base/path/library_one.css: {}
http://example.com/external.min.css: { type: external, minified: true }
/pattern/base/path/library_two.css: {}
theme:
http://example.com/external.min.css: { type: external, minified: true }
js:
/pattern/base/path/library_one.js: {}
http://example.com/external.min.js: { type: external, minified: true }
/pattern/base/path/library_two.js: {}
#
#-
# actual:
# id: pattern_name
# libraries:
# - drupal/library_one
# - drupal/library_two
# expected: []
#
#
#-
# actual:
# id: pattern_name
# base path: ''
# libraries:
# - library_one:
# css:
# component:
# library_one.css: {}
# js:
# library_one.js: {}
# - library_two:
# css:
# component:
# library_one.css: {}
# js:
# library_two.js: {}
# expected:
# pattern_name.library_one:
# css:
# component:
# library_one.css: {}
# js:
# library_one.js: {}
# pattern_name.library_two:
# css:
# component:
# library_one.css: {}
# js:
# library_two.js: {}
......@@ -27,6 +27,14 @@ function ui_patterns_theme() {
*/
function ui_patterns_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if (\Drupal::service('plugin.manager.ui_patterns')->isPatternHook($hook) && !empty($variables['context'])) {
\Drupal::service('module_handler')->alter('ui_patterns_suggestions', $suggestions, $variables, $variables['context']);
\Drupal::moduleHandler()->alter('ui_patterns_suggestions', $suggestions, $variables, $variables['context']);
}
}
/**
* Implements hook_library_info_build()
*/
function ui_patterns_library_info_build() {
return \Drupal::service('plugin.manager.ui_patterns')->hookLibraryInfoBuild();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment