Community & Support

Prevent first taxonomy term from being default selection

In one of my content types I have a required category field with a hierarchical listing of categories. However, Drupal automatically sets the first item listed as selected by default. I'm worried that users will gloss over this and/or just accept the default.

Is it possible to set it so that *no* term is selected initially, so the user is forced to actively choose one before he can save the node?

So for example, the first item listed would be something like "". If the "" option were submitted, the taxonomy module would throw an error and prevent the node from being saved until the user selected a valid category.

Comments

Has anyone got any solution?

Cause I need a solution to the exact same problem too!!

Probably not a solution but

Probably not a solution but if you use "Multiple select" and check "Required" then the user has to make an active choice. The problem is of course that it is a multiple select (and takes a lot more room on the screen) which maybe isn't what you want.

Code Workaround

Another "dirty" solution may be this one.

Opne to edit "module/taxonomy.module", find the function "taxonomy_form" and REPLACE the line
$blank = 0;
with
$blank = '<'. t('select') .'>';

Next, open to edit "form.inc", find the function "_form_validate" and ADD after

      if ($elements['#required'] && empty($elements['#value']) && $elements['#value'] !== '0') {
        form_error($elements, t('%name field is required.', array('%name' => $elements['#title'])));
      }

this lines
      if ($elements['#type'] == 'select' && strpos($elements['#name'], 'taxonomy') !== false && $elements['#value'] == '0') {
        form_error($elements, t('%name field is required.', array('%name' => $elements['#title'])));
      }

I've tried this solution and it works. In my opinion it should be implemented in the official release (just after a complete review...) since a required select field that automatically gets the first element may cause confusion.

Hope this patch helps.

Regards.

Works great

...and I second that it should be default behaviour. Thanks for sorting this out!

Works great, but one more change

This works as advertised. Thanks! However, I don't want <select> to show up at the top of multi-selects, so I added one more thing to taxnomy.module.

In the function _taxonomy_term_select, change if($blank){ to if($blank AND !$multiple){

The following sentence is true.
The preceding sentence is false.
http://www.AskDerekScruggs.com

---
The following sentence is true.
The preceding sentence is false.
http://www.AskDerekScruggs.com

yet another workaround,

yet another workaround, though not elegant for many categories, would be to use the taxonomy_super_select module.

It's still a workaround. So, does this feature made his way to the core or any contributed module? or are there plans to do so?

Thanks in advance

It's not a good idea to

It's not a good idea to directly edit core code as this will cause a nightmare when update time comes.

Instead, use the hook_form_alter() function to make the necessary edits.
-----------------------------------------------------------------
Petafoo - We Love Animals

taxonomy cck module dont work

To prevent first taxonomy term work with taxonomy module

How can I do for work with taxonomy cck module?

Thanks

OM

Its Work

To prevent first taxonomy term work with taxonomy module

How can I do for work with taxonomy cck module?

Thanks

OM

taxonomy cck module

To prevent first taxonomy term work with taxonomy module

How can I do for work with taxonomy cck module?

Thanks

OM