Form submission with AJAX to override a block content So, I have a block that I render with the following code.

public function build() {
  $filterForm = \Drupal::formBuilder()-&gt;getForm('Drupal\smeno\Form\FilterForm');

  $renderArray['filterform'] = $filterForm;
  $renderArray['McRefund_data'] = $this-&gt;getMcRefund();

  return [
    '#theme' =&gt; 'Remboursement-sur-compte',
    '#McRefund_build_data' =&gt; $renderArray,
  ];
}



  


As you can see within this block, I render a form and data (McRefund_build_data) which I put in a table(couple of Divs). The purpose of the form is to filter the data and put the response back in the block so that the block get rendered with the new filtered data. For this process I want to use AJAX to avoid the page from being reloaded.

I tried adding this on buildForm().

$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
  '#type' =&gt; 'submit',
  '#value' =&gt; $this-&gt;t('OK'),
  '#button_type' =&gt; 'primary',
  '#prefix' =&gt; '&lt;div class="tc btn_pay relative"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;&lt;/div&gt;',
  '#attributes' =&gt; array(
    'class' =&gt; array('proximabold', 'tc', 'transition', 'no-underline', 'ph3', 'pv2', 'mb2', 'dib', 'white'),
  ),
  '#ajax' =&gt; [
    // 'callback' =&gt; 'Drupal\smeno\Controller\RemboursementsController::filterRefunds',
    'url' =&gt; Url::fromUserInput('/filterremboursement'),
    'event' =&gt; 'click',
    // 'wrapper' =&gt; 'edit-output',
    'progress' =&gt; [
      'type' =&gt; 'throbber',
      'message' =&gt; t('Verifying entry...'),
    ],
  ],
);


I added a route and a controller to handle the request and the response.

Route

filterRemboursement:
path: '/filterremboursement'
defaults:
  _controller: 'Drupal\smeno\Controller\RemboursementsController::filterRefunds'
  _title: 'filterRemboursement'
methods:  [POST]
requirements:
  _access: 'TRUE'


Controller

function filterRefunds() {
  ddl('HELLO WORLD'); //devel module debug messages (usefull for ajax requests)
}


This isn't working, though.
Remove the data table from the block

public function build() {
    $filterForm = \Drupal::formBuilder()-&gt;getForm('Drupal\smeno\Form\FilterForm');

    $renderArray['filterform'] = $filterForm;

    return [
       '#theme' =&gt; 'Remboursement-sur-compte',
       '#McRefund_build_data' =&gt; $renderArray,
    ];
}


and add it to the form (placed in a wrapper):

  $form['McRefund_data'] = [
    '#type' =&gt; 'container',
    '#attributes' =&gt; ['id' =&gt; 'table-wrapper'],
    'table' =&gt; $this-&gt;getMcRefund(),
  ];


Attach this ajax callback to a form element

          '#ajax' =&gt; [
            'event' =&gt; 'click',
            'callback' =&gt; '::updateTable',
            'wrapper' =&gt; 'table-wrapper',
            'progress' =&gt; [
              'type' =&gt; 'throbber',
              'message' =&gt; t('Verifying entry...'),
            ],
          ],


and replace the table in the ajax callback:

  public function updateTable(array $form, FormStateInterface $form_state) {
    return $form['McRefund_data'];
  }