I have been trying to use og_vocab terms in some of my views exposed filters.
I am creating a view using EVA and loading in group content by contextual filters and relationships which works fine.
I am using og_vocab vocabularies and terms to create some subgroups.
e.g
Group 1, vocab x, terms subgroupa, subgroupb
Group 2, vocab y, terms subgroupc, subgroupd

What I wanted to do is when a user was looking at a view they'd see the group context and be able to filter by these taxonomy terms. Not rocket science you'd think but for the life of me I couldn't work out the configuration to get the filters to respect the group context.
I could set taxonomy = blank textbox in filters, or I'd check the box 'Render Views filters as select list', which would load in the terms in my exposed filters, but they wouldn't respect the group context.
I tried various contextual filters but couldn't work out how to expose only the appropriate terms.

As a workaround I have developed a hook_form_alter function as below.
My question is, am I missing some configuration which would allow me to do this within views, if not I wonder how this could be made more useful for the og_vocab project?

/*Implements hook_form_alter*/
function mymodule_form_alter(&$form, $form_state, $form_id) {

  if ($form['#id'] == 'views-exposed-form-[VIEW-NAME]-[WHICHEVER VIEW NUMBER]') {

    // Get the current og group context
    $current_context = og_context();
          
    //Get the vocabularies of the group(s), we need to do some looping if there was more than one vocab per group
    $current_vocab = og_vocab_relation_get_by_group('node', $current_context['gid']);
    $current_vocab_vid = $current_vocab[0]->vid;
          
    //Query the taxonomy_term_data table
    $terms= db_select('taxonomy_term_data') 
    ->fields('taxonomy_term_data', array('name', 'tid'))
    ->condition('vid', $current_vocab_vid)
    ->execute()->fetchAll(); 
          
    //Build an array for the dropdown list
    $dropdown_array = array("All" => t("- Any -")); 
    foreach ($terms as $item) {
      $key = $item->tid;
      $value = $item->name;
      $dropdown_array[$key] = $value;
    }
     
    unset($form['og_vocabulary_target_id']);
    $form['og_vocabulary_target_id'] = array(
      '#type' => 'select',
      '#default_value' => 'All',
      '#options' => $dropdown_array,   
       );
  }
}

Comments

roysegall’s picture

mexicoder’s picture

Hi RoySegall

yes I looked at that article a number of times but couldn't work out if it was trying to solve the same issue I was facing- it is a bit of a messy thread. Do you know if that patch would work for my issue and if so which views configuration I should use?

roysegall’s picture

I pretty confused from what you describe all that EVA and your data structure, i'll try to make it more simple:
You want to add filter by terms in your views, and the terms you want to filter by is the terms from the group that user watching now - right?

mexicoder’s picture

Hi RoySegall

yes that is about it- I want to expose those filters to the user.
My code works perfectly but I just wondered if I was missing something that I could already configure within the view?

roysegall’s picture

The path i suggested here add to relation ship. You can have a look in last patch i suggested that actually contained a view that add a block which display the term in the group the user is currently at. If you based on that view you can add an expose filter.

You also might want to use the entity field query and not the db select: http://drupal.org/node/1343708

mexicoder’s picture

Hi thanks
yes I tried the patch and the route you suggested but basing my view on the og_vocab didn't make sense in this case because not all of the content I was bringing in was tagged with a term but thanks anyway.
Yes I looked at entity field query but was maybe being dumb but couldn't see how to get two fields in the same way I am doing with the database query as in my code example?

Renee S’s picture

Yeah, I have a similar need. The views integration in that issue only works when the base table is og_vocab, and not for a regular content listing (eg: with a contextual argument of OG). I'm trying to do something similar: let users filter a list by taxonomy, on a group homepage. Is that possible with the current views integration? I'll give @dumbass's solution a try in the meantime, but it could be extremely useful functionality...

eta: giving it a try, there's no way to not choose a vocabulary when adding any kind of taxonomy filter. Hmm.

mexicoder’s picture

Hi Renee S I have got my solution working pretty well for this issue and have polished my code, if you are having any issues let me know and I'll either post up some new code or may be able to answer some questions if you have any.

roysegall’s picture

I don't see the problem when basing on the og_vocab table - you still get the terms that in your group like in this patch: http://drupal.org/node/1963898#comment-7285952.

mexicoder’s picture

Hi RoySegall in my case not all of the content I wanted to pull in had an og_vocab term.

Renee S’s picture

Hi @RoySegall, yes, as I say above, it works fine on the og_vocab table, but I want a listing of content, not terms.

@dumbass: if you have some code, I would love to take a look! Possibly we could extend it to be a views plugin for og_vocab...

roysegall’s picture

@Renee S you need to make a view that display content relate to the terms on your group?

Renee S’s picture

Ah, yep, I got it sorted, you're right, that's easily doable with relationships. But only one og_vocab can be used at a time. Is there a way to use a contextual filter to grab the group somehow?

Renee S’s picture

Ok, so I think actually this is just a bug report: adding Content: Tags (og_vocabulary) as an exposed filter doesn't work. The views form doesn't give you the option to select a relationship... so the query is expecting tid and it gets 'term name'.

roysegall’s picture

Can you supply screen shots

Renee S’s picture

StatusFileSize
new29.82 KB
new32.63 KB
new78.52 KB

Screenshots: the relationship shows up (have to add both entityreference bridge and the og_vocab Taxonomy terms on Node), but it doesn't actually have any effect.

eta: darn it, I had nice descriptions for those. (shakes fist at d.o)...

weri’s picture