2

On a business review page, I have a views block which shows each individual review submitted for the business. In the review form available to the user, I am using integer fields to allow the user to select their choices and to store the submitted information in the database. For instance, if a user selected in their review that the staff was "Very Helpful," then a 5 is stored in the database. This allows me to show the average value on the main business page and change the output based on php (using computed field). However, because of the way that the data is stored, the view block displays a "5" in the field for Staff Helpfulness instead of "Very Helpful."

So, the topic of the question, how can I rewrite text in a view field based on its value (i.e. replace the displayed "5" with "very helpful")?

2 Answers 2

0

You need to create a template for your field. Field templates are extremely simple. Their content is:

<?php print $output; ?>

So you should go and change the template to something like this:

<?php
 if($output == 5) {
    print "very helpful";
  }
  else if($output == 4){...}
?>

and so on. In the advanced section of your view hit "Theme" to see a list of available templates you can create. If you want to do all this through drupal interface you can do it using Rewrite the output but you will need to enable another module to be able to write php code inside the box. Take a look at Views PHP, Views Custom Field. I suggest you choose the template way. Your template is fairly simple.

1
  • That's the ticket. I had already installed Views Custom Field to try and solve something else, but I haven't quite gotten it to work correctly. This was much easier.
    – Mrweiner
    May 18, 2012 at 16:12
0

you can rewrite the output of a field using "Rewrite the output" option in views.

In your case, you might also need php code: ex:

   $selectedValue=5; // this you can get from the replacement tokens easily

   // this array can be populated using some db query too.
   $selectedText=array('1'=>'satisfactory','5'=>'Very helpful');

   $selectedDisplayTitle=$selectedText[$selectedValue];
   return $selectedDisplayTitle;

Hope this helps. Let us know if you come across a better solution.

1
  • See the other suggested answer for a better explanation.
    – Mrweiner
    May 18, 2012 at 16:12

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.