3

Reading the docs about Views hooks doesn't really help me to make the right choice between all the hooks. As per this comment, this is the Views hooks execution order:

  1. hook_views_pre_view
  2. hook_views_pre_build
  3. hook_views_post_build
  4. hook_views_pre_execute
  5. hook_views_post_execute
  6. hook_views_pre_render
  7. hook_views_post_render

I'm currently trying to alter properly the output of a view field :

I tried with hook_views_post_execute(&$view) :

function MODULE_views_post_execute(&$view) {
  if ($view->name == 'foo' && $view->current_display == 'bar') {

    foreach ($view->result as $result) {

      if (isset($result->field_field_status)) {

        switch($result->field_field_status[0]['raw']['value'] ) {
          case 1: 
            $text = 'ABC';
            break;
          case 2: 
            $text = 'DEF';
            break;
          //and so on
        }  
        $result->field_field_status[0]['rendered']['#markup'] = $text;
      }
    }   
  }
}

This worked fine at first glance : returned string display nice, but raw data were displayed again after a while. After clearing page cache the expected output turns back, but then disappeared again, and so on.

Then I tried with hook_views_pre_render(&$view) , with exactly the same code inside that other hook giving exactly the same result.

Question(s) :

What makes the hook be applied or not ? Wrong hook use maybe ? Is there any logic to apply one or another of these hooks ? (After reading @Alexar comments : yes I'd explicitly like to know about views hooks, the example is just to illustrate my searches and misunderstandings.)

I really need to understand the logic behind all that for my "learning curve".

Edit :

Lastly, I used a views-view-field.tpl.php to change the output of my field, another possibility adding more confusion to my global understanding of the view construction background... Anybody for a bit of Drupal pedagogy ?

2 Answers 2

6
+50

The documentation View Hooks is clear about the purpose of every hook. It says about 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.

And gives an example what you can do with this hook:

function hook_views_post_execute(&$view) {
  // If there are more than 100 results, show a message that encourages the user
  // to change the filter settings.
  // (This action could be performed later in the execution process, but not
  // earlier.)
  if ($view->total_rows > 100) {
    drupal_set_message(t('You have more than 100 hits. Use the filter settings to narrow down your list.'));
  }
}

At this point the database query was executed and you can do something with the result of the query, for example place this warning.

When you search in drupalcontrib, you find, that it is only used once:

drupal contrib search

Here it is used following the documentation. But no one in contrib used it for other things. You would be the first, with unpredictable results.

What you want to do is not specific to views, and because of that it is not in your list of view hooks. It is more connected to theming in drupal.

It is not advisable to put extensive code in the templates *.tpl.php, better put it into a preprocess function. Some examples for views:

  • hook_preprocess_views_view
  • hook_preprocess_views_view_field
  • hook_preprocess_views_view_fields
  • hook_preprocess_views_view_grid
  • hook_preprocess_views_view_table
  • hook_preprocess_views_view_unformatted
  • hook_preprocess_views_view_list

Use the preprocess hook, that correspondents with your views configuration.

4
  • Tks for your answer. Maybe it's a language barrier or whatever, but in the post_execute example, there is no interaction with the view itself. Does it mean you can read the result but not influence the output ? About the 2nd part of your answer preprocess functions, why shouldn't I alter the output in a .tpl file please? At least it seems to be working much better than post_execute. Sorry for searching needles in a haystack, this is because I want to do exactly the right think at the right place, and to know why.
    – Kojo
    Nov 12, 2015 at 22:11
  • 1
    Please see the edits in the answer. Hope it is now clearer. It does not really matter, if you put php in preprocess or template. You will see, if there is too much php in a template, how ugly it gets.
    – 4uk4
    Nov 12, 2015 at 23:26
  • Yes, much clearer. Your explanations are very useful and valuable, thanks a lot ! But I also tried with hook_views_pre_render(&$view), with same unstable result, hook from which doc says Altering the content can be achieved by editing the items of $view->result. This hook can be utilized by themes. Isn't ` $result->field_field_status[0]['rendered']['#markup'] = $text;` an item edit ?
    – Kojo
    Nov 14, 2015 at 17:37
  • OK, never mind, I'm fine with your answer, which is a great help anyway if I still have doubts. A well earned bounty, with upvote ;) Tks for your time !
    – Kojo
    Nov 16, 2015 at 13:17
1

I am unsure about the hook_views_post_execute render issues. That is a new one on me. From what I understand about what you are trying to accomplish I would suggest using neither. For altering output I like to use preprocess hooks (if post_execute does not work). I have not tested it in this exact type of situation but you may want to look at hook_preprocess_views_view_field(&$vars). This should allow you to change the output directly.

4
  • Thank you for your contribution. I'm looking for a more explanatory answer about Views hooks, and who knows, an explanation to the inconsistency detailed above.
    – Kojo
    Nov 10, 2015 at 11:46
  • This answer is the right answer to what you asked. The answer is using theming preprocessors, etc not views hooks. If you explicitly want to know about views hooks, then you'll need to revise your question.
    – Alexar
    Nov 11, 2015 at 21:37
  • 1
    I plan on fully addressing the hooks issue when I get some time. A brief explanation on a possible reason for why this was not working for you would be cache. Think of Views hooks as query-centric. If your changing the markup in a post(query)_execute but the query is cached it can cause issues. A good way to alter markup through a function is what I detailed above.
    – sareed
    Nov 11, 2015 at 22:50
  • 1
    @Alexar, Sareed, thanks for your comments (I've had no notification because you commented the answer instead of the question, so I'm a bit late at answering). I believe my question addresses precisely Views hooks, even if I learned in the meanwhile that my example has to be done with theming preprocessors. The question indeed is How to choose between all the Views hooks and I would be very glad to understand at once what these hooks are made for, that's why I offered a bounty even if practical issue is solved. Sareed maybe could you enhance your answer? Thanks again for your time !
    – Kojo
    Nov 12, 2015 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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