Views hierarchical taxonomy select box - drupal

I'm trying to create a Drupal web for a travel agency and need your help.
I store locations in a taxonomy with hierarchy, the structure looks like this:
Europe
France
Paris
Germany
Berlin
I'm using a view with "Has taxonomy terms (with depth)" filter, exposed as a selectbox.
"Show hierarchy in dropdown" is enabled
It works fine, but the selectbox starts to get pretty huge as the number of the location is increasing.
So my question is:
Is it possible to show just the 1st and 2nd level of the taxonomy in the select box?
Or is it possible to show each level of the taxonomy in a separate select box?
Thank you!

Use hierarchical select module to create select list. This will give you a hierarchy with '-' symbol in front all child elements.
Then you can alter the form with hook_form_alter function.
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if (($form_id == 'your_form_id')) {
foreach ($form['tid']['#options'] as $term_key => $term) {
// Check if this is a child by looking for '-' as first char in string
$term_value = reset($term->option);
if($term_value[0] == '-') {
unset($form['tid']['#options'][$term_key]);
}
}
}
}

You should check this link. I am pretty sure, you will be able to it by some custom coding in any custom module or template.php
You can check this module too, really helpful.
Thanks

"is it possible to show each level of the taxonomy in a separate select box?"
The only module I have seen that might be useful to you is the Hierarchical Select module. http://drupal.org/project/hierarchical_select

Related

Use views to select nodes

I'm trying to find a way to let users select nodes from views results, and then get information from the selected nodes (such as node ID) for use in my module. This would probably be done in a form.
More broadly, what I'm trying to accomplish is to present users with a list of nodes tagged with a certain term x, have them to select any number of nodes from that list, and then have my module apply another term y to the selected nodes. I can handle that last part, but I'm struggling with creating a list of nodes that users can select from, and then somehow getting the information about the nodes selected that way.
I assumed views are the way to go but after a lot of searching I haven't found a way to achieve this functionality. Can anyone can show me a solution or point me in the right direction?
Thanks!
I'm using Drupal 7, and Views 7.x-3.7
EDIT: If I had the ability to select nodes with checkboxes via a module like VBO, I would like to do something like the following (terrible) pseudo-code:
foreach (vbo_selected_node) {
$node = vbo_selected_node -> node;
$nid = $node -> nid;
$node = node_load($nid);
$node->field_vocabulary_field['und'][0]['tid'] = $termID;
}
I hope that makes sense. Basically I want to take each selected node and apply another term to it.
I've been through a similar scenario and came up with a solution through custom module. I'm not sure if it is the best option but still you can use node_load_multiple() method. A quick reference can be found in
http://eureka.ykyuen.info/2012/08/08/drupal-7-get-mulitple-nodes-using-entityfieldquery-and-node_load_multiple/

Drupal Views Entity Reference Context

I'm running Drupal 7 with Entity Reference and Organic Groups. I have two content types, one of which is a group and the other is group content. I have an Entity Reference field (Select List) that references group content associated with the group.
I want to create a View that ONLY shows the value of the field that is selected from this Entity Reference field in the group content type.
For instance:
Team: Red Sox
Location: Fenway
Location is a content type (group content) and Team is the group. There are many teams and many locations but when I'm looking at the group page I want a View that ONLY shows a single location (the one SELECTED in the group content type).
After much research I realized that Views and context can't handle this on their own. I ended up using View PHP to construct a filter that effectively filtered out all OTHER results than the one that I wanted:
$node = menu_get_object();
$item = field_get_items('node', $node, 'field_name');
$loc = $item[0]['target_id'];
$refnode = node_load($loc);
$primary = $refnode->title;
if ($primary != $row->title) {
return TRUE;
}
You're welcome for this one :) If anybody has any better suggestions on how to code this feel free to comment or post alternate solutions.

Drupal - Views. Setting a filter programmatically

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).
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.
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.
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.

Use Wordpress custom post in a contact form

