drupalentitydrupal-moduleshook-form-alter

Unable to add --select--value at the top of drop down drupal entity registration module required field


I am using registration entity module in my drupal website.And i have dropdowns of home phone and mobile no on that form.Home Phone no is required and it doesn't show --select-- as value at the top of dropdown.While Mobile no is not required and hence it show --select-- at the top.

Now i want to alter that form and add the --select-- option value at the top of home phone no.and i am writing this code in my custom module.

function custom_va_form_alter(&$form, $form_state, $form_id) {


if($form_id == 'commerce_checkout_form_registration'){
   if($form['registration_information']['prod-va_application']['prod-va_application-reg-0']['field_phone']['und'][0]['field_home_phone']['und']['#required'] == 1){
   dsm($form);

   $form['registration_information']['prod-va_application']['prod-va_application-reg-0']['field_phone']['und'][0]['field_home_phone']['und'][0]['country_codes']['#options']= '--select--';
   return $form;
   }
 }
}

But i am unable to add --select-- at the top of dropdown.What i am doing wrong?

is there any way other to alter the form in my custom module


Solution

  • #options expect an array, so if you want to add at begining use array_unshift function :

    array_unshift($form['registration_information']['prod-va_application']['prod-va_application-reg-0']['field_phone']['und'][0]['field_home_phone']['und'][0]['country_codes']['#options'], '--select--');
    

    array_unshift documentation : http://php.net/manual/en/function.array-unshift.php