$form['stop_date'] = array(
  '#type' => 'date',
  '#title' => t('Stop Date'),
  '#default_value' => date("Y-m-d"), // default to today 
  '#id' => 'stop_date', 
  '#attributes' => array('class' => array('dateSel-Fld')), 
  '#required' => true,

When using the #attribute property the form is not being rendered successfully.

  • I think I had something like that before. Alternatively you could try to add your class with a wrapper via '#prefix' => '<div class="foo">', '#suffix' => '</div>', or nest your form element one level deeper with some '#type' => 'container', as parent. – leymannx Oct 31 '18 at 21:38
  • Make sure to use the new array formatting when dealing with Drupal 8 too, so [] rather than array(). – kiwimind Aug 23 at 11:35


Here's the references:


And a usage example:


  • 1
    Japo, thanks for the response. However, my issue resides in Drupal 8. I'm beginning to suspect there is a bug in core. – Mike Mar 14 '16 at 14:02

In Drupal 8, you can try this if you want to add certain attributes to the views filter (in my case, it is a select dropdown)

Add this to your mymodule.theme file

    function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      $form['field_month_target_id']['#attributes']['class'][] = 'GAEventTrack';
      $form['field_month_target_id']['#attributes']['eventcategory'] = 'Category';
      $form['field_month_target_id']['#attributes']['eventaction'] = 'Action';
      $form['field_month_target_id']['#attributes']['eventlabel'] = 'Label';

Of course you can add a checking to fit your needs =)

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.