0

I am struggling a bit with the array structure of Drupal.

I am using PHP in a contextual filter inside views. I have the same vocab on both a content type and user via profile2. I manage to get the filter working for single term. The key is in how I return the values at the end of my function.

return $items[0]['tid'];

This returns 2, a correct tid on user profile taxonomy.

I can also get other values by using:

return $items[1]['tid'];

This returns 4, also a correct tid.

How would I be able to say?

return $items[ALL_ARRAYS]['tid'];

So that it would print all the term ids associated with that vocab?

My PHP code:

global $user;
$profiles = profile2_load_by_user($user);
    foreach ($profiles as $profile) {
      if ($items = field_get_items('profile2', $profile, 'field_your_interests')) {
        break;
      }
    }
$tids = array();
foreach ($items as $item) {
  $tids[] = $item['tid'];
}
//returns nothing in view, not sure if a contextual filter can handle 'and'?
//return implode(',', $tids);

//This works with 1st value in array 
//return $items[0]['tid'];

//This doesn't return anything
  return $tids;
  • Welcome to Drupal Answers! Please don't use php: You are not asking a question about PHP. Drupal is then written in PHP; if we were to use this tag just because the question shown PHP code, 99% of the questions here will use it. – kiamlaluno Aug 31 '15 at 19:24
2

There are a few ways you can do this, but this is probably the most straightforward:

$tids = array();
foreach ($items as $item) {
  $tids[] = $item['tid'];
}

// Return this if you want to use "or" logic
return implode('+', $tids);

// Return this if you want to use "and" logic
return implode(',', $tids);
  • thanks this helped. By using dpm($tids); i can see that the array is only made up of the desired id's. I tried using the add logic to return the values, but my view keeps returning an empty result. I updated my original code to include all my code, as you can see I'm still new to this :) – Ddroid_za Aug 31 '15 at 18:34
  • @Ddroid_za do you have your contextual filter set to allow multiple values? In the settings for the contextual filter, near the bottom, I think it's under the "more" link. – Aaron Aug 31 '15 at 18:51
  • Thanks for the speedy reply Aaron.Yes I did, both the multiple "Allow multiple values" and "Allow multiple filter values to work together" checkboxes are ticked under the More section. I also made sure the distinct option is not turned on in the query settings. By providing a default fixed value in the contextual filter it all works 100%. – Ddroid_za Aug 31 '15 at 18:57
  • @Ddroid_za Many folks get confused on the difference between "and" and "or" logic. Please try return implode('+', $tids); as I suspect you're more interested in "or" logic than "and" logic. Also: note that you can test it by manually entering arguments in the live preview. Try putting in 2+4 (or, if you really do want "and", 2,4) in as an argument in the live preview to see if it gives you what you expect. – Aaron Aug 31 '15 at 19:01
  • You sir are my hero! Thanks the '+' works perfectly! – Ddroid_za Aug 31 '15 at 19:04

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.