-1

I have a custom form for emails with the following form element.

$form['type'] = array(
  '#title' => t('TYPE DE SOIN SOUHAITÉ'),
  '#type' => 'select',
  '#options' => array("Choisissez le type de soin ou offrez un cadeau",
      "Entrees Spa","Soins du visage","Massage", "Soins Duo","Soins du corps","Offres speciales","Invitation cadeau"),
);

When I try to get the value submitted from the users, I get the array index of the chosen option, not the option string. For example, if the selected value is Soins Duo, I get 4 as submitted value?

  • How are you sending the email ? please share code, is this Drupal 8 ? – GiorgosK Mar 13 at 11:40
  • This Drupal 7, i use site-wide contact form, the default form Drupal 7, and use smtp authentification support module for send the email – FF08 Mar 14 at 2:58
0

That happens because the array you are passing for #options is the following one.

array(
  0 => "Choisissez le type de soin ou offrez un cadeau",
  1 => "Entrees Spa",
  2 => "Soins du visage",
  3 => "Massage",
  4 => "Soins Duo",
  5 => "Soins du corps",
  6 => "Offres speciales",
  7 => "Invitation cadeau"
)

Drupal uses the array index as selected value, and the array value as string to show for the form element. 4 is the array index for "Soins Duo".

If you want the value to be a string too, you need to use the following code, for that form element.

$form['type'] = array(
  '#title' => 'TYPE DE SOIN SOUHAITÉ',
  '#type' => 'select',
  '#options' => drupal_map_assoc(array("Choisissez le type de soin ou offrez un cadeau",
      "Entrees Spa","Soins du visage","Massage", "Soins Duo","Soins du corps","Offres speciales","Invitation cadeau")),
);

As side note, the literal string passed to t() needs to be in English. If you are using another language, it is better not to use t() at all, since it only translates from English to the language set for the site. Better, you use t() with an English string, and use the Drupal translation system to get the site in the language you prefer.

  • wow thanks, and on "t()" will be translate if i use english? i just think it hardcode and can't be translate, but i will try, thank you so much for the answer – FF08 Mar 15 at 7:47
  • Yes, the first argument needs to contain an English message because t() is not for translating from any language to any language, but from English to any language. If you don't want to translate the message, just use the message without passing it to t(). – kiamlaluno Mar 15 at 8:38
0

If it is a Drupal 7 field, maybe you can try this:

$field = field_info_field('field_replace'); //replace for your field_name
$label = $field['settings']['allowed_values'][$key]; //$key is the value of your "type"
  • It's a form element, not an entity field. – kiamlaluno Mar 14 at 12:30

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.