Skip to content
This repository was archived by the owner on Jan 5, 2018. It is now read-only.

Commit 607254d

Browse files
paranojikslashrsm
authored andcommitted
Issue #2712111 by paranojik, slashrsm, Dave Reid, Wim Leers: Fix Embed specific attributes are cached along with the entity.
1 parent 63ce40c commit 607254d

File tree

6 files changed

+102
-9
lines changed

6 files changed

+102
-9
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ Entity Embed can be installed via the
3434
*Warning: If you were using the module in very early pre-alpha
3535
stages you might need to add `data-entity-id` to the list of allowed
3636
attributes.*
37+
* If you're using both the 'Align images' and 'Caption images' filters make
38+
sure the 'Align images' filter is run before the 'Caption images' filter in
39+
the **Filter processing order** section. (Explanation: Due to the
40+
implementation details of the two filters it is important to execute them in
41+
the right sequence in order to obtain a sensible final markup. In practice
42+
this means that the alignment filter has to be run before the caption
43+
filter, otherwise the alignment class will appear inside the <figure> tag
44+
(instead of appearing on it) the caption filter produces.)
3745

3846
## Usage
3947

entity_embed.module

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66
* format.
77
*/
88

9+
/**
10+
* Implements hook_theme().
11+
*/
12+
function entity_embed_theme() {
13+
return [
14+
'entity_embed_container' => [
15+
'render element' => 'element',
16+
],
17+
];
18+
}
19+
20+
/**
21+
* Prepares variables for entity embed container templates.
22+
*
23+
* Default template: entity-embed-container.html.twig.
24+
*
25+
* @param array $variables
26+
* An associative array containing:
27+
* - element: An associative array containing the properties of the element.
28+
* Properties used: #attributes, #children.
29+
*/
30+
function template_preprocess_entity_embed_container(&$variables) {
31+
$variables['element'] += ['#attributes' => []];
32+
$variables['attributes'] = $variables['element']['#attributes'];
33+
$variables['children'] = $variables['element']['#children'];
34+
}
35+
936
/**
1037
* Implements hook_entity_embed_display_plugins_alter() on behalf of file.module.
1138
*/

