1

In my tests, I was able to make content be loaded asynchronous with

function callback_function() {
    sleep(5);
    return ['#markup' => time()];
}

...

$build['timestamp'] = [
  '#lazy_builder' => ['callback_function', []],
  '#create_placeholder' => TRUE,
];

In the case above, drupal renders whatever it wants to render and after the 5 seconds from the sleep it prints the timestamp. So it runs asynchronous.

But when having placeholders attached, it will waits 5 seconds before rendering everything, its not asynchronous. Like the code below:

$build['#markup'] => 'The timestamp is [timestamp]';
$build['#attached']['placeholders']['[timestamp]'] = [
  '#lazy_builder' => ['callback_function', []],
  '#create_placeholder' => TRUE,
];

My ultimate goal is to have for example "The timestamp is 1498827840" and then get it replaced to "The timestamp is 1498827845" after the 5 seconds sleep.

1 Answer 1

1

Normally placeholders are used for elements that are not cacheable, like a timestamp, so that the rest of the render array no longer depends on the dynamic data and can be cached. On a cache hit the placeholder gets replaced by the result of the lazy builder. This is the asynchronous part.

What you are trying to do looks more like asynchronous loading of web content in the browser. You can use BipPipe for this, but then don't put a sleep command in the lazy builder, try to solve this in javascript.

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.