Skip to content

Commit a45c739

Browse files
wengerkWengerK
authored andcommitted
Issue #3087804 by wengerk, AurianaHg: Support for Page level suggestion
1 parent 9ed9915 commit a45c739

File tree

5 files changed

+159
-63
lines changed

5 files changed

+159
-63
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ CHANGELOG
33

44
## NEXT RELEASE
55
- Issue #3008554 by wengerk, valthebald: Display widget in advanced group only when it exists.
6-
- close #3044924 - fix Drupal-CI Composer failure since Drupal 8.7.x+ - Update of drupal/coder squizlabs/php_codesniffer"
7-
6+
- Issue #3044924 - fix Drupal-CI Composer failure since Drupal 8.7.x+ - Update of drupal/coder squizlabs/php_codesniffer"
7+
- Issue #3087804 by wengerk, AurianaHg: Support for Page level suggestion
8+
89
## 8.x-2.2 (2018-06-05)
910
- Fix Token suggestion my crash on unused suggestion lookup - Issue 2974817.
1011
- Add better warning of suggestion usage before deletion.

inc/suggestions/entity.inc

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains function to generate suggestions on the page level.
6+
*/
7+
8+
use Drupal\Core\Entity\ContentEntityInterface;
9+
10+
/**
11+
* Method used in the template_whisperer_theme_suggestions_alter().
12+
*
13+
* Generate additional suggestions based on the entity.
14+
* - node--1--article--teaser--suggestion.html.twig
15+
* - node--article--teaser--suggestion.html.twig
16+
* - node--1--article--suggestion.html.twig
17+
* - node--article--suggestion.html.twig.
18+
*/
19+
function _template_whisperer_theme_suggestions_entity(array &$suggestions, array $variables, $hook) {
20+
if (isset($variables['elements']['#entity_type']) && $variables['elements']['#entity_type'] === $hook) {
21+
$twManager = \Drupal::service('plugin.manager.template_whisperer');
22+
23+
$entity = NULL;
24+
if (isset($variables['elements']['#' . $hook])) {
25+
$entity = $variables['elements']['#' . $hook];
26+
}
27+
28+
if (!empty($entity) && $entity instanceof ContentEntityInterface) {
29+
$suggestionsEntity = $twManager->suggestionsFromEntity($entity);
30+
31+
if (!empty($suggestionsEntity)) {
32+
33+
// Expose one suggestion by fields.
34+
foreach ($suggestionsEntity as $suggestion) {
35+
// Collect every suggestions variants that will be injected.
36+
$base_suggestions = [];
37+
38+
// Will suggest node--article--suggestion.html.twig.
39+
$base_suggestions[] = [
40+
$entity->getEntityTypeId(),
41+
$entity->bundle(),
42+
];
43+
44+
// Will suggest node--1--article--suggestion.html.twig.
45+
$base_suggestions[] = [
46+
$entity->getEntityTypeId(),
47+
$entity->id(),
48+
$entity->bundle(),
49+
];
50+
51+
// Colect variation based on the view_mode.
52+
if (isset($variables['elements']['#view_mode'])) {
53+
// Will suggest node--article--teaser--suggestion.html.twig.
54+
$base_suggestions[] = [
55+
$entity->getEntityTypeId(),
56+
$entity->bundle(),
57+
$variables['elements']['#view_mode'],
58+
];
59+
// Will suggest node--1--article--teaser--suggestion.html.twig.
60+
$base_suggestions[] = [
61+
$entity->getEntityTypeId(),
62+
$entity->id(),
63+
$entity->bundle(),
64+
$variables['elements']['#view_mode'],
65+
];
66+
}
67+
68+
// Generate suggestions for every variations.
69+
foreach ($base_suggestions as $base_suggestion) {
70+
$suggestions[] = implode('__', $base_suggestion) . '__' . $suggestion;
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}

inc/suggestions/page.inc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains function to generate suggestions on the page level.
6+
*/
7+
8+
use Drupal\Core\Entity\ContentEntityInterface;
9+
10+
/**
11+
* Method used in the template_whisperer_theme_suggestions_alter().
12+
*
13+
* Generate additional suggestions based on the page.
14+
* - page--node--1--suggestion.html.twig
15+
* - page--node--suggestion.html.twig.
16+
*/
17+
function _template_whisperer_theme_suggestions_page(array &$suggestions, array $variables, $hook) {
18+
if ($hook !== 'page') {
19+
return;
20+
}
21+
22+
// Get the entity from the Route parameters.
23+
$entity = NULL;
24+
foreach (\Drupal::routeMatch()->getParameters() as $param) {
25+
if ($param instanceof ContentEntityInterface) {
26+
$entity = $param;
27+
break;
28+
}
29+
}
30+
31+
if (!$entity) {
32+
return;
33+
}
34+
35+
$twManager = \Drupal::service('plugin.manager.template_whisperer');
36+
$suggestionsEntity = $twManager->suggestionsFromEntity($entity);
37+
38+
if (empty($suggestionsEntity)) {
39+
return;
40+
}
41+
42+
// Expose one suggestion by fields.
43+
foreach ($suggestionsEntity as $suggestion) {
44+
// Collect every suggestions variants that will be injected.
45+
$base_suggestions = [];
46+
47+
// Will suggest page--node--1--suggestion.html.twig.
48+
$base_suggestions[] = [
49+
$hook,
50+
$entity->getEntityTypeId(),
51+
$entity->id(),
52+
];
53+
54+
// Will suggest page--node--suggestion.html.twig.
55+
$base_suggestions[] = [
56+
$hook,
57+
$entity->getEntityTypeId(),
58+
];
59+
60+
// Generate suggestions for every variations.
61+
foreach ($base_suggestions as $base_suggestion) {
62+
$suggestions[] = implode('__', $base_suggestion) . '__' . $suggestion;
63+
}
64+
}
65+
}

template_whisperer.module

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,17 @@
55
* Contains template_whisperer.module.
66
*/
77

8-
use Drupal\Core\Entity\ContentEntityInterface;
8+
require_once 'inc/suggestions/page.inc';
9+
require_once 'inc/suggestions/entity.inc';
910

1011
/**
1112
* Implements hook_theme_suggestions_alter().
1213
*/
1314
function template_whisperer_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
14-
if (isset($variables['elements']['#entity_type']) && $variables['elements']['#entity_type'] === $hook) {
15-
$twManager = \Drupal::service('plugin.manager.template_whisperer');
16-
17-
$entity = NULL;
18-
if (isset($variables['elements']['#' . $hook])) {
19-
$entity = $variables['elements']['#' . $hook];
20-
}
21-
22-
if (!empty($entity) && $entity instanceof ContentEntityInterface) {
23-
$suggestionsEntity = $twManager->suggestionsFromEntity($entity);
24-
25-
if (!empty($suggestionsEntity)) {
26-
27-
// Expose one suggestion by fields.
28-
foreach ($suggestionsEntity as $suggestion) {
29-
// Collect every suggestions variants that will be injected.
30-
$base_suggestions = [];
31-
32-
// Will suggest node--article--suggestion.html.twig.
33-
$base_suggestions[] = [
34-
$entity->getEntityTypeId(),
35-
$entity->bundle(),
36-
];
37-
38-
// Will suggest node--1--article--suggestion.html.twig.
39-
$base_suggestions[] = [
40-
$entity->getEntityTypeId(),
41-
$entity->id(),
42-
$entity->bundle(),
43-
];
44-
45-
// Colect variation based on the view_mode.
46-
if (isset($variables['elements']['#view_mode'])) {
47-
// Will suggest node--article--teaser--suggestion.html.twig.
48-
$base_suggestions[] = [
49-
$entity->getEntityTypeId(),
50-
$entity->bundle(),
51-
$variables['elements']['#view_mode'],
52-
];
53-
// Will suggest node--1--article--teaser--suggestion.html.twig.
54-
$base_suggestions[] = [
55-
$entity->getEntityTypeId(),
56-
$entity->id(),
57-
$entity->bundle(),
58-
$variables['elements']['#view_mode'],
59-
];
60-
}
61-
62-
// Generate suggestions for every variations.
63-
foreach ($base_suggestions as $base_suggestion) {
64-
$suggestions[] = implode('__', $base_suggestion) . '__' . $suggestion;
65-
}
66-
}
67-
}
68-
}
69-
}
15+
_template_whisperer_theme_suggestions_page($suggestions, $variables, $hook);
16+
_template_whisperer_theme_suggestions_entity($suggestions, $variables, $hook);
7017
}
7118

72-
// Includes.
7319
require_once 'inc/rm_cardinality.inc';
7420
require_once 'inc/help.inc';
7521
require_once 'inc/tokens.inc';

tests/src/Functional/UiFieldTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,19 @@ public function testFieldSaved() {
226226
// Asserts the debug mode of twig is enabled.
227227
$this->assertTrue(strpos($output->getContent(), '<!-- THEME HOOK: \'node\' -->') !== FALSE);
228228

229-
// Asserts that all Template Whisperer based suggestions are present.
229+
// Asserts that all Page Template Whisperer based suggestions are present.
230+
$this->assertTrue(strpos($output->getContent(), '* page--node--1--googlemap.html.twig') !== FALSE);
231+
$this->assertTrue(strpos($output->getContent(), '* page--node--googlemap.html.twig') !== FALSE);
232+
233+
// Asserts that all Entity Template Whisperer based suggestions are present.
230234
$this->assertTrue(strpos($output->getContent(), '* node--article--googlemap.html.twig') !== FALSE);
231235
$this->assertTrue(strpos($output->getContent(), '* node--1--article--googlemap.html.twig') !== FALSE);
232236
$this->assertTrue(strpos($output->getContent(), '* node--article--full--googlemap.html.twig') !== FALSE);
233237
$this->assertTrue(strpos($output->getContent(), '* node--1--article--full--googlemap.html.twig') !== FALSE);
234238
}
235239

236240
/**
237-
* Tests the Node whitout Template saved, don't suggestion it.
241+
* Tests the Node whiteout Template saved, don't suggestion it.
238242
*/
239243
public function testFieldWhitoutTemplate() {
240244
$article = $this->container->get('entity_type.manager')->getStorage('node')
@@ -256,7 +260,11 @@ public function testFieldWhitoutTemplate() {
256260
// Asserts the debug mode of twig is enabled.
257261
$this->assertTrue(strpos($output->getContent(), '<!-- THEME HOOK: \'node\' -->') !== FALSE);
258262

259-
// Asserts that all Template Whisperer based suggestions are not present.
263+
// Asserts that Page Template Whisperer based suggestions are not present.
264+
$this->assertTrue(strpos($output->getContent(), '* page--node--1--googlemap.html.twig') === FALSE);
265+
$this->assertTrue(strpos($output->getContent(), '* page--node--googlemap.html.twig') === FALSE);
266+
267+
// Asserts that Entity Template Whisperer based suggestions are not present.
260268
$this->assertTrue(strpos($output->getContent(), '* node--article--googlemap.html.twig') === FALSE);
261269
$this->assertTrue(strpos($output->getContent(), '* node--1--article--googlemap.html.twig') === FALSE);
262270
$this->assertTrue(strpos($output->getContent(), '* node--article--full--googlemap.html.twig') === FALSE);

0 commit comments

Comments
 (0)