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:

  1. Standard install of Drupal 8
  2. Enable twig_debug in sites/default/services.yml, and clear caches or drush cr
  3. Log in as user 1
  4. Navigate to /node/add/article
  5. 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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

star-szr’s picture

Status: Active » Needs review
Issue tags: +Needs tests
FileSize
702 bytes

Patch.

joelpittet’s picture

Status: Needs review » Needs work
+++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
@@ -371,7 +371,9 @@ public static function process($element, FormStateInterface $form_state, $form)
-    $element['#theme'] = 'file_widget';
+    if (!isset($element['#theme'])) {

Could use a comment but this looks like a good solution.

RainbowArray’s picture

Status: Needs work » Needs review
FileSize
769 bytes
644 bytes

Commented!

joelpittet’s picture

Thanks 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

martins.kajins’s picture

I created a test. I'm not sure about assert which I used and about target class.

jaxxed’s picture

I 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.

RainbowArray’s picture

  1. +++ b/core/modules/image/src/Tests/ImageFieldWidgetTest.php
    @@ -0,0 +1,35 @@
    +  ¶
    

    Extra space

  2. +++ b/core/modules/image/src/Tests/ImageFieldWidgetTest.php
    @@ -0,0 +1,35 @@
    \ No newline at end of file
    

    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.

martins.kajins’s picture

Added extra line at the end of file

RainbowArray’s picture

1. 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.

+++ b/core/modules/image/src/Tests/ImageFieldWidgetTest.php
@@ -0,0 +1,36 @@
+    $this->assertNotEqual(0, count($this->xpath('//div[contains(@class, "field--widget-image-image")]')), 'Image field widget not found on add/node page', 'Browser');

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.

RainbowArray’s picture

Status: Needs review » Needs work
martins.kajins’s picture

Status: Needs work » Needs review
FileSize
1.98 KB
949 bytes

Deleted two extra spaces and changed assertNotEqual to assert to assertEqual and also it's message.

Status: Needs review » Needs work

The last submitted patch, 11: 2511036-11-addedwidgettest.patch, failed testing.

The last submitted patch, 11: 2511036-11-addedwidgettest.patch, failed testing.

martins.kajins’s picture

Status: Needs work » Needs review
FileSize
1.98 KB
795 bytes

Replaced assertEqual with assertNotEqual and message is Image field widget found on add/node page. If result is not 0, then there is image widget.

Status: Needs review » Needs work

The last submitted patch, 14: 2511036-14-addedwidgettest.patch, failed testing.

LewisNyman’s picture

Status: Needs work » Needs review
gints.erglis’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
75.32 KB

Patch #14 can be applied and image-widget.html.twig template was used.

  • alexpott committed 08b1d33 on 8.0.x
    Issue #2511036 by martins.kajins, mdrummond, Cottser: image-widget.html....

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: 2511036-14-addedwidgettest.patch, failed testing.

RainbowArray’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

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