3

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:

  1. 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
  2. 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
  3. 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

1

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(&$variables, $hook) {
   echo "<em>$hook</em><br>\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(&$variables, $hook) {
   if ( $hook == 'views_view_grid' ) {
      kint($variables);
   }
}
  • That is a great function. I see the $variables structure, for instance the array element item. But $variables->item produces notice Trying to get property of non-object in mycustom_preprocess() and $variables['item'] produces a notice Undefined index: item in mycustom_preprocess() These notices, especially first, are odd since kent($variables) shows that item is an object. – Brian Huether Oct 19 '15 at 20:21

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.