1

I am working on a form made mostly of multi-select fields, which are optional for the user to choose.

The multi-select fields work fine in general; however I really need to hide the -None- option in the multi-select fields which are not required for user to select. I can override it and change the text but thats not the point, since user can still select the -None- value in the process of selection, even tho it wont show after node is saved.

It just looks a bit confusing, even though it works as it should; users can select both the -None- option along with other options available and that makes the whole form different from what I want.

Here is an image of an example field showing what I mean.

screenshot

screenshot

I tried searching through the CCK module, but I couldn't really find anything.

  • 1
    @rishariss What happens in your scenario if someone actually selects an item from the drop down then wants to back out of it? Forcing them to reload the page and start again is far worse UX than what you're explaining – Clive Jul 10 '13 at 14:56
  • Could you answer question from my first comment: "is it Form API form, Webform, Quick Form?.." Without that data it's simply impossible to help you. And yea, we know you don't want him to select none and something. All we wanted to know is how you plan to allow them unselect all options. Checkboxes in dropdown are nice way to do so :) – Mołot Jul 10 '13 at 15:40
  • 1
    Im using standard CCK method under manage fields of content type to add fields. To be more exact it's a term refference field with select list widget and multiple values avaliable. Then to change the look and usability of it, i used the jquery ui multi-select widget. I hope i answered your question. – risha riss Jul 10 '13 at 15:45
1

To get rid of "- None -" from selected field of a selected form, use:

function MYMODULE_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
    unset($form['field_name'][LANGUAGE_NONE]['#options']['_none']);
}

Of course for many fields it's inconvenient, but if they all share common parent (fieldset), you can iterate using foreach like:

function MYMODULE_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
    foreach( element_children($form['fieldset_name']) as $key => $child) {
       unset($form['fieldset_name'][$key][LANGUAGE_NONE]['#options']['_none']);
    }
}

This is not a ready code, just a sketch. Hints to make it work:

  1. Unset elements of original $form, not $child generated by foreach.
  2. Test if what you want to unset is there at all.
  3. dpr() or var_dump() to see what's the exact structure you are working with.

You will need to wrap these into a Drupal custom module. Read Module developer's guide, especially Creating Drupal 7 modules, and hook_form_alter() API reference if you need more backgroung.

Inspired by this answer.

  • Thank you for your answer Mołot! Im really new to php and that code is not fully understandable to me yet, but i will certainly do some research and try to make it work. – risha riss Jul 11 '13 at 7:42
  • @rishariss updated answer with some links that will give you basics needed to use that code in Drupal. PHP coding is something I hardly can't help you with, you need to know it to modify Drupal's internals. – Mołot Jul 11 '13 at 7:58
  • Big thanks! I resolved my issue by creating new module and using the code which you suggested earlier. I used a sparete unset for each field individually, even tho im pretty sure there is a better way to do it, still, it works!. function MYMODULE_form_FORM_ID_alter(&$form, &$form_state, $form_id) { unset($form['field_name'][LANGUAGE_NONE]['#options']['_none']); } – risha riss Jul 11 '13 at 8:28
  • @rishariss if works, consider accepting my answer ;) You should see tick under vote arrows. If you will, people looking for this issue will know your question was answered in a helpful way, so they will read it and get what they need. – Mołot Jul 11 '13 at 8:32
  • you missunderstood me, ofcourse im accepting it, i just meant that i could probablly target all fields in one line instead of targeting em in each individual line separately if i knew what i was doing :) Once again, big thanks! best regards – risha riss Jul 11 '13 at 8:36

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.