src/EntityHelperTrait.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,13 @@ protected function renderEntityEmbed(EntityInterface $entity, array $context = a
193193

194194
// Build and render the Entity Embed Display plugin, allowing modules to
195195
// alter the result before rendering.
196-
$build = $this->renderEntityEmbedDisplayPlugin(
196+
$build = array(
197+
'#theme_wrappers' => ['entity_embed_container'],
198+
'#attributes' => ['class' => ['embedded-entity']],
199+
'#entity' => $entity,
200+
'#context' => $context,
201+
);
202+
$build['entity'] = $this->renderEntityEmbedDisplayPlugin(
197203
$entity,
198204
$context['data-entity-embed-display'],
199205
$context['data-entity-embed-settings'],
@@ -213,11 +219,6 @@ protected function renderEntityEmbed(EntityInterface $entity, array $context = a
213219
$build['#attributes']['data-caption'] = $context['data-caption'];
214220
}
215221

216-
// If this is an image, the image_formatter template expects #item_attributes.
217-
if (!empty($build['#theme']) && !empty($build['#attributes']) && $build['#theme'] == 'image_formatter') {
218-
$build['#item_attributes'] = $build['#attributes'];
219-
}
220-
221222
// @todo Should this hook get invoked if $build is an empty array?
222223
$this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed", 'entity_embed'), $build, $entity, $context);
223224
$entity_output = $this->renderer()->render($build);

src/Tests/EntityEmbedFilterTest.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
*/
1515
class EntityEmbedFilterTest extends EntityEmbedTestBase {
1616

17+
/**
18+
* Modules to enable.
19+
*
20+
* @var array
21+
*/
22+
public static $modules = [
23+
'file',
24+
'image',
25+
'entity_embed',
26+
'entity_embed_test',
27+
'node',
28+
'ckeditor',
29+
];
30+
1731
/**
1832
* Tests the entity_embed filter.
1933
*
@@ -32,6 +46,7 @@ public function testFilter() {
3246
$this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
3347
$this->assertText($this->node->body->value, 'Embedded node exists in page');
3448
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
49+
$this->assertRaw('<article class="embedded-entity">', 'Embed container found.');
3550

3651
// Tests entity embed using entity UUID and view mode.
3752
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-view-mode="teaser">This placeholder should not be rendered.</drupal-entity>';
@@ -44,6 +59,7 @@ public function testFilter() {
4459
$this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
4560
$this->assertText($this->node->body->value, 'Embedded node exists in page.');
4661
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
62+
$this->assertRaw('<article class="embedded-entity">', 'Embed container found.');
4763

4864
// Ensure that placeholder is not replaced when embed is unsuccessful.
4965
$content = '<drupal-entity data-entity-type="node" data-entity-id="InvalidID" data-view-mode="teaser">This placeholder should be rendered since specified entity does not exists.</drupal-entity>';
@@ -69,6 +85,7 @@ public function testFilter() {
6985
$this->assertText($this->node->body->value, 'Entity specifed with UUID exists in the page.');
7086
$this->assertNoText($sample_node->body->value, 'Entity specifed with ID does not exists in the page.');
7187
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
88+
$this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
7289

7390
// Test deprecated 'default' Entity Embed Display plugin.
7491
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="default" data-entity-embed-settings=\'{"view_mode":"teaser"}\'>This placeholder should not be rendered.</drupal-entity>';
@@ -80,10 +97,11 @@ public function testFilter() {
8097
$this->drupalGet('node/' . $node->id());
8198
$this->assertText($this->node->body->value, 'Embedded node exists in page.');
8299
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
100+
$this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
83101

84102
// Ensure that Entity Embed Display plugin is preferred over view mode when
85103
// both attributes are present.
86-
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="default" data-entity-embed-settings=\'{"view_mode":"teaser"}\' data-view-mode="some-invalid-view-mode">This placeholder should not be rendered.</drupal-entity>';
104+
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="default" data-entity-embed-settings=\'{"view_mode":"full"}\' data-view-mode="some-invalid-view-mode" data-align="left" data-caption="test caption">This placeholder should not be rendered.</drupal-entity>';
87105
$settings = array();
88106
$settings['type'] = 'page';
89107
$settings['title'] = 'Test entity embed with entity-embed-display and data-entity-embed-settings';
@@ -92,6 +110,12 @@ public function testFilter() {
92110
$this->drupalGet('node/' . $node->id());
93111
$this->assertText($this->node->body->value, 'Embedded node exists in page with the view mode specified by entity-embed-settings.');
94112
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
113+
$this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
114+
115+
// Ensure the embedded node doesn't contain data tags on the full page.
116+
$this->drupalGet('node/' . $this->node->id());
117+
$this->assertNoRaw('data-align="left"', 'Align data attribute not found.');
118+
$this->assertNoRaw('data-caption="test caption"', 'Caption data attribute not found.');
95119

96120
// Test that tag of container element is not replaced when it's not
97121
// <drupal-entity>.
@@ -113,6 +137,25 @@ public function testFilter() {
113137
$this->drupalget('node/' . $node->id());
114138
$this->assertNoText($this->node->body->value, 'embedded node exists in page');
115139
$this->assertRaw('<div data-entity-type="node" data-entity-id');
140+
141+
// Test that attributes are correctly added when image formatter is used.
142+
/** @var \Drupal\file\FileInterface $image */
143+
$image = $this->getTestFile('image');
144+
$image->setPermanent();
145+
$image->save();
146+
$content = '<drupal-entity data-entity-type="file" data-entity-uuid="' . $image->uuid() . '" data-entity-embed-display="image:image" data-entity-embed-settings=\'{"image_style":"","image_link":""}\' data-align="left" data-caption="test caption" alt="This is alt text" title="This is title text">This placeholder should not be rendered.</drupal-entity>';
147+
$settings = [];
148+
$settings['type'] = 'page';
149+
$settings['title'] = 'test entity image formatter';
150+
$settings['body'] = [['value' => $content, 'format' => 'custom_format']];
151+
$node = $this->drupalCreateNode($settings);
152+
$this->drupalget('node/' . $node->id());
153+
$this->assertRaw('<img src', 'IMG tag found.');
154+
$this->assertRaw('data-caption="test caption"', 'Caption found.');
155+
$this->assertRaw('data-align="left"', 'Alignment information found.');
156+
$this->assertTrue((bool) $this->xpath("//img[@alt='This is alt text']"), 'Alt text found');
157+
$this->assertTrue((bool) $this->xpath("//img[@title='This is title text']"), 'Title text found');
158+
$this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
116159
}
117160

118161
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{#
2+
/**
3+
* @file
4+
* Default theme implementation of a container used to wrap embedded entities.
5+
*
6+
* Available variables:
7+
* - attributes: HTML attributes for the containing element.
8+
* - children: The rendered child elements of the container.
9+
*
10+
* @see template_preprocess_entity_embed_container()
11+
*
12+
* @ingroup themeable
13+
*/
14+
#}
15+
<article{{ attributes }}>{{ children }}</article>

tests/modules/entity_embed_test/entity_embed_test.module

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,5 @@ function entity_embed_test_entity_embed_alter(array &$build, EntityInterface $en
7070
}
7171

7272
// Set title of the 'node' entity.
73-
$node = $build['#node'];
74-
$node->setTitle("Title set by hook_entity_embed_alter");
73+
$entity->setTitle("Title set by hook_entity_embed_alter");
7574
}

0 commit comments

Comments
 (0)