I have created a field Text(plain, long) and have a custom template which currently only outputs this field.

{{ content.field_testing }}

When the output is rendered, all the line breaks are converted to <br> I need to remove. I could add some filters but I figure there must be a way to prevent it in the first place. How can I do this?


Since you are using a custom template, the quicker way is just to use the replace() filter.

{{ content.field_testing|replace({'<br>': '', '<br />': ''}) }}

The field formatter used in the case of a plain text field doesn't have settings, so you cannot decide if the formatter should output the <br /> tags you want to remove. It would probably make sense in few cases, especially if you need to replace those <br /> with other text, but somebody could argue that if you don't need them, you could just avoid entering new lines in the field.

The alternative would be implementing a different field formatter, but this is not as quicker as using a filter. If you just need this for this exact purpose, it sounds a bit excessive.

  • Thank you i have gone with this as it seems the simplest solution right now to just replace. Jun 22 '16 at 10:24
  • 1
    Note that inside a field template, item.content is an array and not a string, so I had to do the slightly more messy option of: {{ item.content|render|replace({'<br>': '', '<br />': ''})|raw }}. Probably better to just create a true "plain text" formatter (br's are really annoying if using <pre> or <code>...), but I was lazy and this was easier to without an extra module. Dec 17 '18 at 18:46

The default FieldFormatter for Text(plain, long) is BasicStringFormatter, and it does this:

$elements[$delta] = [
    '#type' => 'inline_template',
    '#template' => '{{ value|nl2br }}',
    '#context' => ['value' => $item->value],

You could overwrite it without the nl2br.


Finally i created a new formatter

  namespace Drupal\inception\Plugin\Field\FieldFormatter;

  use Drupal\Core\Field\FormatterBase;
  use Drupal\Core\Field\FieldItemListInterface;

   * Plugin implementation of the 'basic_string_no_br' formatter.
   * @FieldFormatter(
   *   id = "basic_string_no_br",
   *   label = @Translation("Plain text No BR"),
   *   field_types = {
   *     "string_long",
   *   },
   * )
  class BasicStringNoBrFormatter extends FormatterBase {

   * {@inheritdoc}
  public function viewElements(FieldItemListInterface $items,   $langcode) {
      $elements = [];

      foreach ($items as $delta => $item) {
          // The text value has no text format assigned to it, so the user input
          // should equal the output, including newlines.
          $elements[$delta] = [
              '#type' => 'inline_template',
              '#template' => '{{ value }}',//Plain value without nl2br
              '#context' => ['value' => $item->value],

      return $elements;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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