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
(Spotted while testing #2489664: Remove unnecessary markup from core templates, a.k.a. divitis )
ImageWidget::process sets #theme to 'image_widget', then calls parent::process (FileWidget::process) which clobbers #theme by setting it to 'file_widget'.
You can see this by following these steps:
- Standard install of Drupal 8
- Enable twig_debug in sites/default/services.yml, and clear caches or
drush cr
- Log in as user 1
- Navigate to /node/add/article
- Inspect the Image field. The twig debug output shows:
<!-- BEGIN OUTPUT from 'core/themes/classy/templates/content-edit/file-widget.html.twig' -->
Proposed resolution
Conditionally set #theme in FileWidget::process so this doesn't happen.
Remaining tasks
- Patch
- Tests
- Patch review
User interface changes
TBD
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#18 | Screen Shot 2015-09-22 at 17.39.47.png | 75.32 KB | gints.erglis |
#14 | 2511036-interdiff-14-11.txt | 795 bytes | martins.kajins |
#14 | 2511036-14-addedwidgettest.patch | 1.98 KB | martins.kajins |
#11 | 2511036-interdiff-11-8.txt | 949 bytes | martins.kajins |
#11 | 2511036-11-addedwidgettest.patch | 1.98 KB | martins.kajins |
Comments
Comment #1
star-szrPatch.
Comment #2
joelpittetCould use a comment but this looks like a good solution.
Comment #3
RainbowArrayCommented!
Comment #4
joelpittetThanks mdrummond. This just needs a test to confirm the right widget is being loaded when overridden.
Can be done by making a render array for file. Then swap the #theme and check the output for different content. Doesn't have to check what content changed as to keep it generic to the problem.
Just a suggestion
Comment #5
martins.kajins CreditAttribution: martins.kajins at Wunder commentedI created a test. I'm not sure about assert which I used and about target class.
Comment #6
jaxxed CreditAttribution: jaxxed at Wunder commentedI mentored the #5 patch.
I didn't like the idea of testing an override for the flat #theme, as the "image_class" is not technically a part the file module, so I suggested trying just to test the initial issue problem "does the image widget show up"
Options for testing the actual #theme override would have been:
- test a #theme handler that doesn't exist, and check for empty render results
- test a #theme handler that does exists, that is guaranteed to exist in the test context (which handler?)
Let us know if you really want a test on that file #theme change.
Comment #7
RainbowArrayExtra space
Needs newline at the end of the file.
Nice work!
My memory is that we usually do assertions that we have found something, not that we haven't found something. I actually find that confusing when reviewing test errors, but the logic might need to be reversed on the assertion. joelpittet may be able to confirm.
Comment #8
martins.kajins CreditAttribution: martins.kajins at Wunder commentedAdded extra line at the end of file
Comment #9
RainbowArray1. There are still two extra spaces on line 17 (the line right after class ImageFieldWidgetTest extends ImageFieldTestBase.
2. I think the logic of the test may still need to be examined. Usually an error message on a test says "X appears on page Y" when X does NOT appear. I think this test logic does the opposite.
Change assertNotEqual to assertEqual and message to 'Image field widget found on add/node page'
Maybe I'm wrong on the above, but I think that's what I've seen for other tests.
Comment #10
RainbowArrayComment #11
martins.kajins CreditAttribution: martins.kajins at Wunder commentedDeleted two extra spaces and changed assertNotEqual to assert to assertEqual and also it's message.
Comment #14
martins.kajins CreditAttribution: martins.kajins at Wunder commentedReplaced assertEqual with assertNotEqual and message is Image field widget found on add/node page. If result is not 0, then there is image widget.
Comment #17
LewisNymanComment #18
gints.erglis CreditAttribution: gints.erglis commentedPatch #14 can be applied and image-widget.html.twig template was used.
Comment #21
RainbowArray