0

I have defined couple of fields inside my custom entity basefielddefinitions() like so...
$fields['product_line'] = BaseFieldDefinition::create('entity_reference')...

$fields['product'] = BaseFieldDefinition::create('entity_reference')....

Then in entity form class (ProductLineForm.php), I try to attach an ajax callback to my product_line select list such that it populates the product select list, like this

    $form['product_line']['#group'] = 'details';
    $form['product_line']['#ajax'] = [
      'callback' => '::populateProducts',
      'event' => 'change',
      'wrapper' => 'edit-products'
    ];

Then my product select list looks like this...

    $form['product']['#group'] = 'details';
    $form['product']['#attributes'] = [
      'id' => 'edit-products',
    ];

and then my populateProducts() function...

  public function populateProducts(array $form, FormStateInterface $form_state) {
    $form['product']['#options'] = ['1' => 'One', '2' => 'Two'];
    $form_state->setRebuild(TRUE);
    $response = new AjaxResponse();
    $option = "<option value='45'>Just testing</option>";
    $response->addCommand(
      new \Drupal\Core\Ajax\ReplaceCommand(
        '#edit-products',
        ($form['product'])
      )
    );
    
    return $response;
  } 

The problem is that nothing whatsoever happens, no signs of any ajax call, and the product select list stays empty as it always been.

(1)Is the the entityform class the right place for me to attach an #ajax call to a field defined in a custom entity? Or do I need a hook? Meanwhile I've done a lot of other modifications on the custom fields here in this entity form edit class (like grouping into fieldsets, vertical tabs and even modifying weight (eg $form['product_line']['#weight'] = 3 - and they all work, why not $form['product_line']['#ajax']....?

(2) Is there a way of attaching the ajax callback directly inside my custom entity's basefielddefinitions where I define the fields, I would prefer that...

Really stuck on this one, folks, please could someone help me? Appreciate all your input, Thank you all.

10
  • Which Form API element types have you used for product_line and product? '#type' => 'select'?
    – baikho
    Jan 17 at 0:25
  • 3
    Does this answer your question? AJAX dependent select
    – baikho
    Jan 17 at 0:26
  • Thanks baikho, yes both product_line and product are select.
    – ilechcod
    Jan 17 at 4:28
  • A couple of clarifications from the Ajax dependent select example you sent above - where should I run this code? Is it ok to run it in my Product Line Form class? Or should this be done in a form alter hook? Also is the Ajax callback function required to be in a js file? Or it should be in the class where I modify the form?
    – ilechcod
    Jan 17 at 4:32
  • Right now everything is inside my \module\src\Form\ProductLine Form which is the add_form for my ProductLine entity...
    – ilechcod
    Jan 17 at 4:44
0

just got this sorted out! Decided to drill down to the array structure of the form, and found out that the #ajax element had to be attached to the widget array of the field, like this... $form['product_line']['widget']['#ajax'] = [...] Now it works. Another reason I keep crying "better documentation for Drupal"!!! How in the world was anybody supposed to know that for existing form fields, this was the format for adding Ajax?!!! Oh well, thanks community for pointing in the right direction. Anybody knows the right place to insert this in the existing documentation? Cheers all.

1
  • I just discovered now that the Ajax command only runs once. When the select changes, the progress throbber still shows but content of the second select remains the same. Any solutions pls anyone?
    – ilechcod
    Jan 18 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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