Problem/Motivation

Steps to reproduce:
- embed a node
- configure it to be displayed as Teaser
- add caption and/or alignment
- check display of embedded node in some other place (/node for example)
- Expected result: no embed-specific things can be seen
- Actual result: data-align and/or data-caption can be found on tag that was previously embedded

Another sequence of steps that is most likely caused by the same issue:
- Fresh install
- Enable Entity embed
- Configure CKEditor to use Node embed button
- create node that will be embedded
- create host node
- Embed child node, set alignment to none
- Save
- Embedded node is displayed as part of the host node
- Edit host node
- Edit embed settings, set alignment to left
- Embedded node is aligned in WYSIWYG
- Save
- Expected result: Embedded node is now aligned as configured
- Actual result: Embedded node is not aligned

This happens because we add data- tags to the render array that belong to the embedded entity (which gets cached).

Proposed resolution

Add a wrapper around embedded entity and move embed-specific attributes to it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

slashrsm created an issue. See original summary.

paranojik’s picture

Status: Active » Needs review

Cross posting from github:
Problem was that the data attributes were added to the embedded entity itself. This caused the same data attributes to appear everywhere the same view mode was used/displayed on the site. That also meant that the cached version was not invalidated until the embedded entity's cache was cleared.

To prevent all mentioned problems we wrap the embedded entity and append the data attributes to the wrapper element.

Pull request: https://github.com/drupal-media/entity_embed/pull/220

slashrsm’s picture

Issue summary: View changes

Update attribute section.

Wim Leers’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#2593379: Analyze/improve/fix handling of bubbleable metadata in entity_embed

The filter that renders embedded entities must associate the necessary cacheability metadata. It sounds like that's not happening.

This is therefore basically a duplicate of #2593379: Analyze/improve/fix handling of bubbleable metadata in entity_embed. This only covers one of the many potential side-effects/consequences. The lack of test coverage for this is what's the real problem: EntityEmbedFilterTest only tests if something works at all, not if it is updated when necessary. We should at least test:

  • that if an embedded entity is modified, the host entity's rendered output is also updated
  • that if a tag on an embedded entity is added or removed, the host entity's rendered output is also updated
  • that an embedded entity that becomes unpublished, it is also no longer rendered by the host entity's rendered output, because it's no longer accessible … but only for anonymous users
slashrsm’s picture

Title: Outdated cached version of embedded entity is displayed when embed tag is updated » Embed specific attributes are cached along with the entity
Issue summary: View changes
Status: Closed (duplicate) » Needs review

I don't agree with #4. This is a different issue than #2593379: Analyze/improve/fix handling of bubbleable metadata in entity_embed. See my comment on GH: https://github.com/drupal-media/entity_embed/pull/220#issuecomment-21709...

Also updating the title as it is misleading.

Wim Leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests

Even if they're not the exact same problem (which means this issue summary doesn't do a good enough job explaining the problem), they're at least strongly related.

I stand by my statement that the real problem is the lack of test coverage for cache-related aspects of the Entity Embed filter. If we'd have that, then there would also be far less potential for confusion.

slashrsm’s picture

Completely agree on that. It is very likely that I'll have some time to look into both issues next week (incl. tests). Thank you for the feedback.

Wim Leers’s picture

Cool :) I will be able to review this patch. If you get stuck, I should be able to find time to help with the patch itself, but that may take longer. So, you rolling the patch and me unblocking you on reviews would be perfect IMO.

paranojik’s picture

I'm attaching the test scenario that should indicate what the main problem is.

The last submitted patch, 9: embed_specific-2712111-9.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 9: embed_specific-2712111-9-TEST_ONLY.patch, failed testing.

paranojik’s picture

Status: Needs work » Needs review
Issue tags: -Needs issue summary update, -Needs tests
FileSize
5.21 KB

Adding the wrapper broke EntityEmbedHooksTest tests.

Dave Reid’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests

Can we check if the caption & alignment filter in core removes the data attributes or keeps them on image elements?

Dave Reid’s picture

Status: Needs work » Needs review
Issue tags: -Needs issue summary update, -Needs tests
slashrsm’s picture

They do, but this happens after the rendered entity was cached.

Dave Reid’s picture

+++ b/src/EntityHelperTrait.php
@@ -213,11 +217,6 @@ trait EntityHelperTrait {
-    // 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'];
-    }

I'm guessing we lack test coverage for this, since I would wager this is a regression.

slashrsm’s picture

It is actually not a regression. Since we're adding additional wrapper on the embedded entity we don't need this any more. Adding test that proves this (and is beneficial in any case as we currently have no test coverage for this).

I don't like the fact that we need to add a wrapper, but it seems that this is the only way to fix this bug and still keep all the embed specific attributes.

Unrelated: It seems that EntityEmbedFilterTest could be converted to a kernel test. Follow-up?

slashrsm’s picture

Also, while working on this issue @paranojik and myself noticed that #2528314: template_preprocess_node() does not add cacheability metadata also applies to Entity embed. It is not related to this bug, but definitely something we should watch and help to fix if possible.

Dave Reid’s picture

I guess my next question, is with the wrapping element, how would #2640398: Allow <drupal-entity> elements to be inline CKEditor Widgets be possible with the wrapping div forced back on now? Eventually supporting inline displays of file entities was on the list, and I would like to make sure we're not backing ourselves into a corner again.

slashrsm’s picture

I dislike the fact that we're adding a wrapper too. Not only for the reasons @Dave Reid brought up, but also from the clean markup standpoint. Unfortunately I don't see any other solution for this problem for now.

Could we use some other html element that would allow us to work with inline elements?

Dave Reid’s picture

After reading more about cache contexts and render cacheability, I think the wrapper might be the only way.

slashrsm’s picture

I created a custom container template which should allow for overrides if non-default wrapper element is needed. Decided for to be the default. Any suggestions about that are welcome, but it seemed better than

. Also added a default class on it.
paranojik’s picture

Seems OK to me. After re-reading the W3C spec (https://www.w3.org/TR/html5/sections.html#the-article-element) looks like <article> is an appropriate choice for the wrapper element.
...but shouldn't we add some tests for the wrapper too?

paranojik’s picture

Updated tests with asserts for the new wrapper element....

Status: Needs review » Needs work

The last submitted patch, 24: 2712111-24.patch, failed testing.

paranojik’s picture

FileSize
11.46 KB

....argh. Screwed up creating the patch :) Uploading a new one. Interdiff still holds.

paranojik’s picture

Status: Needs work » Needs review
Dave Reid’s picture

<article> is already used for nodes, so now we would have two consecutive/nested article tags, which seems like it's heading in a strange direction?

paranojik’s picture

It is a bit strange, I agree. On the other hand the spec states

When article elements are nested, the inner article elements represent articles that are in principle related to the contents of the outer article.

and from this perspective it might be OK.
IMHO either <article> or <span> should be used.

slashrsm’s picture

I think that <article> should be semantically OK. I thought it would be better than <div>, which has no meaning at all.

slashrsm’s picture

Issue tags: +beta blocker

  • slashrsm committed 607254d on 8.x-1.x authored by paranojik
    Issue #2712111 by paranojik, slashrsm, Dave Reid, Wim Leers: Fix Embed...
slashrsm’s picture

Status: Needs review » Fixed
Issue tags: +D8Media

Committed. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.