1

I'm trying to render a Block's Field as Plain Text as I need it used as part of HTML, I've tried using |RAW however I read it was unstable + it didn't work haha!

This is my existing HTML minified

<a href="#" style="background-color:#FFFFFF;">Read More </a>

However I would like to make it more useable

<a href="#" style="background-color:#{{ content.field_color }};">Read More </a>

This would mean that when a user modifies the DrupalBlock HEX code it would change the color of the box. However the issues is when it's printed on the page it's looking like this

<div data-quickedit-field-id="#" class="field field--name-field-color field--type-string field--label-hidden field--item quickedit-field">FFFFFF</div>

the only thing I would like printed is "FFFFFF" with no div's

-

Here is my question: How do I display my Field_color as plain text when it prints?

2 Answers2

1

You can use |raw : {{ content.field_color|raw }}.

If you need more information please ask.

  • RAW is not secure and was removed in DRUPAL8 which this question is about, I also explained "I've tried using |RAW however I read it was unstable + it didn't work haha!" - .getString() seemed to work, thanks though. –  Sep 06 '16 at 22:29
  • NOT correct! Check: https://api.drupal.org/api/drupal/core!modules!system!tests!modules!twig_theme_test!templates!twig-raw-test.html.twig/8.2.x http://twig.sensiolabs.org/doc/filters/raw.html The raw filter marks the value as being "safe", which means that in an environment with automatic escaping enabled this variable will not be escaped if raw is the last filter applied to it. – Arnold PÉTER Sep 13 '16 at 11:20
  • I wasn't trying to start a discussion or asked to be corrected, I would ask that you refer to: https://www.drupal.org/node/2603074 –  Sep 15 '16 at 18:34
0

I suggest you do a dump or kint of your content.field_color variable. You might be able to get some more information on it and get the answer!

Anyway, we have something similar in our project and the way we do it is by using a .getString() method.

{% set image_align = content.field_image_align['#items'][0].getString() %}

<div class="{{ image_align }}">

Our field is a list of values so you'll have to look for another array item to call the .getString() method on.

VJamie
  • 616
  • 3
  • 14
  • Thanks so much haha, the issues I went through trying to pre-process and using hooks caused me issues haha! –  Sep 06 '16 at 00:23