Altering fields before rendering: hook or template? I have made some recent posts about Drupal 8 and its paradigm for altering fields. Having spent many hours reading up on APIs and some trial and error, I have come to the conclusion that I am really not sure how to proceed with certain basic tasks, such as altering a field.

So the point of this post is to see how one should go about a task such as altering a field.

First, let me describe exactly what I am trying to do. I would like to edit a field before it is rendered. It is an image field. So all I need to do is alter the target_id of the field that points to the image. I want this to happen in every view, display, etc.

Some thoughts:


Overriding via templates: I don't think this makes sense. Seems a template gets a render array, and that one would want to alter the array before the theme does its thing. Also, I want to be able to switch out themes, so this approach is non-ideal
Rewriting field in Views: since I want the fields to be altered regardless of whether it is Views, Display Suite, etc, this would not make sense
Module hooks: I think this is what I need to do, but the info online on Drupal 8 is fairly sparse. For instance, I found out by trial and error that mymodule_preprocess_field will not get called if the field is being shown by Views. The Views hooks won't make sense because as I mentioned I need to alter a field regardless of Views.


So this takes me to 3. You can see Drupal 8 hooks here. At first I was trying to use hook_preprocess_HOOK, and in my case mymodule_preprocess_field. But as mentioned above that was not getting called in Views displays. Then I thought about mymodule_preprocess, but deciphering its $variables array is proving daunting, and the more I read about Drupal 8, the more I am convinced that this probably is not really in the spirit of Drupal 8.

The field specific hooks don't seem to be a match, for instance, hook_field_info_alter seems like it is for altering meta data.

So finally, I came to the conclusion that with Drupal 8 being so Entities focused, I probably need to look at an entity hook. For instance one of the following:

hook_entity_prepare_view
hook_ENTITY_TYPE_view
hook_ENTITY_TYPE_view_alter         
hook_entity_view


But coming to this conclusion, a) I am not sure it is the right conclusion, and b) if it is the right conclusion, how to choose one of the above and how to actually implement it? (Note, using dsm(), debug() in Drupal 8 often results in results that are not very decipherable since so many array elements are protected.)

Anyway, I am curious how others would go about this!

EDIT: The above entity hooks are not called for taxonomy page... So I guess that takes me back to hook_preprocess which seems to be the only way to alter the field in all cases. But still trying to decode the $variables array and figure out the right methods/functions to make changes.

Thanks
You can configure views in many ways and the hooks, that are called, will change accordingly.

You can easily debug this, if you put a "catch all" hook into your module and print out the hooks that are called:

function mymodule_preprocess(&amp;$variables, $hook) {
   echo "&lt;em&gt;$hook&lt;/em&gt;&lt;br&gt;\n";
}


Then your page will be full of hooks, that are called at different places and you can try to narrow it down to the hook you need.

The next step would be to install devel and enable kint and debug the variables of the hooks, that you are interested in:

function mymodule_preprocess(&amp;$variables, $hook) {
   if ( $hook == 'views_view_grid' ) {
      kint($variables);
   }
}