0

TLDR: I need a way to consistently name/search a field that is referencing the same term reference across multiple vocabularies.

I have several Vocabularies that all apply independently to a content type. Within these vocabularies though, terms from certain vocabularies should only apply if its tagged with another term in a Term Reference field that has been added to the vocabularies.

For example:

  • Master Category (vocabulary)

    • Category A
    • Category B
    • Category C
  • Option Set One

    • Option A (applies only to Category A)
    • Option B (applies only to Category A and B)
    • Option C (applies only to Category C)
  • Option Set Two

    • Option A (applies only to Category C)
    • Option B (applies only to Category B and C)
    • Option C (applies only to Category A and C)

...and so on. The specifics of what applies to what does not matter, but only that I need to filter vocabulary terms based on them being referenced to another vocabulary term, and they can be tagged with one to many terms from the related vocabulary.

I have set it up so that the other vocabularies have a term reference to the "Master Category" and I can tag it with one to many of those. So far so good...

I can get lists of terms correctly based on this relationship using this Entity Field Query:

    $query = new EntityFieldQuery();
    $result = $query->entityCondition('entity_type', 'taxonomy_term')
        // vid is the specific vocabulary Im trying to search in this case
        ->propertyCondition('vid', $vid)             

        // This is the term reference field.  I can filter by its referenced term ids
        ->fieldCondition('field_related_terms', 'tid', array(14, 15)) 
        ->propertyOrderBy('weight')
        ->execute();

    if (!empty($result['taxonomy_term'])) {
        // Load all terms
        $terms = taxonomy_term_load_multiple(array_keys($result['taxonomy_term']));

        foreach ($terms as $term) {
            $term_options_list[$term->tid] = $term->name;
        }
    }

Ok, so far this is all great. $term_options_list contains all of the terms that have been tagged with the related terms specified in the query.

Now the problem: This actually applies to a lot of different sets of vocabularies, and I need to perform the query above completely dynamically. The issue is the machine name in the fieldCondition function. Since I can't use the same name for this term reference on each vocabulary (can't duplicate machine names) and I won't know specifically which vocabulary I'm working with, how can I make a function which can perform the query above regardless of which vocabulary it's operating on? I need to pass in the vid for the vocab to be searched, and the array of vid's that this term reference field needs to include, and get the results.

Hopefully that makes sense and it's not too long winded. Let me know if I'm going about trying to do this in the wrong way all together. Any advise is appreciated!

  • Wow, that really is a lot of text. You certainly did a good job providing additional information, something that often lacks in a question. However, I find it difficult to get to the core of your problem. It could be me, but can you describe your problem one more time in a few words/lines? – Patrick Scheffer Dec 16 '15 at 8:42
  • Yes, sorry that was a lot :) That first line kind of sums up the whole story though. I need to search any given vocabulary to get terms based on a term reference, which is a field added to several different vocabularies (one to many term references in that field). I know how to search for this and get results by the machine name in the fieldCondition query above, but since I can't repeat machine names, how can I go about performing the search above across any number of vocabularies (all of which would have this term reference added which references the same vocabulary)? – Ryan Dec 16 '15 at 9:47
  • ...think of it as I need to search all nodes tagged with specific terms, but instead of nodes I need to search other terms (across multiple vocabularies) which have this term reference as a field themselves. – Ryan Dec 16 '15 at 9:50
  • Alright, I think I'm beginning to understand it. Say, Option B from Option Set One is referenced by a node, you also want to retrieve Option A and Option B from Master category, is that right? – Patrick Scheffer Dec 16 '15 at 12:29
  • I think you've got it backwards. I want to say "Option Set One, give me all your terms that have been tagged Category A". So in that case I may get 5 terms back from Option Set One. The code above will do that. But now I need to also say "Option Set Two, give me all your terms that have been tagged Category A as well". Terms in Option Set One and Two both have a term reference to the Category vocab (allowing check boxes to check off saying "I apply to Category A and C" (or any combination of that) – Ryan Dec 16 '15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.