2

I hope this is not a stupid question I have been searching for most of the day!

I have a Content Type (Documents) which simply contains a title, file and a category. The category value is required and is 'powered' by Taxonomy.

I now wish to create a view which will display these documents grouped and titled by the taxonomy term.

Using my limited Drupal knowledge I intent to iterate through the relevant terms IDs (using taxonomy_get_tree($vid)) and then render each view accordingly.

To do this I have been hoping to use this snippet.

view = views_get_view('documents');

$view->set_display($display_id);

$filter = $view->get_item($display_id, 'filter', 'field_dl_category');

$filter['value']['value'] = $filter_value;

$view->set_item($display_id, 'filter', 'field_dl_category', $filter);

$viewsoutput = $view->render();

But this is not working; when I query the value of the $filter ($view->get_item($display_id, 'filter', 'field_dl_category')) I get null returned.

Might this be that my filter name is not the same as the CCK field name?

I am using Drupal 7.

Any help much appreciated, I am running out of ideas (and time).

2
  • So, does the "documents" view actually exist? i.e. you had created it from the Views UI? Commented Feb 26, 2012 at 5:46
  • Yes Amar the view does exist. Commented Feb 27, 2012 at 12:40

3 Answers 3

0

I finally managed to get this working but I took a slightly different approach.

I changed my view and added the relevant contextual filter and then used this function views_embed_view to get at my required results.

If this helps! this is my solution:

$display_id = 'default';
$vid = 7; 
$terms = taxonomy_get_tree($vid);

foreach($terms As $term){    
    $content = views_embed_view('documents', $display_id, $term->tid);

    //now we see if any content has been provided
    if(trim($content) != ''){
        print "<h3>" . $term->name . "</h3>";
        print $content;        
    }
}

In my case the trim($content) returns '' with no data as the view template has been edited, this might not be the case for all.

I am a very new Drupal developer so I'm sure there are much better ways of doing this, if so please do post.

Sign up to request clarification or add additional context in comments.

Comments

0

I am going to go ahead and assume that you want to show, using Views, a list of document nodes grouped by the category that they have been tagged with.

There are two (of maybe more) ways by which you can do this in Views 3:

(a) Choose a display style that allows you to select a Grouping field. (You could try the table style that ships with Views by default). Suppose you have properly related the node table to the taxonomy_term_data table through a Views relationship, you could choose taxonomy_term_data.name as the grouping field.

Note that this grouping is done before the view is just rendered. So, your query would just have to select a flat list of (content, tag) pairs.

(b) You could also make use of the Attachment display type to achieve something similar. Show the used categories first in a list view clicking on which will show a page (attachment) with all documents tagged in that chosen category.

To understand how to do (a) or (b), turn on the advanced_help module (which is not a Views requisite but is recommended) first.

For (a), read the section on Grouping in styles i.e. views/help/style-grouping.html and
For (b), read the section on Attachment display i.e. views/help/display-attachment.html

A couple of things about your approach:

(a) It will show all terms from that vocabulary irrespective of whether or not they were used to tag at least one document.

(b) views_embed_view() will return NULL even if the currently viewing user does not have access to the view. So, ensure that you catch that case.

Comments

0

Here's an alternative:

$view = views_get_view('view_machine_name');
$view->init_display('default');
$view->display_handler->display->display_options['filters']['your_filter_name']['default_value'] = 'your_value';
$view->is_cacheable = FALSE;  
$view->execute();
print $view->render();

I know you can probably set this using some convoluted method and obviously that would be better. But if you just want a quick and dirty straight access without messing around this will get you there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.