0
public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);
    $selected = 'value of a field';
    $form['node'] = [
      '#type' => 'entity_autocomplete',
      '#title' => $this->t('My content type'),
      '#target_type' => 'node',
      '#selection_settings' => [
        'target_bundles' => ['bundle'],
      ],
      '#ajax' => [
        'callback' => [$this, 'my_ajax_callback'],
        'wrapper' => 'my-specializations',
      ],
    ];

    $form['first_select'] = [
      '#type' => 'select',
      '#options' => [0 => 'x', 1 => 'y'],
      '#title' => 'doctor',
      '#ajax' => [
        'callback' => [$this, 'my_ajax_callback'],
        'wrapper' => 'my-specializations',
      ],
    ];

    $form['specializations'] = [
      '#type' => 'select',
      '#title' => $this->t('Specializations'),
      '#options' => $this->specialization($selected),
      '#prefix' => '<div id="my-specializations">',
      '#suffix' => '</div>',
    ];

    return $form;
  }

 private function specialization($selected) {
    if (!empty($selected)) {
      return ['1' => 'a', '2' => 'b'];
    }
    else {
      return [];
    }
  }

  public function my_ajax_callback(array &$form, FormStateInterface $form_state) {
    return $form['specializations'];
  }

The ajax call back function is not working on auto complete field. How can I make it working on the auto complete field?

And on the first-select it is working but I am getting an error:

TypeError: Argument 1 passed to Drupal\Core\Render\MainContent\AjaxRenderer::renderResponse() must be of the type array, null given, called in /var/www/web/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php on line 89 in Drupal\Core\Render\MainContent\AjaxRenderer->renderResponse() (line 53 of /var/www/web/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php)

Can you someone tells me, what I am doing wrong?

2
  • Can you post the code that declares the doctor_ajax_callback function? I don't see public function doctor_ajax_callback(...
    – No Sssweat
    Commented Jun 19, 2020 at 10:49
  • That function is actually my_ajax_callback
    – Ahmad
    Commented Jun 22, 2020 at 4:34

1 Answer 1

0

To answer the title's question

  '#ajax' => [
    'callback' => '::my_ajax_callback',
    'wrapper' => 'my-specializations',
    'event' => 'change', // run ajax on change
    'keypress' => TRUE, // if set to TRUE, then the element's #ajax['event'] will be triggered if the ENTER key is pressed while the element has focus.
  ],
2
  • I am getting this error now. Symfony\Component\HttpKernel\Exception\HttpException: The specified #ajax callback is empty or not callable
    – Ahmad
    Commented Jun 22, 2020 at 5:07
  • @Ahmad turns out when using BlockBase, it requires a different approach. See Ajax Calls on BlockBase extensions don't work. You'll have to do a solution like in #46
    – No Sssweat
    Commented Jun 22, 2020 at 6:14

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.