1

I broke a site yesterday using views global php, even if it was working in the preview, so I'm a bit nervious about that php in database and I'm trying to implement the same thing with Views API.

Depending on the key value of a hidden field in the current display, I need to put a custom text. Something like (global php version) :

if( isset($data) && isset($data->field_field_statut_depart) ) {

  switch(  $data->field_field_statut_depart[0]['raw']['value'] ) {

    case 1: 
      $text = 'ABC';
      break;
    case 2: 
      $text = 'DEF';
      break;
    //and so on

  }
  print $text;
}

I'm trying to build it with hook_views_pre_view(&$view, &$display_id, &$args) but I can't find the required field_statut_depart value in dpm($view).

Maybe it's not the right hook ?

EDIT : after applying solution described in the answers below (views_post_execute hook) and 12 hours running, I'm facing a strange behaviour : the changes made by views_post_execute hook are not stable : after clearing caches, they are visible for a while but raw data (without my function applied) is displayed then ! I'm afraid it's still not the right place to do that. Any help is welcome.

2

You may want to try the views_post_execute hook.

Otherwise there is a reduced list of hooks used for views rendering at the bottom of the Views Hooks API page.

It is a little hidden but I have used it many times and you may find a solution to your issue there.

  • please have a look at the question, savoyard ;) The view does not display the processed data but the raw values after a while ! – Kojo Nov 6 '15 at 12:55
1

Thanks to @kartsims suggestion, this did the trick :

function MODULE_views_post_execute(&$view) {
  if ($view->name == 'foo' && $view->current_display == 'bar') {
    //dpm($view);
    foreach ($view->result as $result) {
      //dpm($result);
      if (isset($result->field_field_statut_depart)) {

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

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

        $result->field_commerce_stock[0]['rendered']['#markup'] = '';

        if($result->field_commerce_stock[0]['raw']['value'] < 6 ) {
          $result->field_commerce_stock[0]['rendered']['#markup'] = $result->field_commerce_stock[0]['raw']['value'].' items remaining';
        }  
      }
    }   
  }
}

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.