custom-fieldsdrupal-8plaintextdrupal-blocks

Display raw text from custom field in Drupal


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?


Solution

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