Commit 607254d2 authored by paranojik's avatar paranojik Committed by slashrsm

Issue #2712111 by paranojik, slashrsm, Dave Reid, Wim Leers: Fix Embed...

Issue #2712111 by paranojik, slashrsm, Dave Reid, Wim Leers: Fix Embed specific attributes are cached along with the entity.
parent 63ce40cc
......@@ -34,6 +34,14 @@ Entity Embed can be installed via the
*Warning: If you were using the module in very early pre-alpha
stages you might need to add `data-entity-id` to the list of allowed
attributes.*
* If you're using both the 'Align images' and 'Caption images' filters make
sure the 'Align images' filter is run before the 'Caption images' filter in
the **Filter processing order** section. (Explanation: Due to the
implementation details of the two filters it is important to execute them in
the right sequence in order to obtain a sensible final markup. In practice
this means that the alignment filter has to be run before the caption
filter, otherwise the alignment class will appear inside the <figure> tag
(instead of appearing on it) the caption filter produces.)
## Usage
......
......@@ -6,6 +6,33 @@
* format.
*/
/**
* Implements hook_theme().
*/
function entity_embed_theme() {
return [
'entity_embed_container' => [
'render element' => 'element',
],
];
}
/**
* Prepares variables for entity embed container templates.
*
* Default template: entity-embed-container.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #attributes, #children.
*/
function template_preprocess_entity_embed_container(&$variables) {
$variables['element'] += ['#attributes' => []];
$variables['attributes'] = $variables['element']['#attributes'];
$variables['children'] = $variables['element']['#children'];
}
/**
* Implements hook_entity_embed_display_plugins_alter() on behalf of file.module.
*/
......
......@@ -193,7 +193,13 @@ trait EntityHelperTrait {
// Build and render the Entity Embed Display plugin, allowing modules to
// alter the result before rendering.
$build = $this->renderEntityEmbedDisplayPlugin(
$build = array(
'#theme_wrappers' => ['entity_embed_container'],
'#attributes' => ['class' => ['embedded-entity']],
'#entity' => $entity,
'#context' => $context,
);
$build['entity'] = $this->renderEntityEmbedDisplayPlugin(
$entity,
$context['data-entity-embed-display'],
$context['data-entity-embed-settings'],
......@@ -213,11 +219,6 @@ trait EntityHelperTrait {
$build['#attributes']['data-caption'] = $context['data-caption'];
}
// If this is an image, the image_formatter template expects #item_attributes.
if (!empty($build['#theme']) && !empty($build['#attributes']) && $build['#theme'] == 'image_formatter') {
$build['#item_attributes'] = $build['#attributes'];
}
// @todo Should this hook get invoked if $build is an empty array?
$this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed", 'entity_embed'), $build, $entity, $context);
$entity_output = $this->renderer()->render($build);
......
......@@ -14,6 +14,20 @@ namespace Drupal\entity_embed\Tests;
*/
class EntityEmbedFilterTest extends EntityEmbedTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'file',
'image',
'entity_embed',
'entity_embed_test',
'node',
'ckeditor',
];
/**
* Tests the entity_embed filter.
*
......@@ -32,6 +46,7 @@ class EntityEmbedFilterTest extends EntityEmbedTestBase {
$this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
$this->assertText($this->node->body->value, 'Embedded node exists in page');
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
$this->assertRaw('<article class="embedded-entity">', 'Embed container found.');
// Tests entity embed using entity UUID and view mode.
$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 @@ class EntityEmbedFilterTest extends EntityEmbedTestBase {
$this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
$this->assertText($this->node->body->value, 'Embedded node exists in page.');
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
$this->assertRaw('<article class="embedded-entity">', 'Embed container found.');
// Ensure that placeholder is not replaced when embed is unsuccessful.
$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 @@ class EntityEmbedFilterTest extends EntityEmbedTestBase {
$this->assertText($this->node->body->value, 'Entity specifed with UUID exists in the page.');
$this->assertNoText($sample_node->body->value, 'Entity specifed with ID does not exists in the page.');
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
$this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
// Test deprecated 'default' Entity Embed Display plugin.
$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 @@ class EntityEmbedFilterTest extends EntityEmbedTestBase {
$this->drupalGet('node/' . $node->id());
$this->assertText($this->node->body->value, 'Embedded node exists in page.');
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
$this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
// Ensure that Entity Embed Display plugin is preferred over view mode when
// both attributes are present.
$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>';
$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>';
$settings = array();
$settings['type'] = 'page';
$settings['title'] = 'Test entity embed with entity-embed-display and data-entity-embed-settings';
......@@ -92,6 +110,12 @@ class EntityEmbedFilterTest extends EntityEmbedTestBase {
$this->drupalGet('node/' . $node->id());
$this->assertText($this->node->body->value, 'Embedded node exists in page with the view mode specified by entity-embed-settings.');
$this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
$this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
// Ensure the embedded node doesn't contain data tags on the full page.
$this->drupalGet('node/' . $this->node->id());
$this->assertNoRaw('data-align="left"', 'Align data attribute not found.');
$this->assertNoRaw('data-caption="test caption"', 'Caption data attribute not found.');
// Test that tag of container element is not replaced when it's not
// <drupal-entity>.
......@@ -113,6 +137,25 @@ class EntityEmbedFilterTest extends EntityEmbedTestBase {
$this->drupalget('node/' . $node->id());
$this->assertNoText($this->node->body->value, 'embedded node exists in page');
$this->assertRaw('<div data-entity-type="node" data-entity-id');
// Test that attributes are correctly added when image formatter is used.
/** @var \Drupal\file\FileInterface $image */
$image = $this->getTestFile('image');
$image->setPermanent();
$image->save();
$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>';
$settings = [];
$settings['type'] = 'page';
$settings['title'] = 'test entity image formatter';
$settings['body'] = [['value' => $content, 'format' => 'custom_format']];
$node = $this->drupalCreateNode($settings);
$this->drupalget('node/' . $node->id());
$this->assertRaw('<img src', 'IMG tag found.');
$this->assertRaw('data-caption="test caption"', 'Caption found.');
$this->assertRaw('data-align="left"', 'Alignment information found.');
$this->assertTrue((bool) $this->xpath("//img[@alt='This is alt text']"), 'Alt text found');
$this->assertTrue((bool) $this->xpath("//img[@title='This is title text']"), 'Title text found');
$this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
}
}
{#
/**
* @file
* Default theme implementation of a container used to wrap embedded entities.
*
* Available variables:
* - attributes: HTML attributes for the containing element.
* - children: The rendered child elements of the container.
*
* @see template_preprocess_entity_embed_container()
*
* @ingroup themeable
*/
#}
<article{{ attributes }}>{{ children }}</article>
......@@ -70,6 +70,5 @@ function entity_embed_test_entity_embed_alter(array &$build, EntityInterface $en
}
// Set title of the 'node' entity.
$node = $build['#node'];
$node->setTitle("Title set by hook_entity_embed_alter");
$entity->setTitle("Title set by hook_entity_embed_alter");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment