0

In a Drupal 8.3 View, I am displaying fields. One of these fields is configured to "rewrite results" so as to "output this field as a custom link" with a custom link path.

I would like to modify these links in code (based on some later logic). However, I cannot find a place to actually modify this value in code. If someone knows how to accomplish this, please let me know. (This was much easier in D7.)

Here's what I've tried thus far.

  • theme_preprocess_views_view(): I found ...['#view'] -> field['name'] -> options['alter']['path'] in Drupal 8's endless arrays, but changing this value did not affect the output. I could not find my custom link anywhere else in this endless array.
  • hook_views_post_render(): This supposedly allows one to alter the output string, but doesn't actually give the output as a string (broken documentation).
  • I can see the output string in a twig template, but cannot easily substitute my link given that PHP doesn't run in twig. This would be a less desirable option in any case.

2 Answers 2

2

After more time and effort poking around, I found one possible solution, but perhaps not the best or most efficient.

What didn't work:

My original approach of changing the field path links in theme_preprocess_views_view() didn't work because this function is too late in the page build. ("Preprocess" is apparently contextual to the target function. In this case it comes before the final View, not the View's fields.)

I also tried using template_preprocess_views_view(), which was called before other View preprocess functions. While I was able to change the View rewrite values (e.g., $view -> field['name'] -> options['alter']['path'] = 'my new path'), this changed the links for all the fields, not allowing each field to be individually customized.

I was able to change the Views rewrite path value inside hook_preprocess_views_view_field(&$variables), but this affected the following field in the results array, not the current field.

My Solution:

I ended up using hook_preprocess_views_view_field(&$variables), and rendering my own custom links and saving them to $variables['output'].

$variables['view'] and $variables['row'] were available for finding data for the logic, as well as additional objects depending on what the View is displaying.

I also used template_preprocess_views_view() to re-organize all the Views rows before they were rendered in the twig template, but as mentioned before, I couldn't change the content of the rows inside this function because they were already rendered before this function was called.

I hope this saves time for someone. Feel free to post more eloquent solutions.

1

I'm late to the game but had similar questions. The drawback of using hook_preprocess_views_view_field(&$variables) is it operates on a field for every row. In order to have some logic based on row values you have to loop through all rows, like
foreach ($variables['row'] as $idx) { // your logic here }

Since in my logic I had to check for a node NOT having paragraphs and entity->hasField('fieldname') was unobtainable in hook_preprocess_views_view_field I reverted to hook_views_post_execute($view) and did all of the rewrite logic there.

Eric

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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