3

I am using the views data export module with Drupal 8. I have a view that shows a list of users and the desired columns to be exported to the csv are:

  • Name
  • Trained
  • Status

Unfortunately, both of the Trained and Status columns are calculated through a semi-complex method from my module. Using the calculated field module isn't really an option.

Ideally my csv should look something like the below. The "Trained" and "Status" columns would be calculated by running the user_id through their respected methods:

Name | Trained | Status
user_name_1 | is_trained(user_id_1) | get_status(user_id_1)
user_name_2 | is_trained(user_id_2) | get_status(user_id_2)
...

I have tried what I feel is every combination of views pre_render, post_render, post_execute, etc...; however, I just can't seem to find a way to add a calculated value to the row. It is possible I am approaching this wrong but any suggestions on how to accomplish this would be appreciated.

3
  • 1
    If you're already working with a custom module for this logic, why don't you write a Views field plugin to render the calculated value rather than trying to hook into the view and alter it?
    – Shawn Conn
    Commented Nov 6, 2020 at 7:27
  • Yeah, what @ShawnConn says. Provide your own custom Views field. If it's just for display and you don't need to sort or filter on it than that's the way to go.
    – leymannx
    Commented Nov 6, 2020 at 11:50
  • Wow, this was perfect for what I needed. Thank you! Commented Nov 12, 2020 at 17:20

1 Answer 1

1

I am going to go ahead and answer my own post to help anyone else out.

A simple way to do this for me was to write a views plugin as suggested in the comments.

Here is a great link to walk you through this: https://www.webomelette.com/creating-custom-views-field-drupal-8

I do not feel I can write any explanation better than what is done in this article so I would jump over there for more information.

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