4

I have a Views table that simply shows a list of Entities.

Now at the moment, field 'status' just outputs the raw data 0 or 1.

I want this to be rewritten as 'Pending' or 'Approved' respectively.

I've looked at rewrite behaviour but this seems to be limited and my requirements out of the scope. Everywhere people say that adding PHP through the UI is bad practice.

So how do people normally deal with select boxes stored as integers, when displaying the data using Views?

(Maybe there is a hook to create a function in my module that will always rewrite this field wherever it displays in the Drupal system?)

3 Answers 3

5

You could do this several ways, the easiest from a hook standpoint might be to use views_pre_render(), eg, something along the lines of...

function YOURMODULE_views_pre_render(&$view) {

  if ($view->name=='THE_VIEW_YOU_ARE_LOOKING_FOR') {

     drupal_set_message('<pre>' . print_r($view->result, TRUE) . '</pre>'); // to see what you have to work with

     foreach($view->result as $r => $result) {
      if ($result->node_data_field_status_field_status_value) { // or whatever the $result field is you are looking to change from 0 or 1 to Pending or Approved
        $result->node_data_field_status_field_status_value='Approved';
      } else {
        $result->node_data_field_status_field_status_value='Pending';
      }
    }

  }

}
0

There is also a module that can help you achieve this. It's View conditional. With that module you can add conditional field like: if this field is equal to/not equal to/empty/not empty/greather than/contains ecc something, then write something, otherwise write something else.

0

I had the same problem in a view of entityform submissions and I found that it is possible to change the results "0" or "1" of the field "Entityform Submission: Draft" in this way:

1) Select "Rewrite the output of this field" and insert 'Pending' in the text area
2) In "No results behavior" insert 'Approved' in the text area, check Count the number 0 as empty and check Hide rewriting if empty

I hope these configurations could help you!!

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.