Is there a way to hide/disable dropbox of a parent in hierarchical select?

Here is a situation where such a feature will be very useful: Consider a vocabulary named 'Produce'. It has the following hierarchy.

Produce
-----------
Milk
-- Organic
-- Natural
-- Hormone free
-- Conventional
Fruit
-- Organic
-- Natural
-- Conventional

I have a node called 'product' which links to a node of type 'fruit' or 'milk' via node reference.Hierarchical select lets me configure dropdown boxes to select the tags. I have preset the parent through action/trigger but I also want to hide/disable the parent (fruit/milk) so that the user cannot edit it. Effectively, I want to display only the dropdown box for child nodes (organic, conventional, etc.) depending on whether the product is defined to be of type fruit or milk.

Is this possible with the current hierarchical_select type? If not can we add a feature that will enable selection of some # of levels pf parents to be hidden if one or more tags have been already selected at those levels?

Thanks
iyer

Comments

wim leers’s picture

Component: Code - Taxonomy » Code
Assigned: Unassigned » wim leers
Status: Active » Postponed (maintainer needs more info)

1) Because this requires support for node reference, this requires a custom implementation (i.e. write-your-own) of the HS API.
2) It's not possible to mark a level as "locked" (or "disabled", in HTML terms).
3) You can of course show the parent through a form item with #type = item and then use a form item with #type = hierarchical_select to select the child.

ilakshmir’s picture

Hi Wim

Thanks for your response. I wrote about the node reference to provide the context for the design problem. I am able to create the parent tag (milk/fruit) but am not able to theme the #type = hierarchical_select to show the drop down boxes only for the children. Are you saying that this can be achieved with

3) You can of course show the parent through a form item with #type = item and then use a form item with #type = hierarchical_select to select the child.

This is not clear to me. Can you elaborate a bit more?

Thanks
iyer

wim leers’s picture


$selected_parent = 'milk';

$form['selected_parent'] = array(
  '#type' => 'item',
  '#value' => $selected_parent,
);

$form['children'] = array(
  '#type' => 'hierarchical_select',
  '#config' => array(
    // This is the HS config array, see API.txt as a reference.
    // Since this will have to be a custom implementation of the
    // HS API, you can choose which parameters are necessary.
    // Just pass through the selected parent and you should have
    // enough context to generate a list of children.
    'params' => array(
      'selected_parent' => $selected_parent,
    ),
  ),
);

I hope that helped you.

ilakshmir’s picture

Great! I think I have enough information to get going.
Thanks again!
Iyer

ilakshmir’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

I am sharing my piece of code for those looking to do a similar thing:

1. Create a new module 'new_hs_taxonomy' as a copy of hs_taxonomy.module and rename the functions appropriately.

2. In the function below, set 'module' to the new name: 'module' => 'new_hs_taxonomy',
function hs_taxonomy_form_taxonomy_form_term_alter(&$form, &$form_state)

3. Modify the function to look like the one below:

function new_hs_taxonomy_hierarchical_select_root_level($params) {
//$terms = _hs_taxonomy_hierarchical_select_get_tree($params['vid'], 0, -1, 1); // I commented this line
// new code begins
if (isset($params['selected_parent'])){
$terms = taxonomy_get_children($params['selected_parent'], $params['vid']);
}else{
$terms = _new_hs_taxonomy_hierarchical_select_get_tree($params['vid'], 0, -1, 1);
}
// new code ends

// If the root_term parameter is enabled, then prepend a fake "" term.
if ($params['root_term'] === TRUE) {
$root_term = new StdClass();
$root_term->tid = 0;
$root_term->name = '<'. t('root') .'>';
$terms = array_merge(array($root_term), $terms);
}

// Unset the term that's being excluded, if it is among the terms.
if (isset($params['exclude_tid'])) {
foreach ($terms as $key => $term) {
if ($term->tid == $params['exclude_tid']) {
unset($terms[$key]);
}
}
}

return _new_hs_taxonomy_hierarchical_select_terms_to_options($terms);
}

4. Add a hook_form_alter to add the selected_parent to params :
$form['taxonomy'][$vid]['#config']['params']['selected_parent']=$tid;

GreenSpiderDesign’s picture

I have a situation where I want to create States, and within each State list Counties, and within each County list Areas, and within each Area list Cities, and within each City list Neighborhoods. I want this, for example, to work such that if you select California you see the Counties that exist under California, and once you select a County you then see the Areas within that County, and so on.

I believe this module is exactly what I need; however, I am a bit confused on implementation. Do I create a single vocabulary under which I put each of these "levels" and under each level create the contents of that level? My understanding would be that under such an implementation I would have to have County under every State listing and have Area under every County listing, etc., which doesn't seem very efficient from a populating perspective.

Or do I create a different vocabulary for each of the levels (state, county, area, city, neighborhood) and then somehow make the associations from there?

I'm a bit confused! HELP!

Thanks,
Terry

3magnus’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev
Assigned: wim leers » Unassigned
Status: Closed (fixed) » Active

An option on Hierarchical SelectSite-wide settings to enable/disable dropbox for taxonomy parenting selection would be the best, so would allow updating too!

I'll see what I can do, but I'm pretty new at this.

3magnus’s picture

Category: support » feature
Status: Active » Patch (to be ported)

Ok, I think I've got it!

I did some changes on:

I know the ideal solution would be vocabulary by vocabulary. But does the trick for now!

Hope it helps.

wim leers’s picture

Status: Patch (to be ported) » Closed (fixed)

1) Please don't reopen ancient issues: just create a new one! :)
2) Please post the patches as attachments to your comments, don't host them on your site! We want patches to Live Forever :)

3magnus’s picture

Version: 7.x-3.x-dev » 6.x-3.x-dev

As suggested, the 7.x-3.x-dev version of this issue has been moved to http://drupal.org/node/1782668.