I
I
Igor2018-09-12 08:48:07
Drupal
Igor, 2018-09-12 08:48:07

How to dynamically update a block in Drupal 8?

Is it possible to update a single block without updating the page as a whole? For example, a block outputs a table depending on the date (for example, parameters dBegin and dEnd), the user changes the parameter, only one block is updated depending on the new set of parameters?
The block is now formed by the build() function, its caching is disabled, i.e. it goes like this:
return array(
'#type' => 'markup',
'#markup' => \Drupal\Core\Render\Markup::create($nodes),
'#cache' => array(
'max-age ' => 0
),
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
afi13, 2018-09-12
@pingvin156

You send an AJAX request with new parameters and replace your block when the response arrives.
According to the Drupal Way, this is done through the Ajax API:
https://api.drupal.org/api/drupal/core%21core.api....
But no one bothers you to make your own route and controller and send a request from JS to it. And upon receipt of the answer, process it as you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question