Score:1

How can I modify a field using PHP code in "rewrite results"?

pg flag

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?

Score:4
cn flag

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;': '&'}) }}
pg flag
Brilliant answer, Is there any link to good documentation surrounding this you may be able to recommend ?
4uk4 avatar
cn flag
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](https://twig.symfony.com/doc/2.x/) ..."
Score:3
us flag

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

Score:1
pl flag

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

pg flag
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 !
Anche avatar
pl flag
@wotney Here is a section of the documentation explaining how to name templates: https://www.drupal.org/docs/theming-drupal/twig-in-drupal/twig-template-naming-conventions . 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\
I sit in a Tesla and translated this thread with Ai:

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.