1

In my view I have a made a customised global field that returns a boolean result.

What I now need to do is if this field equals zero then the whole row should be ignored/removed from the view.

The issue here is that I cannot see this field in Filter Criteria to add as a filter. I have also looked into a programmatical solution but have looked at hook_views_post_render and hook_views_pre_render but cannot see how I can achieve this.

Any help, module suggestions or push in the right direction would be very much welcome.

3

I would use a views row template override for this. Do the following in a Development site first.

1) In the view, expand the Advanced options and click on the Theme: Information link. This displays all the available theme template suggestions.

2) Under the heading "Row style output" you will see all the possible template suggestions. Which one you override depends on your needs, but the last listed is the most specific so I would start there. Select it with your cursor and copy the file name.

3) Open this file in your code editor: sites/all/modules/views/theme/views-view-fields.tpl.php

4)Save it as the most-specific suggestion (the value you copied in step 2) and save it to your theme's template directory (e.g., sites/all/themes/yourtheme/templates or sites/all/themes/yourtheme/templates/views)

5) Now you can edit the template (the one we just saved to your theme). You can use a combination of html and PHP to achieve the exact markup and logic you want. Generally it's best practice to not code a lot of logic into your templates (use preprocess functions instead), but simple logic like checking if a field is present or has certain values is fine to include in the template.

There are many different ways to do this, but here is an example (change field_custom_boolean to the machine name of your custom global field ...

 <?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>
  <?php 
  /* if custom field is present and set to 1 */
  if (isset($content['field_custom_boolean']) && $fields['field_custom_boolean'] == '1'): ?>
    <?php print $field->wrapper_prefix; ?>
      <?php print $field->label_html; ?>
      <?php print $field->content; ?>
    <?php print $field->wrapper_suffix; ?>
  <?php endif; ?>
<?php endforeach; ?>

6) Save file and flush all caches. Now when you check the view Advance:theme information settings, you should see your template override listed in boldtype. When you load your view it should only display rows for records where the custom global field is set to 1.

Good luck.

For more info on Views templates, see:

https://api.drupal.org/api/views/theme%21theme.inc/group/views_templates/7.x-3.x

https://www.drupal.org/docs/7/modules/views

| improve this answer | |
  • Hi Anne Thanks for this. I've used views templates before but hadn't considered them this time because previously I found issues when trying to use the 'View data export' module to download view data I'd manipulated in templates. However in this scenario this solution will suffice nicely . – Deejay Dec 14 '16 at 8:18
1

If I was trying to quickly achieve this frontend, I would use JS/jQuery.

$(".view-name .boolean").each(function() {
    $this = $(this);
    $this.addClass("value-" + parseInt( $this.text() ) );
});

Then, I would just use CSS to hide it:

.value-0 {
  display: none;
}

JSfiddle

Update: It looks like Views has a new filter called the "Global: Combine fields filter". Might be worth checking out.

| improve this answer | |
  • 1
    This could be a solution only if divulging the data is not an issue. – rovr138 Feb 19 '17 at 22:56

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.