Score:0

Node form ajax dependent dropdown fields issue

cn flag

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!!!

Score:0
cn flag

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;
}

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.