0

I would think this would be a pretty common thing, but I have a job board that I am exposing for users to search on. They can search by city and/or state so I exposed some filters on my view for city and state. Now, of course, plain text boxes work fine out of the box if I want the user to freely type in a state, but I really want it to be a dropdown of states. I see that I can do a "Grouped Filter" which allows me to specify some key|value pairs, but do I REALLY need to enter in all 50 states manually? Is there a way programatically to have the dropdown be a list of states? I also noticed that even if I DO specify a few values to test with like AZ|AZ, CA|CA, NV|NV, when I go to use the exposed filters instead of the URL being something like this:

http://sandbox/job-board?city=&state=AZ

it actually uses:

http://sandbox/job-board?city=&state=1

where there is a "1" instead of "AZ". I would really like to be able to pass "AZ" not a 1 in the URL because if I have other apps posting to the job search page, I don't want to have to map ID's to actual state values, I just want to pass in the state code. What's the best way to go about this?

THANKS

3
  • Look at Address Field. It is a bit of an overkill but at least you will have options should you wish to expand on features later. Commented Sep 17, 2014 at 3:58
  • Does the Address module provide a list of states within exposed filters on views? FYI the view that I am creating is against a custom table. I had to make custom views support within my custom module. Would the Address module still help me in this instance?
    – Erich H.
    Commented Sep 17, 2014 at 15:28
  • Not out of the box. There is a patch for that. You need to look at comment #6 and #9. Three files need patching: addressfield.views.inc (line 90 and not 24 as they say), addressfield_views_handler_filter_administrative_area.inc, and addressfield.info. These patches can be copied and pasted. Commented Sep 18, 2014 at 4:33

2 Answers 2

1

Thanks guys. I don't think the Address Field module will work for me because the field I am exposing is not an address field. It is a field from a custom table, so I don't think the Address field module would pick that up. Anyway, I ended up just doing a form alter on the exposed filter form swapping out the text box with a drop down of states and it works great:

/*
*    hook_form_alter()
*/
function bullhorn_form_alter(&$form, $form_state, $form_id) {

  // Check form is exposed filter, object is a view and view name
  if ($form_id == 'views_exposed_form' && is_object($form_state['view']) && $form_state['view']->name=='job_board' ) {

    $states = array(
      ''=>'-ALL-',
      'AL'=>'Alabama',
      'AK'=>'Alaska',
      'AZ'=>'Arizona',
      'AR'=>'Arkansas',
      'CA'=>'California',
      'CO'=>'Colorado',
      'CT'=>'Connecticut',
      'DE'=>'Delaware',
      'DC'=>'District of Columbia',
      'FL'=>'Florida',
      'GA'=>'Georgia',
      'HI'=>'Hawaii',
      'ID'=>'Idaho',
      'IL'=>'Illinois',
      'IN'=>'Indiana',
      'IA'=>'Iowa',
      'KS'=>'Kansas',
      'KY'=>'Kentucky',
      'LA'=>'Louisiana',
      'ME'=>'Maine',
      'MD'=>'Maryland',
      'MA'=>'Massachusetts',
      'MI'=>'Michigan',
      'MN'=>'Minnesota',
      'MS'=>'Mississippi',
      'MO'=>'Missouri',
      'MT'=>'Montana',
      'NE'=>'Nebraska',
      'NV'=>'Nevada',
      'NH'=>'New Hampshire',
      'NJ'=>'New Jersey',
      'NM'=>'New Mexico',
      'NY'=>'New York',
      'NC'=>'North Carolina',
      'ND'=>'North Dakota',
      'OH'=>'Ohio',
      'OK'=>'Oklahoma',
      'OR'=>'Oregon',
      'PA'=>'Pennsylvania',
      'RI'=>'Rhode Island',
      'SC'=>'South Carolina',
      'SD'=>'South Dakota',
      'TN'=>'Tennessee',
      'TX'=>'Texas',
      'UT'=>'Utah',
      'VT'=>'Vermont',
      'VA'=>'Virginia',
      'WA'=>'Washington',
      'WV'=>'West Virginia',
      'WI'=>'Wisconsin',
      'WY'=>'Wyoming',    
    );

    $form['state'] = array(
      '#type' => 'select',
      '#options' => $states,
    );

  }
}
0
AL|AL
AK|AK
AS|AS
AZ|AZ
AR|AR
CA|CA
CO|CO
CT|CT
DE|DE
DC|DC
FM|FM
FL|FL
GA|GA
GU|GU
HI|HI
ID|ID
IL|IL
IN|IN
IA|IA
KS|KS
KY|KY
LA|LA
ME|ME
MH|MH
MD|MD
MA|MA
MI|MI
MN|MN
MS|MS
MO|MO
MT|MT
NE|NE
NV|NV
NH|NH
NJ|NJ
NM|NM
NY|NY
NC|NC
ND|ND
MP|MP
OH|OH
OK|OK
OR|OR
PW|PW
PA|PA
PR|PR
RI|RI
SC|SC
SD|SD
TN|TN
TX|TX
UT|UT
VT|VT
VI|VI
VA|VA
WA|WA
WV|WV
WI|WI
WY|WY
AE|AE
AA|AA
AP|AP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.