1

I'm using a custom module to change subject and recipient email. Actually my fields are a single text field for the subject, and a select field for recipients.

I would like to add a more options actually. My webform is on a public page opened to all. I add a new page with a link to the contact form and I want that link to provide some options.

So my link is like this: /node/5?option=1

This bring me to the aforementioned page and set the subject field according to my custom module like this:

function ca_formdispatch_form_alter(&$form, &$form_state, $form_id)
{
    $nid = arg(1);
    drupal_set_message(t(" webform_client_form: .", $form_id));

    //determination de l'ID du formulaire afin de l'alt�rer
    if ($form_id == 'webform_client_form_5' && $nid == 5)
    {
        $origine = $_GET['origine'];

        drupal_set_message(t("contenu d'origine : @origine.", array("@origine" => $origine)));

        //liste des email destintaire possible en fonction de l'origine du destinataire

        switch ($origine)
        {
            case '1':
                $email_dest='[email protected]';
                $sujet_mail="Message pour 1";
                break;
            case '2':
                $email_dest='[email protected]';
                $sujet_mail="Message pour 2";
                break;
        }

        //affectation de la valeur de $email_dest comme email destinataire du message, 
        $form['submitted']['destinataire']['#default_value']=$email_dest;
        drupal_set_message(t("contenu de email : @email.", array("@email" => $email_dest)));

        //affectation de la valeur de $sujet_mail comme sujet du message
        $form['submitted']['sujet']['#default_value']=$sujet_mail;
        drupal_set_message(t("contenu de sujet mail : @sujet_mail.", array("@sujet_mail" => $sujet_mail)));
    }
}

It works for the subject field but not for the recipient field. My recipient select field can take 4 options. I can set the select on an existing key but not add a new key not provided by the select by default.

I would to lock the key set by the module or hide the select field to don't allow is change by the user that did click on the link before.Is it possible? I know I can use hook_webform_select_options or hook_webform_select_options_alter but I don't know how with to make this code ... if someone could help me.

EDIT: i test this to get the "key" and pass it to the webform field but i think it doesn't work.

switch ($origine) {
  case 'charge-relation-entreprise'://valeur de $origine
  $clef="Chargé des Relations Entreprises";
  $email='[email protected]';
  break;....

$form['submitted']['choisissez_votre_destinataire']['#options'][$clef] = $clef;
$form['submitted']['choisissez_votre_destinataire']['#value']=$clef;
$form['submitted']['choisissez_votre_destinataire']['#key']=$clef;

but if this display the right text in the select,it doesn't send mail...

EDIT2 : i get the right key label displayed if i set a default value key in the webform select list setting like here https://computing.artsci.wustl.edu/help/drupal-webforms-advanced

But my select doesn't work anymore if i go to the webform page directly,whithout instructions in the url.Like with the edit1

2 Answers 2

3

For select fields to accept a default value, this value must be part of the #options list.

So you have to add the option before you can set the default value (important is the array key):

$form['submitted']['destinataire']['#options'][$email_dest] = $email_dest;
$form['submitted']['destinataire']['#default_value'] = $email_dest;

Then to hide the form element you can do:

$form['submitted']['destinataire']['#access'] = FALSE;

Or to keep it visible but disabled:

$form['submitted']['destinataire']['#attributes']['disabled'] = 'disabled';

But in any case, the submitted value for select elements must be part of the options list, otherwise it won't be accepted as a valid submission value. This is one of the many ways that Drupal tries to prevent malicious form manipulation.

For the difference between #value and #default_value: if #value is set, then it doesn't matter if a user chooses a different value for the form element. The finally submitted value that arrives in the submit handler will be the value set with #value. It's hardwired so to speak. #default_value on the other hand is more like a suggestion, it is changeable by the user and whatever is chosen will also arrive at the submit handler.

1
  • i rework on it and i get a problem because the recipient (select list) field display the email instead of the text of the choice of the list like "choix 1"...how can i ask the form to display the corresponding key ?
    – user8753
    Sep 2, 2014 at 10:01
0

To set a default value on a select list, you need to use “#value” and not “#default_value”. So in your case it will be:

$form['submitted']['destinataire']['#value']=$email_dest;

Hope it works. Good luck!

1
  • @Priti This doesn't solve the OP's problem, see my answer for an explanation.
    – berliner
    Feb 6, 2014 at 2:52

Your Answer

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