I want the user to be able to choose whether to show a description or not for each node in the view. Adding the filter only works on the entire object, I just want to show/hide one field. How can I achieve this?
2 Answers
You can do this with Views out of the box. No need for Views PHP here.
Add your boolean field in Views. (This assumes the default boolean set up, i.e. 0 for not checked and 1 for checked.)
Use the Key formatter - this isn't obvious, but I couldn't get it to work without this.
Select REWRITE RESULTS and put the description you want in there. This can include tokens so you can grab material from other fields in the node if that's what you need.
On NO RESULTS BEHAVIOR check all the boxes (Count the number 0 as empty, Hide if empty, Hide rewriting if empty)
You should be able to accomplish this with th Views PHP module.
Set up your view with the checkbox and description fields included but not visible. Then include a Global:PHP field, and under the field configuration put something like:
<?php
echo ($row->checkbox) ? $row->description : '';
?>
in the Display box.
-
"While this module allows you to directly use PHP inside views which may be useful for quick and easy solutions, it is highly advisable to use regular handlers and plugins when available (or even to create one yourself). Take note that filtering and sorting a view using PHP always has a considerable perfomance impact."– HugoCommented Jan 20, 2016 at 12:05
#states
FAPI element in the Drupal API. Or, just just custom jQuery on the page ...