2

I have a views filter on a particular field being empty (Is empty (NULL)).

However, I would like to have it check on the field being either empty or equal to the current user's uid. I would like to do this programmatically in a custom module, so without installing a contributed module.

The field in question is $node->field_quality_personal[LANGUAGE_NONE][0]['value']. So this field can be either empty, or it can contain a uid.

3

I found the way to fix this. The answer is twofold:

First create two filters: one for the field being empty and one for the field being equal to the string "uid". Then as a Filter Criteria option, choose And/Or, Rearrange and add the Or condition:

enter image description here

This is the result:

enter image description here

Second implement hook_views_query_alter() to replace the "uid" string we added earlier with the actual user uid:

/**
 * Implements hook_views_query_alter().
 *
 * Search for 'personal' field to be empty or equal to uid.
 */
function example_views_query_alter(&$view, &$query) {
  if ($view->name == 'myview') {
    foreach ($query->where as &$condition_group) {
      foreach ($condition_group['conditions'] as &$condition) {
        if ($condition['field'] == 'table.field_quality_personal' && $condition['value'] == 'uid') {
          global $user;
          $condition['value'] = $user->uid;
        }
      }
    }
  }
}

That's it!

| improve this answer | |

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.