0

I have a Views Page which lists nodes of the content type, Article. This page also contains pagination since I configured it to display no more than 6 articles per page.

So for example, if I have 10 articles, 6 of them would be displayed on page 1 and the remaining 4 would be on page 2. In addition, Article has a term reference field called Tags.

I was wondering if it's possible to have a Views block I can attach to my original View I mentioned earlier (a list of Article content type) that lists Tags belonging only Articles currently visible on the original View on one of the paginated pages.

Going again with my example, I want this Views block to display Tags from the 6 Articles when I'm on page 1. Likewise, I want this block to display Tags from the remaining 4 Articles when I'm on page 2.

Here's a little graphic of what I'm trying to do. http://i.imgur.com/7rPsLaz.png

Also, I'm not sure if this info is relevant but I plan to use the Context module to attach this Views Block to the Views page.

I was thinking along the lines of Contextual Filters but I'm not quite sure what arguments I would use.

Any help is appreciated.

0

Offhand i'm not sure how Context will help you do this ... off the top of my head this solution came to mind.

  • In your 1st view make the Tag field a hidden field the view so the View knows about it.
  • Use hook_views_pre_render to loop through the results (and maybe respect page offset and # of items per page -- you can get that from the $view object) and grab your view field Tag values and store them into an array.
  • Still in hook_views_pre_render you can set $view->attachment_after = $some_string; where $some_string is like the list of your Tags.

In code it will look something like this:

function MYMODULE_views_pre_render(&$view) {
  if ($view->current_display == 'VIEW_ID') {
    $tags_on_page = array();
    // $_GET['page'] has page number.
    // $views->items_per_page has items per page count.

    $page_offset = intval($_GET['page']);
    // this is somewhat wasteful, but easier than a loop at the moment.
    $paged_results = array_chunk($view->result, $view->items_per_page, TRUE);

    // Build up the Tag list for X page of the results.
    foreach ($paged_results[$page_offset] as $i => $result) {
      $tags_on_page[] = $result->value; // save the taxonomy term name to the tags list.
    }

    // Take our tags and build a block of HTML to embed after the view.
    // Alternatively, maybe take these tag values and pass them as parameters
    // to a new view we attach after programmatically -- thats more work.

    // if you know the url prefix you've given these taxonomy terms use it here, or use something like entity_uri().
    $num_tags = count($tags_on_page);
    if ($num_tags) {
      for ($i=0;$i<$num_tags; $i++) {
        $term = $tags_on_page[$i];
        $tags_on_page[$i] = l($term, '/URL/ALIAS/BASE/PATH/TO/TERM' . $term);
        // or, ... $term may have a path value already ... I cant remember :).
        // $term = taxonomy_get_term_by_name($tags_on_page[$i]);
        // $term_path = entity_uri('taxonomy_term', $term[0]);
        // $tags_on_page[$i] = l($term[0]->name, $term_path);
      }
      $links = implode('<br>', $tags_on_page);
      $view->attachment_after = '<div class="myBlockAfterView"><h3>Related Tags</h3>' . $links . '</div>';
    }

  }
}
  • I should really look at attached pics before I start writing code ... now i dont understand if your tiling those results on a page, or those are per-page example boxes ... and my code, probably doesnt do whatever that pic is showing. sigh. – tenken May 1 '13 at 15:27
  • Haha no worries, Tenken. I still appreciate the input! As for your inquiry, the explanation gets weird because a "Page" generated through Views can be a collection of pages if Pagination is enabled. So the block would be a per page basis...but automatically rebuilt since each page would contain different Articles with different tags. – johnmiste May 1 '13 at 16:07

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.