Problem/Motivation

Tokens are supported in field descriptions and it works correctly for more fields. For file/image widgets, the tokens are not replaced. I am marking it as major because tokens could be used to communicate important information and there is no workaround. Please change the priority if relevant.

To reproduce:

  • Go to Structure -> Content Types -> Article -> Manage Fields -> Image on a plain Drupal 8 site (such as simplytest.me).
  • Type a token such as '[site:name]' in the Help text box.
  • Go to Create Content -> Article, the token in description is not replaced.
  • Repeat the steps for another field such as body. The token is replaced.
  • The token is not replaced on both file and image widgets regardless of cardinality.

Proposed resolution

Fix the bug.

Remaining tasks

Patch, review, and apply

User interface changes

None, except the tokens will work now.

API changes

None

Data model changes

None

Comments

hussainweb created an issue. See original summary.

hussainweb’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.55 KB

The patch fixes the issue.

hussainweb’s picture

hussainweb’s picture

Just a very rough test. This should fail. I will then write a proper test for both file and image widgets.

hussainweb’s picture

StatusFileSize
new905 bytes

This should do it (hopefully).

swentel’s picture

Component: token system » field system
StatusFileSize
new2.14 KB

This will fail - moving to field system as well as it's not really a token problem, but rather a widget(base) problem.

swentel’s picture

StatusFileSize
new3.95 KB

And with fix - we'll need additional tests for the file field widget too to make sure that it works there (since I changed code in FileWidget too).

The last submitted patch, 6: 2642674-6.patch, failed testing.

berdir’s picture

+++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
@@ -119,7 +119,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f
 
     $title = $this->fieldDefinition->getLabel();
-    $description = FieldFilteredMarkup::create($this->fieldDefinition->getDescription());
+    $description = FieldFilteredMarkup::create(\Drupal::token()->replace($this->fieldDefinition->getDescription()));
 
     $elements = array();

What about a helper method on WidgetBase for this? getFilteredDescription() or something like that?

hussainweb’s picture

That sounds useful, however, I don't think we can do that for 8.0.x. If we agree on the method, we could create a follow-up for 8.1.x.

swentel’s picture

WidgetBase is abstract, so I think that's totally fine todo for 8.0.x even.

swentel’s picture

StatusFileSize
new4.53 KB
new3.1 KB

reroll + new method

berdir’s picture

+++ b/core/lib/Drupal/Core/Field/WidgetBase.php
@@ -567,4 +567,13 @@ protected function isDefaultValueWidget(FormStateInterface $form_state) {
+   * @return \Drupal\Core\Field\FieldFilteredMarkup
+   */

I think we still require a description for @return, even if it is as simple as this?

swentel’s picture

StatusFileSize
new4.59 KB
new652 bytes

Yeah, lazy me :)

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Great. Lets get this fixed.

The last submitted patch, 14: 2642674-14.patch, failed testing.

catch’s picture

Version: 8.0.x-dev » 8.1.x-dev

Adding the protected method to the base class is something we can't do in a patch release.

Not 100% sure for a minor release either - for me I'd make the change but be prepared to roll back before tagging a stable release if it turns out someone added a method to a subclass with the same name. Pinged alexpott for a second opinion.

Once this is in 8.1.x it's backportable to 8.0.x without the protected method, or possibly with an underscore prefix and @internal note.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Talked to @xjm to confirm we're on the same page.

While we treat base classes as @api, adding a method to a base class is not an API change. It could theoretically break someone's code somewhere, but that's what minors are for.

If we do break someone's code before a release candidate, they can file a bug report and we can talk about it. Otherwise it's just an improvement for everyone else anyway.

However, it's an API addition to the base class, so we should add a change notice.

catch’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
StatusFileSize
new4.58 KB

Here it is:
https://www.drupal.org/node/2668850

Patch didn't apply to 8.1.x due to entity_create() conversion, so re-rolled.

mohit_aghera’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new34.81 KB
new23.96 KB
new21.94 KB

Tested #19 on local.
Screenshots are attached. Seems working fine for me.

heykarthikwithu’s picture

Tested #19
+1 RTBC

  • catch committed 6872eec on 8.1.x
    Issue #2642674 by swentel, hussainweb: Tokens not replaced in file/...
catch’s picture

Status: Reviewed & tested by the community » Fixed

I only did a straight re-roll so I think I'm OK to commit this to 8.1.x

For 8.0.x we either need to skip the change to the base class and inline that code, or make it _underscorePrefixed() and @internal with a note that it'll change in 8.1.x.

I'm marking this fixed against 8.1.x, if you want to work on a backport please re-open and move it to 8.0.x.

naveenvalecha’s picture

Thanks!
Published the CR https://www.drupal.org/node/2668850

berdir’s picture

Version: 8.1.x-dev » 8.0.x-dev
Status: Fixed » Needs review
StatusFileSize
new3.95 KB

Lets just go back to the patch from #7 then for the backport. No need to introduce this method here if it causes so many complications. Wasn't aware that we're so strict on that in 8.0.x.

Re-uploading that patch, didn't make any changes.

Status: Needs review » Needs work

The last submitted patch, 25: 2642674-7.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new3.99 KB
new2.26 KB

Didn't apply anymore, rerolled.

swentel’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

The original patch seems to be missing test coverage.

+++ b/core/modules/image/src/Tests/ImageFieldWidgetTest.php
@@ -27,9 +27,10 @@ public function testWidgetElement() {
+    $this->assertNoText('Image test on [site:name]');

At the very least we should be asserting for the expected text as well... $this->assertNoText() is very vulnerable to false positives.

xjm’s picture

Priority: Major » Normal
Status: Needs work » Fixed
Issue tags: +Needs followup

Let's go back to having this fixed against 8.1.x, since 8.0.x is about to be EOL. Let's open a followup for fixing the test coverage.

Also retroactively downgrading; the committers agreed this is not a major issue. Thanks!

xjm’s picture

Version: 8.0.x-dev » 8.1.x-dev

Status: Fixed » Closed (fixed)

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