From 19a9c36b8bba489f1c9d0cd2a6c372d43fc2318e Mon Sep 17 00:00:00 2001 From: Gab Date: Thu, 5 Sep 2019 13:43:05 +0100 Subject: [PATCH 1/3] Sanitise '-' when using pattern id as theme hook --- src/Definition/PatternDefinition.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Definition/PatternDefinition.php b/src/Definition/PatternDefinition.php index 07bf07c2..6d3a61a1 100644 --- a/src/Definition/PatternDefinition.php +++ b/src/Definition/PatternDefinition.php @@ -65,7 +65,10 @@ public function __construct(array $definition = []) { $this->id = $this->definition['id']; $this->setFields($this->definition['fields']); $this->setVariants($this->definition['variants']); - $this->setThemeHook(self::PATTERN_PREFIX . $this->id()); + // As we know allow hyphens the pattern ID, we need to sanitise it when + // setting pattern theme hook. + $hookFriendlyId = str_replace('-', '_', $this->id()); + $this->setThemeHook(self::PATTERN_PREFIX . $hookFriendlyId); if (!empty($definition['theme hook'])) { $this->setThemeHook($definition['theme hook']); From fc231ec2657318a017bb14b1745ecc8ebc699c13 Mon Sep 17 00:00:00 2001 From: Gabriele Maira Date: Tue, 29 Oct 2019 16:19:17 +0000 Subject: [PATCH 2/3] Fixing change requests #270 --- src/Definition/PatternDefinition.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Definition/PatternDefinition.php b/src/Definition/PatternDefinition.php index 6d3a61a1..a48ef218 100644 --- a/src/Definition/PatternDefinition.php +++ b/src/Definition/PatternDefinition.php @@ -65,10 +65,10 @@ public function __construct(array $definition = []) { $this->id = $this->definition['id']; $this->setFields($this->definition['fields']); $this->setVariants($this->definition['variants']); - // As we know allow hyphens the pattern ID, we need to sanitise it when - // setting pattern theme hook. - $hookFriendlyId = str_replace('-', '_', $this->id()); - $this->setThemeHook(self::PATTERN_PREFIX . $hookFriendlyId); + // As we now allow hyphens in pattern IDs we need to turn them into + // underscores when setting the theme hook. + $hook_friendly_id = str_replace('-', '_', $this->id()); + $this->setThemeHook(self::PATTERN_PREFIX . $hook_friendly_id); if (!empty($definition['theme hook'])) { $this->setThemeHook($definition['theme hook']); From e66667d20562b7d5e6ff20fd279051072a8b491c Mon Sep 17 00:00:00 2001 From: Gabriele Maira Date: Wed, 30 Oct 2019 15:31:11 +0000 Subject: [PATCH 3/3] Adding test coverage for patter template override --- .../templates/pattern-foo-bar.html.twig | 2 +- .../src/Functional/UiPatternsPreviewRenderTest.php | 13 +++++++++++++ ...tern-foo-bar--variant-default--preview.html.twig | 1 + .../ui_patterns_theme_test.info.yml | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/themes/ui_patterns_theme_test/pattern-foo-bar--variant-default--preview.html.twig create mode 100644 tests/themes/ui_patterns_theme_test/ui_patterns_theme_test.info.yml diff --git a/tests/modules/ui_patterns_render_test/templates/pattern-foo-bar.html.twig b/tests/modules/ui_patterns_render_test/templates/pattern-foo-bar.html.twig index 76c7ac2d..3e17bead 100644 --- a/tests/modules/ui_patterns_render_test/templates/pattern-foo-bar.html.twig +++ b/tests/modules/ui_patterns_render_test/templates/pattern-foo-bar.html.twig @@ -1 +1 @@ -Foo Bar +Foo Bar default template diff --git a/tests/src/Functional/UiPatternsPreviewRenderTest.php b/tests/src/Functional/UiPatternsPreviewRenderTest.php index 9d93928f..53aaa17b 100644 --- a/tests/src/Functional/UiPatternsPreviewRenderTest.php +++ b/tests/src/Functional/UiPatternsPreviewRenderTest.php @@ -59,6 +59,19 @@ public function testPatternPreviewSuggestions() { foreach ($suggestions as $suggestion) { $assert_session->responseContains($suggestion); } + + // Pattern foo-bar default template is loaded. + $assert_session->pageTextContains('Foo Bar default template'); + $assert_session->pageTextNotContains('Foo Bar overridden template'); + + // Install test theme to load template(s) overrides. + $this->container->get('theme_installer')->install(['ui_patterns_theme_test']); + $this->container->get('theme_handler')->setDefault('ui_patterns_theme_test'); + $this->container->set('theme.registry', NULL); + + $this->drupalGet('/patterns'); + $assert_session->pageTextContains('Foo Bar overridden template'); + $assert_session->pageTextNotContains('Foo Bar default template'); } } diff --git a/tests/themes/ui_patterns_theme_test/pattern-foo-bar--variant-default--preview.html.twig b/tests/themes/ui_patterns_theme_test/pattern-foo-bar--variant-default--preview.html.twig new file mode 100644 index 00000000..d59b82f0 --- /dev/null +++ b/tests/themes/ui_patterns_theme_test/pattern-foo-bar--variant-default--preview.html.twig @@ -0,0 +1 @@ +Foo Bar overridden template diff --git a/tests/themes/ui_patterns_theme_test/ui_patterns_theme_test.info.yml b/tests/themes/ui_patterns_theme_test/ui_patterns_theme_test.info.yml new file mode 100644 index 00000000..d1087c79 --- /dev/null +++ b/tests/themes/ui_patterns_theme_test/ui_patterns_theme_test.info.yml @@ -0,0 +1,4 @@ +name: 'UI Patterns Test theme' +type: theme +description: 'Theme for testing UI Patterns features in action on a theme.' +core: 8.x