I was just working on a Drupal form, and I wanted to include a list of states (the United States) in a dropdown field (an HTML select/options field). Knowing that I'll need this list of states for other Drupal applications, I decided to put the "states" code in a separate file.
To that end I put this list of states in a file named states.inc, in the same directory with the rest of my Drupal module code:
<?php $state_options = array( '' => t('--- Select ---'), 'AL' => t('Alabama'), 'AK' => t('Alaska'), 'AZ' => t('Arizona'), 'AR' => t('Arkansas'), 'CA' => t('California'), 'CO' => t('Colorado'), 'CT' => t('Connecticut'), 'DE' => t('Delaware'), 'DC' => t('District of Columbia'), 'FL' => t('Florida'), 'GA' => t('Georgia'), 'HI' => t('Hawaii'), 'ID' => t('Idaho'), 'IL' => t('Illinois'), 'IN' => t('Indiana'), 'IA' => t('Iowa'), 'KS' => t('Kansas'), 'KY' => t('Kentucky'), 'LA' => t('Louisiana'), 'ME' => t('Maine'), 'MD' => t('Maryland'), 'MA' => t('Massachusetts'), 'MI' => t('Michigan'), 'MN' => t('Minnesota'), 'MS' => t('Mississippi'), 'MO' => t('Missouri'), 'MY' => t('Montana'), 'NE' => t('Nebraska'), 'NV' => t('Nevada'), 'NH' => t('New Hampshire'), 'NJ' => t('New Jersey'), 'NM' => t('New Mexico'), 'NY' => t('New York'), 'NC' => t('North Carolina'), 'ND' => t('North Dakota'), 'OH' => t('Ohio'), 'OK' => t('Oklahoma'), 'OR' => t('Oregon'), 'PA' => t('Pennsylvania'), 'RI' => t('Rhode Island'), 'SC' => t('South Carolina'), 'SD' => t('South Dakota'), 'TN' => t('Tennessee'), 'TX' => t('Texas'), 'UT' => t('Utah'), 'VT' => t('Vermont'), 'VA' => t('Virginia'), 'WA' => t('Washington'), 'WV' => t('West Virginia'), 'WI' => t('Wisconsin'), 'WY' => t('Wyoming'), );
I then put the following code into the file where I defined my Drupal form:
require_once 'states.inc'; $form['state'] = array( '#title' => t('State'), '#description' => "The state where your business is located.", '#type' => 'select', '#options' => $state_options, '#default_value' => $state, '#required' => TRUE, );
I need to do something different about handling the fact that this field is required, but hopefully everything else here is very straightforward.
This article was inspired by a list of states I found on this ignored by dinosaurs website, and on a related note, I've previously shared a more general list of states for an HTML select/option field (drop-down list).