0

I have a ContentEntityForm with two select elements category and data_type. I want to load the options for the second select element when the first select element changes. I have tried to add the following in the buildForm function of the ContentEntity form but this does not seem to be adding the ajax trigger to the element. Any thoughts on what I am doing wrong?

$form['category']['#ajax'] = [
  'callback' => '_ajax_form_category_change',
  'event' => 'change',
  'wrapper' => 'ajax_id_data_type',
  'method' => 'replace',
];
$form['data_type']['#prefix'] = '<div id="ajax_id_data_type">';
$form['data_type']['#suffix'] = '</div>';

_ajax_form_category_change is a function in my module

function _ajax_form_category_change($form, $form_state) {
  return $form['data_type'];  
}

I have also added a form_alter in my module to change the options of $form['filing_type'].

3
  • 1
    Probably need to make it $form['category']['widget'][0]['value']['#ajax'] like in your other question.
    – No Sssweat
    Commented Nov 7, 2019 at 7:25
  • Than!ks @NoSssweat. Got it working!. Updating question with answer
    – anoopjohn
    Commented Nov 7, 2019 at 15:56
  • Please use answers (below) for answering the question
    – Clive
    Commented Nov 7, 2019 at 16:31

1 Answer 1

0

SOLUTION: Got this working with the following. You have to set the #ajax property on the widget

// Set the ajax function to run on the change of filing category.
$form['category']['widget']['#ajax'] = [
  'callback' => '_ajax_form_category_change',
  'event' => 'change',
  'wrapper' => 'ajax_id_data_type',
  'method' => 'replace',
];

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.