3

I want to preprocess view to alter a field based on another field. I am able to print the value but not change it.

I did this:

function MYTHEME_preprocess_views_view__VIEWNAME(&$vars){

     kpr($vars['view']->result['0']->field_field_name[0]['rendered']['#markup']);
}

Which prints the correct value. But when I try to change/unset the value it does not work. Both of these do not work.

 unset($vars['view']->result['0']->field_field_name[0]['rendered']['#markup']);

 $vars['view']->result['0']->field_field_name[0]['rendered']['#markup'] = '';

I also tried another way which prints the value but does not change it:

 unset($vars['view']->result['0']->_field_data['nid']['entity']->field_name['und'][0]['value']);

Note: I am rewriting the results of a field with view UI.

13

The MYTHEME_preprocess_views_view($vars) hook is executed at the end of the process to build a view. The purpose of the views_view.tpl.php is a general wrapper for all elements of the view. The result of fields have been builded and rendered and they have been stored in the variable $vars['rows'].

If you want to change, alter, unset a view, we can do in different phases. Between them, i've chosen the pre-render phase:

function HOOK_views_pre_render(&$view) {
  if(isset($view->name) && $view->name == 'YOUR_VIEW'){
    // kpr($view);
    foreach($view->result as $r){
      $r->field_field_NAME[0]['rendered']['#markup'] .= '. ADDING TEXT TO CHANGE THE RESULT';  
    }
  }
}

These are the stages that are given by Views API (you will find it in the views folder). Maybe one of them is more useful:

hook_views_pre_view(&$view, &$display_id, &$args) {}

Allows altering a view at the very beginning of views processing, before anything is done.


hook_views_pre_build(&$view)

This hook is called right before the build process, but after displays are attached and the display performs its pre_execute phase.


hook_views_post_build(&$view) {}

This hook is called right after the build process. The query is now fully built, but it has not yet been run through db_rewrite_sql.


hook_views_pre_execute(&$view) {}

This hook is called right before the execute process. The query is now fully built, but it has not yet been run through db_rewrite_sql.


hook_views_post_execute(&$view) {}

This hook is called right after the execute process. The query has been executed, but the pre_render() phase has not yet happened for handlers.


hook_views_pre_render(&$view) {}

This hook is called right before the render process. The query has been executed, and the pre_render() phase has already happened for handlers, so all data should be available.


hook_views_post_render(&$view, &$output, &$cache) {}

Post process any rendered data.

  • In Drupal 7, what file do you set these hooks? View template or some other file? – Dejan Feb 25 '14 at 14:53
  • You could set a hook in the template.php file of your custom theme or in your custom module. – Rufein Feb 27 '14 at 14:44

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.