3

I have a vocabulary called category, another vocabulary called author and the content type is videos. I want to display all the authors(unique) by category vocabulary term?

Can any one please suggest me how to achieve this using views?

1

I ran into a similar situation and hacked up a solution based upon the advanced solution here: http://drupal.org/node/770782.

Addition per commentary...

Basically what I was running into here was a situation where I had node content tagged with a genre as well as a location and what I wanted was a list of all the genres in a location.

A quick description of the view would be "Filter on published content of node type X with an argument (Drupal 6) (now Contextual Filter in Drupal 7) of location and display the genre field of each found."

A difference in my solution is that I kept count of how many nodes were tagged and I sorted on that while in the link given above it was sorted alphabetically.

Doing this with Views created lots of duplicate rows obviously because it was listing each node it found with that genre.

So, basically what I ended up doing was going into hook_views_pre_render() and looping through the results and removing all duplicates, eg:

function YOUR_MODULE_views_pre_render(&$view) {

  if ($view->name=='THE_NAME_OF_THE_VIEW_YOU_ARE_LOOKING_FOR') {

    drupal_set_message('<pre>' . print_r($view->result) . '</pre>'); // to get the correct $result->FIELD_NAMES for your particular View

    // what we are first going to do is go through the results of the View
    // and create an array of unique taxonomy ids that we find.
    // we are going to store that info in array $unique_tids
    // we are also going to keep a count so we can sort on "popularity"

    $unique_tids=array();
    $count_tids=array();

    foreach($view->result as $r => $result) {
      if ($result->term_data_vid != 'THE_VOCAB_WE_ARE_LOOKING_FOR' ) { // looking back, I probably could improve the View substantially by filtering on just one taxonomy vocabulary :)
        unset($view->result[$r]);
      } else {
        $count_tids[$result->term_data_tid]++;  // keep count of how many times we see this tid
        if(in_array($result->term_data_tid, $unique_tids) || !$result->term_data_tid) { // if we have already seen this tid, or it does not exist, delete this row from the results
          unset($view->result[$r]);
        } else { // we haven't seen it, so keep it in the View results
          $unique_tids[] = $result->term_data_tid;
        }
      }
    }

    // at this point we could just return and our View would contain the
    // unique genres, but it would be "sorted" based upon the order in which
    // the node it was first tagged in was created (I think) but we want to
    // get fancy and sort this listing with the most popular genres first
    // based upon how many pieces of content has been tagged with it, so...

    arsort($count_tids); // sort the counts in reverse order so the largest counts come first
    $new_result=array();

    // crude but effective ... curse you for making me look at years old bad code :)
    // but basically go through the counts and find the Views row that corresponds

    foreach($count_tids as $c => $count) {
      foreach($view->result as $r => $result) {
        if ($result->term_data_tid == $c) {
          $new_result[]=$result;
        }
      }
    }

    // we now have our unique results in the order we want so set
    // the View to it...

    $view->result=$new_result;

  }

  // any other pre_render()ing goes here...

}

And that's what I did to present unique genres in a location sorted by how many pieces of content were tagged with a particular genre. Let me know of any/many typos in the code (I had to clean out a lot of extraneous code to get it down to the basics) and or if you would like any other explanations. Also looking back (I wrote this in my nube stages of Drupal) I can clean up a lot of it. Thanks Drupal Answers for making me go back and rewrite totally working two year old code the right way :)

Oh, looking at comments in my code, this created havoc with paging so in my system I just listed everyone on one page since I didn't have all that many genres to worry about. I think if you did this in hook_views_post_execute() instead it would respect paging.

  • Hello. I know you posted it long ago, but could you maybe turn it into something more than a link-only answer? Sorry but my understanding of the article linked is not enough to do this. – Mołot Dec 3 '13 at 7:43
  • Wow. This was a while back. Sure, be glad to. Let me write up what I did. – Jimajamma Dec 3 '13 at 12:42
  • Blame Community, he poked it to bring it to the front page where I could see it ;p – Mołot Dec 3 '13 at 12:49
  • 1
    Thanks Drupal Answers! I now revisit spaghetti code of my youth that works fine but should be fixed :) – Jimajamma Dec 3 '13 at 13:53

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.