Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In #2994696-198: Render embedded media items in CKEditor, @effulgentsia requested:
This warrants a theme hook, so that themes can override the markup and classes.
"This" refers to the error message that the media_embed filter will output for media items that cannot be loaded for rendering during text processing. Right now, the message and its container is not themeable.
Proposed resolution
Make the error message themeable, possibly by adding a new theme hook.
Remaining tasks
Determine the best way to do this, and make it happen.
User interface changes
None.
API changes
Potentially a new theme hook; TBD.
Data model changes
None anticipated.
Release notes snippet
TBD.
Comment | File | Size | Author |
---|---|---|---|
#49 | interdiff.txt | 1020 bytes | lauriii |
#44 | 3071713-44.patch | 22.61 KB | oknate |
#44 | 3071713-interdiff--41-44.txt | 1022 bytes | oknate |
#41 | 3071713-41.patch | 21.61 KB | oknate |
#41 | 3071713--interdiff-37-41.txt | 3.32 KB | oknate |
Comments
Comment #2
oknateHere's an interdiff I'm extracting for reference. It's built off of #2994696: Render embedded media items in CKEditor #194.
Comment #3
phenaproximaBlocker is in, so this is no longer postponed.
Comment #4
bnjmnmNot interdiffing as #2 was there for reference and not intended to work. This builds off of that and adds tests which expose what might be an issue: When performing the test steps manually, I can trigger the preprocess hook that confirms the message is themeable. In the FunctionalJavascript test it does not work, as I discovered it is generating the markup from the
Drupal.theme.mediaEmbedError
inplugin.es6.js
.I'm hoping it's just been a long week and there's an obvious way to resolve this, but it seems like making the embedded media error messages themeable could cause consistency problems as they're not always generated by the theme layout.
Comment #6
oknateThanks for the patch!
The consistency problem you're describing is actually the expected behavior. There are two types of error messages related to media embeds:
1) One from the backend, that comes from the preview route (which should be themable in a twig template). This one I like to call (borrowed from Wim Leers) the "missing media indicator".
2) One from the javascript:
This one is out of scope for this issue, as it already has a javascript theme function. The test updated in #4, ::testPreviewFailure() is the second one, so that's the wrong error message. It shouldn't get the template modifications.
A second thing, the changed the attached library shouldn't change based on #2, but keep the current library. I'm switching it back. The correct library in HEAD is media/media_embed.
Comment #7
oknateAdding functional js testing for the missing media indicator, as we only have a kernel test right now. I think there's value in putting them both in the same test function as well as some comments explaining the differences between the two types of errors. This will add a bit more test coverage and also provide some documentation if someone greps for renderMissingMediaIndicator() in the code base.
Comment #8
AaronMcHaleWould solving #2859550: "status_messages"-like render element with a preset message help with this issue as it sounds like the two might be related?
A related issue over there #3078400: Remove hard coded messages markup might also be relevant here.
Comment #9
Wim LeersLooking good! I found only nits.
🤓The string
string
here should be removed.Nits:
- s/backend/back end/
- s/uuid/UUID/
🤔 What's the point of this Twig template if it does exactly the same anyway?
Same here as in point 1.
Comment #10
oknateAddressing #9.
Fixed nits and dropped redundant templates.
Comment #12
oknateAdding media-embed-error to the list of templates to skip for stable. This allows us to not put the redundant template in stable. This hasn't been used but once before. So, are we supposed to copy redundant templates to stable? I'm adding a second patch here that has the template in stable, since I suspect we're breaking a precedent by not copying it to stable.
Comment #13
oknateSame as the last one (with stable template), but with the updates suggested in #9.1 and #9.4.
Comment #16
Wim LeersPer #3072319-16: Determine the scope of default styling for embedded media (what belongs in Classy, Stable, etc.?):
Moving issue credit over.
Comment #17
Wim LeersThis implements @lauriii's suggestions for the server-side (
@Filter=media_embed
)-triggered media embed errors.The client-side (
@CKEditorPlugin=drupal_media
)-triggered media embed errors still needs to be handled.Comment #18
Wim LeersAnd this does the client-side portion.
Which also revealed a bug: in HEAD, it's impossible for themes to override the
Drupal.theme.*
functions defined incore/modules/media/js/plugins/drupalmedia/plugin.es6.js
!(This is untested, I need to move on to other issues. But I trust @oknate can push it across the finish line 😀!)
Comment #21
Wim LeersFixes the test failure in #17.
Comment #22
Wim LeersAnd this should fix most of the test failures in #18 (untested).
Comment #25
oknateFixing tests.
Comment #26
phenaproximaI'm surprised we repeat this code in both media_test_filter and media_test_ckeditor. Why's that?
Why this change?
How suspenseful! :) Can the comment elaborate a bit on what kind of error message it is and why it would appear?
This is redundant. The fact that we've waited for an element with this class is enough. Can we move the comment to just above the assertNotEmpty() call?
Nit: This can be done with
$this->config('system.theme')->set('default', 'classy')->save()
. No need to get the config factory.We don't need the assertTrue(); we can just add the media-embed-error--missing-source class to the selector we're waiting for.
I assume this changed because the CSS classes are now applied by the backend in a different order.
If I'm correct, then this is why I'm not a huge fan of checking for CSS classes using assertSame(); it becomes brittle and subject to arbitrary changes in ordering, which does not affect the validity of the
class
attribute in any way, yet can cause the test to fail. However, I realize that fixing this is a bit out of scope since this is a pre-existing condition. So you can ignore this feedback, and I'll just pout about it to myself.But...if we wanted to fix this, I'd suggest changing the selector we pass to $this->cssSelect(), since we're already changing that anyway in this patch. If we do that, we are asserting that the correct classes are present, but we don't have to worry about random order changes breaking our tests.
Is this in the correct place alphabetically, relative to other things in libraries-extend?
Wouldn't we be using libraries-override for this?
Comment #27
Wim Leers#26.9: Maybe, good question! 👍
Let me explain why I did it this way. I grepped for
Drupal.theme.
in JS files incore/themes/
and found these instable.info.yml
:Hence the use of
libraries-extend
instead oflibraries-override
.Comment #28
oknateResponding to #26:
1. One is for the filter kernel test and one is for the functional javascript CKEditor test. I guess we don’t need it in both places or the test modules could be combined. But I think that’s out of scope. For now, I think it’s appropriate to have the hook in both places, as I think it's valuable to test both in the filter kernel test and in the CKEditor integration FunctionalJavascript test. I think a follow-up issue to see if those two test modules could be combined is the right thing to do.
2. ✅Undoing that change. I misread it, and missed the assertEmpty, so I thought, the wait was unnecessary. Since we’re expecting it to be empty, having a shorter wait time was right. Undoing the change.
3. ✅ Ruined the suspense with a lengthy explanation.
4. ✅ Removed redundant assertion.
5. ✅ Replaced how we’re setting classy to not use config factory.
6. ✅ Removed the assertion, and added the class to the previous assertion.
7. ✅ Updated to use cssSelect()
8. ✅ I alphabetized them. Seems out of scope? Never mind, undoing this change. It looks very out of scope. They're not alphabetized.
9. We actually want to load both, as we’re only overriding the theme function for the missing media indicator, while leaving the edit button alone. if we used libraries-override without adding the edit button, we’d lose the edit button, and if we add the edit button, I guess we’re doing it unnecessarily.
Comment #29
Wim LeersÜbernit: extraneous newline being added here.
Comment #30
phenaproximaResponding to #28.1:
Agreed. Tagging this issue for a follow-up.
Comment #31
phenaproximaNit: This should be
waitForText
, notwaitforText
. This is fixable on commit.👍 This makes sense, since the only thing in that style sheet is...styling for the errors.
Apart from that one nit, I don't see anything to complain about here. +1 RTBC once green on all backends and the follow-up mentioned in #28.1 is filed.
Comment #32
phenaproximaFiled #3085264: Merge media_test_filter and media_test_ckeditor and restoring RTBC.
Comment #33
lauriiiLet's add a
@see
comment to reference the template here.Let's add
@see
comment here to reference the theme function.💯
🕵️♂️ Ubernit: We can remove this line changes because otherwise, they end up in the actual rendered output.
Comment #34
oknateFixing nits in #29 and #31
Comment #35
oknateDoing my best to address #33
Comment #36
lauriiiWe can probably just reference the template directly. As in
@see media-embed-error.html.twig
.👍
Comment #37
oknateTrying again to address #33.
Comment #38
phenaproximaFrom the interdiff, it looks like @lauriii's feedback is fixed. RTBC once green on all backends.
Comment #39
lauriiiWe should load this CSS in the CKEditor as well by using the
ckeditor_stylesheets
property in the classy.info.yml file.Comment #40
Wim Leers😵🤦♀️ Of course!
Comment #41
oknate1) Adding ckeditor_stylesheets to classy.info. I confirmed without this, it's not styled in the CKEditor and with it, it's styled. Added an assertion to CKEditorIntegrationTest::testErrorMessages().
2) I was getting an error due to CKEditor trying to load a css file that no longer exists in DrupalMedia::getCssFiles(), so removed that.
3) I checked and found the library deleted from media, 'media_embed' was still in stable, so removed that and it's css file.
Comment #42
Wim LeersComment #44
oknateFixing the test failure, due to new ckeditor_stylesheets addition.
Comment #45
phenaproximaLooks like the previous failure was due to an outdated assertion. Restoring RTBC on the assumption that all backends will be green.
Comment #46
lauriiiThe patch looks good. Could we open a change record to notify theme maintainers not using Stable of the new template and theme function?
Comment #47
phenaproximaChange record written: https://www.drupal.org/node/3085857
Comment #49
lauriiiRemoved this on commit because the param doesn't exist. I also used
yarn prettier
to fix eslint failures.