PlaceholderGenerator.php in Drupal 8
File
core/lib/Drupal/Core/Render/PlaceholderGenerator.php
View source
<?php
namespace Drupal\Core\Render;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\Cache;
class PlaceholderGenerator implements PlaceholderGeneratorInterface {
protected $rendererConfig;
public function __construct(array $renderer_config) {
$this->rendererConfig = $renderer_config;
}
public function canCreatePlaceholder(array $element) {
return isset($element['#lazy_builder']) && (!isset($element['#create_placeholder']) || $element['#create_placeholder'] !== FALSE);
}
public function shouldAutomaticallyPlaceholder(array $element) {
$conditions = $this->rendererConfig['auto_placeholder_conditions'];
if (isset($element['#cache']['max-age']) && $element['#cache']['max-age'] !== Cache::PERMANENT && $element['#cache']['max-age'] <= $conditions['max-age']) {
return TRUE;
}
if (isset($element['#cache']['contexts']) && array_intersect($element['#cache']['contexts'], $conditions['contexts'])) {
return TRUE;
}
if (isset($element['#cache']['tags']) && array_intersect($element['#cache']['tags'], $conditions['tags'])) {
return TRUE;
}
return FALSE;
}
public function createPlaceholder(array $element) {
$placeholder_render_array = array_intersect_key($element, [
'#lazy_builder' => TRUE,
'#cache' => TRUE,
]);
$callback = $placeholder_render_array['#lazy_builder'][0];
$arguments = UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]);
$token = Crypt::hashBase64(serialize($placeholder_render_array));
$placeholder_markup = '<drupal-render-placeholder callback="' . Html::escape($callback) . '" arguments="' . Html::escape($arguments) . '" token="' . Html::escape($token) . '"></drupal-render-placeholder>';
$placeholder_element = [];
$placeholder_element['#markup'] = Markup::create($placeholder_markup);
$placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array;
return $placeholder_element;
}
}