Using D8-beta13 I attempted to use a token in a view to rewrite the CSS output:

To reproduce:

  1. Add text field (plain) to a view.
  2. Under Style Settings in the field configuration, select 'create a css class'
  3. Insert: something-{{ field_fieldname }} heading where '{{ field_fieldname }}' is the replacement token grabbed from the 'rewrite results' section.
  4. Save
  5. Load a page with this view on it (in my case, it was a page displaying the blocks of the view.

Expected:
Line would be enclosed in css like this: <i something-fieldcontent heading>whatever</i> This does not fail in the view preview. It renders as expected there. However,

When a page with the block/view is loaded, the "website encountered an unexpected error". Console output shows:
[Thu Jul 30 16:52:36.495200 2015] [fcgid:warn] [pid 17226:tid 3015823360] [client 127.0.0.1:52474] mod_fcgid: stderr: Uncaught PHP Exception Twig_Error_Syntax: "Unexpected token "end of template" of value "" in "{# inline_template_start #}something-{{" at line 1" at /core/vendor/twig/twig/lib/Twig/ExpressionParser.php line 190, referer: http://local.site/admin/structure/views/view/services

Note:
1. The same replacement token works without error in the "Rewrite Results" section. I.e., when no css is involved.
2. CSS replacement works if it does not contain the token.

Comments

mikeker’s picture

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

This is a bug, no reason to push it to 8.1.x yet.

Ameer Khan’s picture

I am also facing same issue in drupal-8.0.5. Is there any patch available?

rootwork’s picture

Version: 8.0.x-dev » 8.1.x-dev
mikeker’s picture

Priority: Normal » Minor

The issue is that we take the string entered in the "CSS class" field and break it up by whitespaces to set those as classes on the element. If you have a whitespace in your token (eg: {{ foo }} instead of {{foo}}) then the token gets broken up resulting in a Twig syntax error.

(Note, it's so much that it works correctly in preview, but that preview doesn't add those classes.)

So the workaround is to remove whitespaces from your tokens. Because there's a feasible workaround, I'm bumping this down to minor.

To fix the issue, we should either add some help text explaining this -- if you copy/paste the token from the replacement values section you will see this issue which is terrible UX -- or change FieldPluginBase::elementClasses to respect Twig token notation when exploding the string.

therealssj’s picture

FileSize
730 bytes

How about this?

therealssj’s picture

FileSize
730 bytes

For 8.1.x,
I hope this fixes the issue

therealssj’s picture

Assigned: Unassigned » therealssj
Status: Active » Needs review
mikeker’s picture

Sorry, I ended up working on this in parallel to @therealssj, but didn't assign the issue to me so we've duplicated some effort here. Rather than a regex, I'd rather we tokenize the entire class string and then explode/clean/implode it.

Also added unit tests and handles elementLabelClasses and elementWrapperClasses.

mikeker’s picture

Assigned: therealssj » Unassigned

Status: Needs review » Needs work

The last submitted patch, 8: 2543796-tests-only.patch, failed testing.

mikeker’s picture

Status: Needs work » Needs review

One of these days I'll remember to upload the test-only patch first...

mikeker’s picture

Cottser’s picture

Priority: Minor » Normal

I don't think this fits under minor, seems like it would be easy enough to run into this.

Patch is looking good I'd say :)

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

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ibuildit’s picture

I'm still having the bug, running latest, and the patches doesn't help. :(

ibuildit’s picture

Sorry, my bad. The patch is working.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

el1_1el’s picture

Status: Needs review » Reviewed & tested by the community

#8 works for me. Thanks!

  • catch committed ffc1f75 on 8.4.x
    Issue #2543796 by mikeker, therealssj, mariagwyn: Using token views to...

  • catch committed 7621d8b on 8.3.x
    Issue #2543796 by mikeker, therealssj, mariagwyn: Using token views to...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.4.x and cherry-picked to 8.3.x. Thanks!

rootwork’s picture

Awesome, thank you!

Status: Fixed » Closed (fixed)

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