8 – Modify Relationship fields in hook_views_post_execute


My account object has a EntityReference field list (for custom content-type Degrees) and I created a view to display this list of the degrees. The view is a User view with a relationship to the EntityReference field. See the view below:

enter image description here

This provides me with the correct list of Degrees, however, I need to modify the results to append some text to the matchedCareer field that is accessed through the entity reference field item.

I’m trying to do this using hook_views_post_execute but am having trouble in accessing that specific field. I’m following an example from D7, but it doesn’t seem to transfer correctly to D8.

In D7, we can access it like:

function my_module_views_post_execute(&$view) {
   ...

   foreach($view->result as $key => $value) {
      $value->field_field_matchedcareer = (
        ...
      )
   }
}

However, in Drupal 8 this doesn’t appear to be the same as field_field_matchedcareer just throws me an error telling me it doesn’t exist. I’ve tried $result(0)->get('field_field_matchedcareer') and other formulas, but still the same issue occurs. I debugged and realized that the $view->result is an array of Users (which makes sense given the view), but how can I change the field values that will appear in the view from this result?

Ultimately, when I call {{ fields.matchedCareer.content }} in my template file, I need to have the customized field I expect to. Perhaps this would be better in a different location other than hook_views_post_execute, but imo it felt like the most reasonable place to alter the data after it’s been retrieved.