I'm trying to figure out how I can replace underscores and a specific text using Replace on a Drupal 8 view custom text. In a template, I could use the following code to replace specific text.

<span class="tag">{{ items.entity.getType|replace({'_': " ", "2": ""}) }}</span>

what can I use on a view on custom text? I tried using {{ type|replace({'_': " ", "2": ""}) }} but it seems to show the text as plain text, not HTML markup.


  • 1
    Quick and dirty fix for this type of output is pipe it to raw. So something like: {{ type|replace({'_': " ", "2": ""})|raw }} – Beebee Jul 13 '18 at 9:09
  • 1
    The better fix is to do this in a preprocessor, and add it to an '#markup' render array – Beebee Jul 13 '18 at 9:11
  • 3
    Possible duplicate of Simple string replacement in views output? – mbomb007 Aug 1 '19 at 13:22

have you try to use Rewrite results in views field settings? In Rewrite results there is an option Override the output of this field with custom text if you click on it it will let you change the output of this field to whatever you want. plus there will be a Replacement patterns option which will show you a list of replacement tokens. also you can add some HTML if you want/need to.

| improve this answer | |
  • This doesn't allow you to replace a character with another. – mbomb007 Aug 1 '19 at 13:21

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.