I have created custom post type "Product" in Wordpress and I would like to use Products within my contact form. For example, I would like to have a drop down that is a list of all of my Products so users can select a Product name as the message's Subject. I have Contact Form 7 installed. Is there an easy way to do this?
Thanks !
I think the short answer is no. There is not an easy way to do this. The Contact Form 7 plugin uses shortcodes to construct the select lists. What you need to do is run a query on your Posts -> Products and generate your own select list. I suppose what I would do is write my own shortcode function. Then you can include it in your page.
[myProductsShortCode]
Then you can iterate through that result set and generate your own select list.
http://codex.wordpress.org/Shortcode_API
http://codex.wordpress.org/wpdb#query_-_Run_Any_Query_on_the_Database
People seem to be able to add custom information like that, from
function test_generator() {
/* need to produce html like this:
<span class="wpcf7-form-control-wrap menu-645"><select name="menu-645" class="wpcf7-select"><option value="one">one</option><option value="two">two</option></select></span>
so here we go: */
$list = "<span class=\"wpcf7-form-control-wrap menu-test\"><select name=\"menu-test\" class=\"wpcf7-select\"><option value=\"test1\">test-1</option><option value=\"test2\">test-2</option></select></span>";
return $list;
}
wpcf7_add_shortcode('test', 'test_generator');
and then just use [test] in the contactform

Drupal 6: Taxonomy splot up in managed fields?

So you have two taxonomies, namely: "Business Type" and "Location"
This is assigned to a node called BUSINESS. In effect, when the user creates a BUSINESS node, her has to choose for example, location "New York" and type "Information Services". My problem is when:
a) Capturing the taxonomy, and
b) Displaying the taxonomy
I want the two terms to be separated from each other. I.e. I want to be able to move the two terms individual positions in the MANAGE FIELDS view, so that they can be grouped or placed seperately. Currently, Drupal only allows one entry, called "TAXONOMY" which is effectively the two terms next to each other.
This is what I have:
alt text http://www.namhost.com/have.jpg
This is what I want:
Bare in mind, I need to be able to use this with Hierarchical Select, which means Content Taxonomy is not an option.
You'll have to separate your problem in 2 parts:
The form filling part, which will have all vocabularies together to the editor.
The content display part, which you'll be able to separate vocabularies.
I'll cover here more the second part, about displaying.
Use the CCK computed field module and create one field for each vocabulary you want to display. Position this field where you want them.
Configure each field as follows:
On the Computed Code, put something like this:
# Get vocabulary ID from its management URL (/admin/content/taxonomy/edit/vocabulary/[VOCABULARY_ID]) and set here:
$node_field[0]['value'] = "5";
# Also, configure this field as 'Raw Text' on Display Fields
On Display Format, use this:
$vocabulary_id=$node_field_item['value'];
$terms=taxonomy_node_get_terms_by_vocabulary($element['#node'], $vocabulary_id);
foreach ($terms as $tid => $details) {
# The taxonomy_get_textual_term_hierarchy_by_id() is implemented on the SolutionHub's theme template.php file
$textualTerms .= taxonomy_get_textual_term_hierarchy_by_id($tid);
}
if (isset($textualTerms)) {
$display='';
$display.=$textualTerms;
$display.='';
}
The taxonomy_get_textual_term_hierarchy_by_id() function is specific to my site and is defined in DRUPAL_ROOT/sites/default/themes/mytheme/template.php and simply rewrites the taxonomy term text in a fancy way to show its entire lineage. So instead of "apple" I'll get something like "food > desert > fruit > apple". I won't paste it here cause it is out of scope.
If your problem is to reposition the vocabulary in the edit form, I would suggest the Content Taxonomy module.
You are stuck with the two taxonomies appearing together in the input form, they come as a package. Taxonomy should be used as a classification system (like animal kingdom classifications) so the terms to belong together in physical space.
But for the other half of your question, keep in mind your users will see the 'Business type' and 'Location' labels in the input form, not the generic 'Taxonomy' label that you see when managing fields.
maybe you could do better with cck. by enabling text (comes with cck) you can add text fields. and you can easily use them separately, use them with views, templates, etc.

Resources