1

I have a view filtering out a specific node by nid, and it was working until I added a relationship. I want to filter based on the nid of the target of the relationship, not the base table nid. Here is my code, pre-relationship:

$options = array(
  'operator' => '!=',
  'value' => array('value' => $nid),
);
$view->add_item ($display_name, 'filter', 'node', 'nid', $options);

I tried replacing nid with the view relationship base field alias, but that didn't seem to be recognized at all. How do I indicate to Views that this filter should work via the relationship?

2

I figured this out by a stroke of luck after posting this. Here is what I did:

// Get a list of all relationships on the view display
$relationships = $view->get_items('relationship', $display_name);
// In case there are no relationships, set this to null by default.
$last_relationship = null;
if (!empty($relationships)) {
  // @todo: is the last relationship always the one whose ID goes with the returned entities?
  $last_relationship = array_pop($relationships);
}
// Filter out the (un)desired item.
$options = array(
  'operator' => '!=',
  'value' => array('value' => $id),
);
if ($last_relationship) {
  $options['relationship'] = $last_relationship['id'];
}
$view->add_item($display_name, 'filter', $view->base_table, $view->base_field, $options);
| improve this answer | |
  • Comparing this to your original code, I'd say, it can't be all luck :p – No Sssweat Aug 18 '16 at 6:48

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.