4

I have several list fields, one of which is a list (text) field. The default value for the form, where no value is stored if nothing is selected, is "-None-". Is there some way to alter this to say "-Select-" instead? The reason is that in the context of the field, the option of "-None-" doesn't make any sense. In this particular field, one of the actual selections being stored in the database is "None". Here's images of a few fields where -None- doesn't make any sense.

Dropdown Menuenter image description hereenter image description here

So, is there any way to change these aside from making them required fields?

2

Have you tried to override the default translation string? It can either be done by editing settings.php or with the module "String Overrides"

Here are some instructions for settings.php here, or download String Overrides

  • However, I'm not sure if - None - is used somewhere else or if you would like the - None - text to be shown on other forms – Fredrik Jul 10 '12 at 5:45
10

Implement hook_form_alter() or hook_form_FORM_ID_alter() and use the following code:

$form['field_name'][LANGUAGE_NONE]['#options']['_none'] = t('Select');

example:

function MODULE_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
    $form['field_name'][LANGUAGE_NONE]['#options']['_none'] = t('Select');
}

The t() function is for translation. It may not be required, but it is good practice to use instead of hard-coded strings.

To remove - None - as a choice all together, you could unset the key using hook_form_alter as well.

example:

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

If you're not using it already, be sure to install the Devel module so you have access to the dpm() and dd() functions. They are incredibly valuable when playing with Drupal forms.

  • That would remove it from the select list completely though, wouldn't it? So, if the user didn't select an option it would submit the first item in the list by default? – Mrweiner Jul 10 '12 at 5:01
  • Dont unset it , just change the value of None to select in form alter. – GoodSp33d Jul 10 '12 at 5:14
  • Sorry to get specific, but what exactly would be the command for that? I'm still trying to get my head around all the aspects of hook_form_alter. – Mrweiner Jul 10 '12 at 8:40
  • Updated my answer to better address the question. :) – Citricguy Jul 10 '12 at 14:13
  • I ended up overriding the string in the settings.php file, but I feel like this will end up being useful at some point. Thanks for the suggestion and the edit. – Mrweiner Jul 11 '12 at 1:15
4

You can override the theme_options_none theme function. Follow the example from the Drupal API. If you want to replace "- None -" with "- Select -" everywhere, this theme implementation should do the trick:

/**
 * Returns HTML for the label for the empty value for options that are not required.
 *
 * The default theme will display N/A for a radio list and '- None -' for a select.
 *
 * @param $variables
 *   An associative array containing:
 *   - instance: An array representing the widget requesting the options.
 *
 * @ingroup themeable
 */
function MYTHEMENAME_options_none($variables) {
  return t('- Select -');
}

Place this function in your theme's template.php.

  • Useful to know, thanks. Btw missing ");" at the end of the return line. – Ben Feb 26 '14 at 16:01
2

If you already have some jquery/javascript included in your system, eg a custom theme or module, you can add something like:

$('select option:contains("- None -")').text("- Select -");

and it will change it up for you. You can cascade more in there if you want to be specific to just a certain select/option as the above will change all of them.

Otherwise, on the php side of things, the - None - is passed through t('- None -') so you could use one of the many localize or customtext modules to change the text, but this would be systemwide as well.

  • Is this dependent on the end-user having javascript enabled, or just dependent on the backend using javascript? – Mrweiner Jul 10 '12 at 4:59
  • no, this is in the client/browser so it would require users to have enabled javascript. – Jimajamma Jul 10 '12 at 13:32
  • oh, and String Overrides mentioned by @Fredrik above is what I was talking about re: customtext modules. It'll intercept t('- None -') and change every instance of it systemwide for you. – Jimajamma Jul 10 '12 at 13:39
  • Sometimes, forms API just takes too much time. jQuery comes to the rescure. On my case, using jQuery was the only solution that worked. – Druvision Oct 14 '13 at 22:39
1

In Drupal 8, this worked for me:

  $form['field_name']['widget']['#options']['_none'] = t('- Select -');

Re: @Citricguy's answer, as I understand it since then:

  • LANGUAGE_NONE was changed to LANGUAGE_NOT_SPECIFIED
  • LANGUAGE_NOT_APPLICABLE and LANGUAGE_MULTIPLE were also added
  • they were all then renamed from LANGUAGE_* to LANGCODE_*

This means you do something like:

use Drupal\Core\Language\Language;
$form['field_name'][LANGCODE_NOT_SPECIFIED]['#options']['_none'] = ...

…however I couldn't get that to work with hook_form_alter(): the actual values of those constants are three letter alphanumeric codes which don't appear to exist in the form field array.

So I reverted to the line at the top of my answer. If anyone can clear that up...?

0

Implement hook_form_alter() or hook_form_FORM_ID_alter() and identify the dropdown field and add this:

e.g.

$form['field_name'][$form['field_name']['#language']]['#options']['#empty_option'] = t('- Select -');

The key thing is to set the #empty_option attribute on your select list.

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.