themetman's picture

I am trying to set the 'selected' tag in the array of months to the current month. I know I have to add the 'selected' tag to the array item, but where does it go? w3schools shows it as this:

<option value="fiat" selected>Fiat</option>

For the life of me I cannot see where to put 'selected'

$form['month'] = array(
      '#type' => 'select',
      '#title' => ('Select Month or The Full Year'),
      '#options' => array(
        '01' => t('January'),
        '02' => t('February'),
        '03' => t('March'),
        '04' => t('April'),
        '05' => t('May'),
        '06' => t('June'),
        '07' => t('July'),
        '08' => t('August'),
        '09' => t('September'),
        '10' => t('October'),
        '11' => t('November'),
        '12' => t('December'),
        '13' => t('The Full Year'),
      ),
    );

I have looked at the form functions in the API but cannot find anything to help.

Thanks in advance.

Accepted answer

Hey @themetman,

If you are actually looking for a way to set the default value, then simply do this:

$form['month'] = array(
  '#type' => 'select',
  '#title' => ('Select Month or The Full Year'),
  '#options' => array(
    '01' => t('January'),
    '02' => t('February'),
    '03' => t('March'),
    '04' => t('April'),
    '05' => t('May'),
    '06' => t('June'),
    '07' => t('July'),
    '08' => t('August'),
    '09' => t('September'),
    '10' => t('October'),
    '11' => t('November'),
    '12' => t('December'),
    '13' => t('The Full Year'),
  ),
  '#default_value' => '10',
);

If you are looking to add other attributes (such as classes/ID etc.) to '#type' => 'select' form elements, then read on...

Comments

I think you're looking for #attributes. So you'd have something like this (untested):

$form['month'] = array(
  '#type' => 'select',
  '#title' => ('Select Month or The Full Year'),
  '#options' => array(
    '01' => t('January'),
    '02' => t('February'),
    '03' => t('March'),
    '04' => t('April'),
    '05' => t('May'),
    '06' => t('June'),
    '07' => t('July'),
    '08' => t('August'),
    '09' => t('September'),
    '10' => t('October'),
    '11' => t('November'),
    '12' => t('December'),
    '13' => t('The Full Year'),
  ),
  '#attributes' => array(
    'selected' => 'selected',
  ),
);

For the reason why I set selected to equal selected, see 'Differences Between HTML and XHTML'.

Also, make sure that you're not actually wanting to use #default_value (I'm not sure how that works with selected...)

klonos's picture

Hey @themetman,

If you are actually looking for a way to set the default value, then simply do this:

$form['month'] = array(
  '#type' => 'select',
  '#title' => ('Select Month or The Full Year'),
  '#options' => array(
    '01' => t('January'),
    '02' => t('February'),
    '03' => t('March'),
    '04' => t('April'),
    '05' => t('May'),
    '06' => t('June'),
    '07' => t('July'),
    '08' => t('August'),
    '09' => t('September'),
    '10' => t('October'),
    '11' => t('November'),
    '12' => t('December'),
    '13' => t('The Full Year'),
  ),
  '#default_value' => '10',
);

If you are looking to add other attributes (such as classes/ID etc.) to '#type' => 'select' form elements, then read on...

klonos's picture

@BWPanda's answer above adds the selected="selected" attribute in the <select> tag instead of an individual <option> tag. What you'd want is something like this instead:

$form['month'] = array(
  '#type' => 'select',
  '#title' => ('Select Month or The Full Year'),
  '#options' => array(
    '01' => t('January'),
    '02' => t('February'),
    '03' => t('March'),
    '04' => t('April'),
    '05' => t('May'),
    '06' => t('June'),
    '07' => t('July'),
    '08' => t('August'),
    '09' => t('September'),
    '10' => t('October'),
    '11' => t('November'),
    '12' => t('December'),
    '13' => t('The Full Year'),
  ),
);
$form['month']['10']['#attributes']['selected'] = 'selected';

...or if you used actual numbers instead of strings for the options keys, then:

$form['month'][10]['#attributes']['selected'] = 'selected';

Unfortunately, the above only works for '#type' => 'radios' or '#type' => 'checkboxes' - it does not work for '#type' => 'select'. It is simply not supported by the Form API in Drupal/Backdrop. See this thread in stuckoverflow.com or this article for a way that involves some theming work.

PS: something that does work that way in the Form API, but is not (well-)documented, is adding #description to each individual option (again, for radios/checkboxes only). We have recently done that here in order to add better help text for the "Row width behavior" option in the settings of Flexible Layout Templates.

klonos's picture

...alternatively, there is the Form Options Attributes contrib module that allowed this in Drupal, but it has not been ported to Backdrop yet. Lucky that most D7 modules work almost as-is with Backdrop :) ...so until it is properly ported, here's what's worked for me:

1. Download the latest 7.x version of the module, and extract it into your modules directory.

2. Edit the form_options_attributes.info file, so that it looks like this:

name = Form Options Attributes
description = Allows form options for select elements to have individual attributes.

backdrop = 1.x
type = module

3. Enable the module.

4. In your form, do something like this:

$form['month'] = array(
  '#type' => 'select',
  '#title' => ('Select Month or The Full Year'),
  '#options' => array(
    '01' => t('January'),
    '02' => t('February'),
    '03' => t('March'),
    '04' => t('April'),
    '05' => t('May'),
    '06' => t('June'),
    '07' => t('July'),
    '08' => t('August'),
    '09' => t('September'),
    '10' => t('October'),
    '11' => t('November'),
    '12' => t('December'),
    '13' => t('The Full Year'),
  ),
  '#options_attributes' => array(
    '10' => array(
      'selected' => 'selected',
    ),
  ),
);

...or for simplicity (if not adding multiple other attributes to the options):

$form['month'] = array(
  '#type' => 'select',
  '#title' => ('Select Month or The Full Year'),
  '#options' => array(
    '01' => t('January'),
    '02' => t('February'),
    '03' => t('March'),
    '04' => t('April'),
    '05' => t('May'),
    '06' => t('June'),
    '07' => t('July'),
    '08' => t('August'),
    '09' => t('September'),
    '10' => t('October'),
    '11' => t('November'),
    '12' => t('December'),
    '13' => t('The Full Year'),
  ),
);
$form['month']['#options_attributes']['10']['selected'] = 'selected';
themetman's picture

Thank you @klonos for so many possibilities.

I have used the

'#default_value' => '10',

because I do not have to fiddle with an unnecessary module, and I can set this using the date('m') function.

Most grateful.

Regards