0

Is there a way to change the output programatically on a specific field in d8 views?

I have a field called 'IMCID' which I want to output as a link, but the link is going to change based on the value of another field for that row.

For example, if IMCID of row #1 is 15, and Format of row #1 is 'Copy', then I want the output URL to be something like example.com/copy/15, however if IMCID of row #1 is 15, and Format is 'Hard Copy', then the output URL would be example.com/hardcopy/15

If possible, I would like to avoid making multiple template files for individual fields, as this functionality will be carried over onto other fields later on, and would rather not have to manage a ton of template files.

I have tried using hook_pre_render() and hook_post_render(), but the markup doesn't appear to be in this hook so I am not sure how to update the link URL.

2 Answers 2

0

Views is mainly a UI tool, also for theming. Check this box in the field:

REWRITE RESULTS

[x] Override the output of this field with custom text

This opens a text box which accepts Twig code and field tokens of other fields which are placed above. Exclude those fields from display if you want to us them only in Twig. You can check the available tokens in REPLACEMENTS PATTERN below the text box. Look in REPLACEMENT PATTERNS for raw field values, don't use formatted output in Twig logic.

2
  • This doesn't answer the question. Views provides hooks for a reason. Overriding the output would apply the same rewrite to ALL ROWS, which is not what I am trying to do. I am also aware that logic doesn't belong in Twig templates, which is why I am asking what hook I need to access to do the rewriting of the output before it gets to the template.
    – Ex0r
    Mar 12, 2021 at 14:11
  • 1
    Logic does belong in Twig, this is a programming language. Views has decided to implement Twig to output fields differently depending on other field values. AFAIK this is the recommend approach. If you want to use PHP I would suggest to use Views handler plugins rather than hooks.
    – 4uk4
    Mar 12, 2021 at 14:17
0

To anyone experiencing a similar situation, you can have a look at: https://drupal.stackexchange.com/a/264191/106121 For Global Text Fields in Views (nothing) using HOOK_views_pre_render(\Drupal\views\ViewExecutable $view) {}

Or,

use THEME_preprocess_views_view_field(&$vars) {} which can affect a particular view raw in the View Results https://api.drupal.org/api/drupal/core%21modules%21views%21views.theme.inc/function/template_preprocess_views_view_field/8.2.x

Update:

You can also utilize the Views Conditional Module [ https://www.drupal.org/project/views_conditional ] to add display conditions to different view fields individually while discarding the use of Global Text. Views Conditional is a good alternative.

This article can give you an outline of its implementation on an existing or new view: https://www.webwash.net/conditionally-display-view-fields-using-views-conditional-in-drupal/

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.