0

I am using Drupal 9. I know how to change single dropdown list using another one. Here is my scenario:

I have three dropdowns, in node form - company, protocol and investigator. What I like to do, when some one select a value in company dropdown, other two dropdown list which change based on selection.

I have written some code, I see the list is updating but its not refreshing.

function hook_form_alter() {
    ....

    case 'node_tsr_form':
    case 'node_tsr_edit_form':
      $form['field_tsr_pi']['#prefix'] = '<div id="my-module-principal-investigator-wrapper">';
      $form['field_tsr_pi']['#suffix'] = '</div>';
      $form['field_protocol']['#prefix'] = '<div id="my-module-tsr-protocol-wrapper">';
      $form['field_protocol']['#suffix'] = '</div>';

      $form['field_company']['widget']['#ajax'] = [
        'callback' => 'my_module_ajax_callback_tsr_pi_and_protocol',
        'event' => 'change',
        'progress' => [
          'type' => 'throbber',
          'message' => t('loading ...'),
        ],
      ];
      $company_val = $form_state->getValue('field_company');
      if (isset($company_val[0]['target_id'])) {
        $company_id = $company_val[0]['target_id'];
      }

      if (!empty($company_id)) {
        $form['field_protocol']['widget']['#options'] = my_module_get_protocols($company_id);
        $form['field_pi']['widget']['#options'] = my_module_get_investigator($company_id);
      }
      else {
        $form['field_protocol']['widget']['#options'] = ['_none' => '- None -'];
        $form['field_pi']['widget']['#options'] = ['_none' => '- None -'];
      }
    ...

/**
 * Called via Ajax callback.
 */
function my_module_ajax_callback_tsr_pi_and_protocol(array &$form, FormStateInterface $form_state) {

  $company_val = $form_state->getValue('field_company');

  if (isset($company_val[0]['target_id'])) {
    $company_id = $company_val[0]['target_id'];
  }
  if (!empty($company_id)) {
    $form['field_protocol']['widget']['#options'] = my_module_get_approved_aup_options($company_id);
    $form['field_tsr_pi']['widget']['#options'] = my_module_get_acitve_user_options($company_id);
  }
  else {
    $form['field_protocol']['widget']['#options'] = ['_none' => '- None -'];
    $form['field_tsr_pi']['widget']['#options'] = ['_none' => '- None -'];
  }

  $response = new AjaxResponse();
  $response->addCommand(new ReplaceCommand('#my-module-tsr-protocol-wrapper',$form['field_protocol']));
  $response->addCommand(new ReplaceCommand('#my-module-principal-investigator-wrapper', $form['field_tsr_pi']));

  return $response;
}

If anyone can help me out, will be really great!!!

1 Answer 1

0

This actually fix my issue:

function hook_form_alter() {
    ....

    case 'node_tsr_form':
    case 'node_tsr_edit_form':
      $form['field_tsr_pi']['#prefix'] = '<div id="my-module-principal-investigator-wrapper">';
      $form['field_tsr_pi']['#suffix'] = '</div>';
      $form['field_protocol']['#prefix'] = '<div id="my-module-tsr-protocol-wrapper">';
      $form['field_protocol']['#suffix'] = '</div>';

      $form['field_company']['widget']['#ajax'] = [
        'callback' => 'my_module_ajax_callback_tsr_pi_and_protocol',
        'event' => 'change',
        'method' => 'html',
        'progress' => [
          'type' => 'throbber',
          'message' => t('loading ...'),
        ],
      ];
      $form['field_protocol']['widget']['#validated'] = TRUE;
      $form['field_tsr_strain']['widget']['#validated'] = TRUE;
    ...

/**
 * Called via Ajax callback.
 */
function my_module_ajax_callback_tsr_pi_and_protocol(array &$form, FormStateInterface $form_state) {
  $company_val = $form_state->getValue('field_company');

  if (isset($company_val[0]['target_id'])) {
    $company_id = $company_val[0]['target_id'];
  }

  // Update Member(s) to notify options.
  $protocols = '';
  $pis = '';
  if (!empty($company_id)) {
    $protocol_data = vivarium_get_approved_aup_options($company_id);
    foreach ($protocol_data as $key => $value) {
      $protocols .= "<option value='".$key."'>".$value."</option>";
    }

    $pis_list = vivarium_get_acitve_user_options($company_id);
    foreach ($pis_list as $key => $value) {
      $pis .= "<option value='".$key."'>".$value."</option>";
    }
  }
  else {
    $protocols = "<option value='_none>- None -</option>";
    $pis = "<option value='_none>- None -</option>";
  }

  $response = new AjaxResponse();
  $response->addCommand(new HtmlCommand("#my-module-tsr-protocol-wrapper select", $protocols));
  $response->addCommand(new HtmlCommand("#my-module-principal-investigator-wrapper select", $pis));

  return $response;
}

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.