1

Drupal 9.49

PHP 8.0

Views 9.49

I am returning the title field in a view. The title being returned is "XYZ Aberdeen".

I want it to simply return "Aberdeen"

When I click on "Content: Title" and then "Rewrite results" I was hoping I could simply use the following code:

<?php print str_replace('XYZ', '', {{ title }}); ?>

Can anyone tell me how to do this please?

3 Answers 3

4

Instead of PHP you use Twig now in "Rewrite Results" of a Views field:

{{ title|replace({'XYZ': ''}) }}

Using Twig filters often results in double escaping, so that you see the HTML code of special characters on the screen. A title field shouldn't contain HTML markup, but an ampersand is quite common. Then you can avoid the double escaping by rolling back the first escape in the replace filter:

{{ title|replace({'XYZ': '', '&amp;': '&'}) }}
2
  • Brilliant answer, Is there any link to good documentation surrounding this you may be able to recommend ?
    – wotney
    Jun 7, 2023 at 11:16
  • 1
    It's in the description below the Rewrite Results input box: "The text to display for this field. You may enter data from this view as per the "Replacement patterns" below. You may include Twig ..."
    – 4uk4
    Jun 7, 2023 at 11:42
3

Adding a php code this way is discouraged as it might lead to security issues. One option is to use a hook. Here's an example code how to do this in your *.theme file for example

/**
 * Implements hook_views_pre_render().
 */
function your_module_views_pre_render(\Drupal\views\ViewExecutable $view) {
   // Specify the view ID and display ID where you want to override the field title.
   if ('your_view_id' === $view->id() && 'your_display_id' === $view->current_display) {
    // Specify the field name whose title you want to override.
    $field_name = 'your_field_name';

    // Specify the new field title.
    $new_title = 'New Field Title';

    // Loop through each result and modify the field title.
    foreach ($view->result as $key => $result) {
      $view->result[$key]->_entity->{$field_name}->getFieldDefinition()->setLabel($new_title);
    }
  }
}

If you don't need to do this for all results, but in general for the view, you can skip the loop and instead target your field directly. More information on the hook's documentation here

1

one of the way is to create a twig template for your field. And use a replace filter inside this template.

2
  • In previous versions of Drupal, in the view - under Advanced > Other there was a section called Theme: information where I could find out the name of the twig file I'd need to generate, but I can't see it on this version. It seems that this feature was removed in D8... I can use theme debugging, but it doesn't appear to give me the accuracy of the theme information functionality. This is a helpful answer though, and thanks - I will look into this and hopefully get it working !
    – wotney
    Jun 6, 2023 at 14:03
  • 1
    @wotney Here is a section of the documentation explaining how to name templates: drupal.org/docs/theming-drupal/twig-in-drupal/… . I always keep it handy. It's a pity that they removed the ability to see the name from views. The sources of templates for views: \core\modules\views\templates\
    – Anche
    Jun 7, 2023 at 6:35

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.