2

The issue seems simple, but I can't get any documentation for it.

I have a content type with two drop-down fields: field_company which lists taxonomy terms and field_protocol which is referencing a view of entity reference type.

I want to pass the field_company value to field_protocol as view argument, so that when the company is selected, the protocol list is automatically filtered via AJAX. I did add an AJAX callback to refresh the node form with field_company field. I don't knoww how to pass the field value and if this value will filter the view by contextual filter.

screenshot

1 Answer 1

1

The way I thought to update the dropdown of the node field will not work. This should be done in this way:

https://codimth.com/blog/web/drupal/dependent-select-dropdowns-using-ajax-node-addedit-form-drupal-8-9

<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Implements hook_form_alter().
 */
function MODULENAME_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
  if ($form_id == 'node_article_form' || $form_id == 'node_article_edit_form') {

    //add wrapper to select 2
    $form['field_select2']['#prefix'] = '<div id="select2-wrapper">';
    $form['field_select2']['#suffix'] = '</div>';

    // add ajax to select 1
    $form['field_select1']['widget']['#ajax'] = [
      'callback' => 'callback_field_select1_trigger',
      'wrapper' => 'select2-wrapper',
      'event' => 'change',
      'progress' => [
        'type' => 'throbber',
        'message' => t('Fetching content...'),
      ],
    ];

    //get select 2 options in edit form
    $field_select1_value = $form_state->getValue('field_select1');
    if ($form_id == 'node_article_edit_form' && !$field_select1_value) {
      $field_select1_edit_value = isset($form['field_select1']['widget']['#default_value'][0]) ? $form['field_select1']['widget']['#default_value'][0] : null;
      $form['field_select2']['widget']['#options'] = getSelect2Options($field_select1_edit_value);
    }

  }
}

/**
 * @param array $form
 * @param $form_state
 * @return mixed
 */
function callback_field_select1_trigger(array $form, $form_state)
{
  $field_select1_value = $form_state->getValue('field_select1');
  if (!empty($field_select1_value)) {
    $select1_value = $field_select1_value[0]['target_id'];
    $form['field_select2']['widget']['#options'] = getSelect2Options($select1_value);
  } else {
    $form['field_select2']['widget']['#options'] = getAllSelect2Options();
  }
  return $form['field_select2'];
}


/**
 * @param $select1_value
 * @return array
 */
function getSelect2Options($select1_value)
{
  $query = \Drupal::entityQuery('taxonomy_term');
  $query->condition('vid', "select2_term");
  $query->condition('field_select1.0.target_id', $select1_value);
  $tids = $query->execute();
  $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
  $options = [];
  $options['_none'] = t('- Any -');
  foreach ($terms as $key => $term) {
    $options[$key] = $term->name->value;
  }
  return $options;
}


/**
 * @return array
 */
function getAllSelect2Options()
{
  $query = \Drupal::entityQuery('taxonomy_term');
  $query->condition('vid', "select2_term");
  $tids = $query->execute();
  $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
  $options = [];
  $options['_none'] = t('- Any -');
  foreach ($terms as $key => $term) {
    $options[$key] = $term->name->value;
  }
  return $options;
}
// example how to get options if select 1 is multiple field
$selectedItems = [];
$default_values = $form['field_select1']['widget']['#default_value'];
if ($default_values != NULL) {
  foreach ($default_values as $key => $value) {
    if ($value != 0) {
      $selectedItems[$value] = $value;
    }
  }
}
if (count($tab) > 0) {
  $form['field_select2']['widget']['#options'] = getSelect2Options($selectedItems);
} else {
  $form['field_select2']['widget']['#options'] = getAllSelect2Options();
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.