0

I'm creating a webform to build up a subscription form, on Drupal 7.65

Goal

What I need to do is: to select a role from a list, and automatically to display the associated name of that role, in a text field. As I said, the name should be displayed into a not modifiable text field just below it.

Suppose valid, the following list (key => value)

Field: Department

  • business_manager|Business Manager
  • hr_consultant|Human Resources
  • training_developer|Training Developer

and from the time going on, the associated names, are respectively

Options can appear into text field hr_business_partner

  • Steve Abc
  • Gertrude Def
  • Sven Hgj Klm

Thus when the trainee selects "Human Resources", the name of "Gertrude Def" should appear into the text field below the select one. I've attached a mokup to better understand what I do need.

mokup webform

IMPORTANT I can't put the names into the list as value, because the association can change but old records should keep the previously registered associations

Tormy Van Cool
  • 658
  • 10
  • 29
  • check conditionnal field module https://www.drupal.org/project/conditional_fields – Fky Apr 10 '19 at 09:10
  • use attributes like "data-person" for each option and select the attribute value on change of department field in javascript / jquery. refer https://stackoverflow.com/questions/8345666/on-select-change-get-data-attribute-value for example – Viswanath Polaki Apr 25 '19 at 09:20
  • Adding to that you can use https://www.drupal.org/project/form_options_attributes module for generating your form. – Viswanath Polaki Apr 25 '19 at 09:22

1 Answers1

1

You can use hook_form_alter() and add a new select field with the paired key value list you need to the webform. And then use javascript to update which field value gets shown in the HR Business Partner field on change, which by the way would also need to be added via your hook_form_alter. You could use a taxonomy to maintain a list of Departments/Business partners which would populate your department and business partners.

Write some javascript to dynamically update your original fields not added through the form_alter, on change. I would suggest making two textfields in your webform components which will hold the value from your form alter added fields. So that these values selected by the user gets saved in your form.

function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "webform_client_form_####"){
        $form['#attached']['js'] = array(drupal_get_path('module','MODULENAME') . '/js/webform.js');
        $form['hr_dept'] = array(
            "#type" => "select",
            "#options" => array("business_manager"=>"Business Manager", "hr_consultant"=>"Human Resources"),
        );
        $partners = taxonomy_get_tree(#); //the VID of the taxonomy

        $list = array("0"=>"None"); //first option

        foreach($partners as $tid => $partner){
            $list[$partner->tid] = $partner->name;
        }

        $form['hr_partner'] = array(
            '#type' => 'select',
            '#options' => $list,
        );
}
    }

In your javascript file, /js/webform.js you can include all your logic to check for which value is selected on the Department field and then display the correct value in the Partners fields. At the same time, updating the original fields you've added as textfields in the webform components UI.

jeev
  • 26
  • 2