Score:0

Applying trim to paragraph value

cn flag

I have the following Paragraph template which displays a heading with a trailing orange dot. The field itself is a normal text field in Drupal. For some reason, there is a trailing space after the heading, which leads to a space between the heading and the dot. I want to remove it (and |trim is the answer), but when I add trim to the below variable I get a PHP warning saying parameter 1 should be a string, not an array.

How do I then get the absolute value of the paragraph value?

{% block paragraph %}
  <div{{ attributes.addClass(classes) }}>
    {% block content %}
      <h1>{{ content.field_heading_h1 }}<i class="orange">.</i></h1>
    {% endblock %}
  </div>
{% endblock paragraph %}

The following gives the PHP error:

{% block paragraph %}
  <div{{ attributes.addClass(classes) }}>
    {% block content %}
      <h1>{{ content.field_heading_h1|trim }}<i class="orange">.</i></h1>
    {% endblock %}
  </div>
{% endblock paragraph %}

Warning: trim() expects parameter 1 to be string, array given in twig_trim_filter()

Score:0
gr flag

As the warning states, content.field_heading_h1 is a (render)-array

you could check it´s structure using {{ kint(content.field_heading_h1) }} (devel-module)

probably you can get the plain text value and trim it with:

{{ content.field_heading_h1.value|trim }}

or

{{ content.field_heading_h1.0|trim }}

refering to How do I get the raw field value in a twig template?

thats just one way to do "operation on symptoms" but the actual question to ask is why there is a whitespace and is there a way to prevent that already in the form that saves it

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.