0

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()

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.