You are here

class PlaceholderGenerator in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Render/PlaceholderGenerator.php \Drupal\Core\Render\PlaceholderGenerator

Turns a render array into a placeholder.

Hierarchy

Expanded class hierarchy of PlaceholderGenerator

1 file declares its use of PlaceholderGenerator
RendererTestBase.php in core/tests/Drupal/Tests/Core/Render/RendererTestBase.php
Contains \Drupal\Tests\Core\Render\RendererTestBase.
1 string reference to 'PlaceholderGenerator'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses PlaceholderGenerator
render_placeholder_generator in core/core.services.yml
Drupal\Core\Render\PlaceholderGenerator

File

core/lib/Drupal/Core/Render/PlaceholderGenerator.php, line 17
Contains \Drupal\Core\Render\PlaceholderGenerator.

Namespace

Drupal\Core\Render
View source
class PlaceholderGenerator implements PlaceholderGeneratorInterface {

  /**
   * The renderer configuration array.
   *
   * @var array
   */
  protected $rendererConfig;

  /**
   * Constructs a new Placeholder service.
   *
   * @param array $renderer_config
   *   The renderer configuration array.
   */
  public function __construct(array $renderer_config) {
    $this->rendererConfig = $renderer_config;
  }

  /**
   * {@inheritdoc}
   */
  public function canCreatePlaceholder(array $element) {
    return isset($element['#lazy_builder']) && (!isset($element['#create_placeholder']) || $element['#create_placeholder'] !== FALSE);
  }

  /**
   * {@inheritdoc}
   */
  public function shouldAutomaticallyPlaceholder(array $element) {
    $conditions = $this->rendererConfig['auto_placeholder_conditions'];

    // Auto-placeholder if max-age is at or below the configured threshold.
    if (isset($element['#cache']['max-age']) && $element['#cache']['max-age'] !== Cache::PERMANENT && $element['#cache']['max-age'] <= $conditions['max-age']) {
      return TRUE;
    }

    // Auto-placeholder if a high-cardinality cache context is set.
    if (isset($element['#cache']['contexts']) && array_intersect($element['#cache']['contexts'], $conditions['contexts'])) {
      return TRUE;
    }

    // Auto-placeholder if a high-invalidation frequency cache tag is set.
    if (isset($element['#cache']['tags']) && array_intersect($element['#cache']['tags'], $conditions['tags'])) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function createPlaceholder(array $element) {
    $placeholder_render_array = array_intersect_key($element, [
      // Placeholders are replaced with markup by executing the associated
      // #lazy_builder callback, which generates a render array, and which the
      // Renderer will render and replace the placeholder with.
      '#lazy_builder' => TRUE,
      // The cacheability metadata for the placeholder. The rendered result of
      // the placeholder may itself be cached, if [#cache][keys] are specified.
      '#cache' => TRUE,
    ]);

    // Generate placeholder markup. Note that the only requirement is that this
    // is unique markup that isn't easily guessable. The #lazy_builder callback
    // and its arguments are put in the placeholder markup solely to simplify<<<
    // debugging.
    $callback = $placeholder_render_array['#lazy_builder'][0];
    $arguments = UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]);
    $token = hash('crc32b', serialize($placeholder_render_array));
    $placeholder_markup = '<drupal-render-placeholder callback="' . Html::escape($callback) . '" arguments="' . Html::escape($arguments) . '" token="' . Html::escape($token) . '"></drupal-render-placeholder>';

    // Build the placeholder element to return.
    $placeholder_element = [];
    $placeholder_element['#markup'] = Markup::create($placeholder_markup);
    $placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array;
    return $placeholder_element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PlaceholderGenerator::$rendererConfig protected property The renderer configuration array.
PlaceholderGenerator::canCreatePlaceholder public function Analyzes whether the given render array can be placeholdered. Overrides PlaceholderGeneratorInterface::canCreatePlaceholder
PlaceholderGenerator::createPlaceholder public function Turns the given element into a placeholder. Overrides PlaceholderGeneratorInterface::createPlaceholder
PlaceholderGenerator::shouldAutomaticallyPlaceholder public function Whether the given render array should be automatically placeholdered. Overrides PlaceholderGeneratorInterface::shouldAutomaticallyPlaceholder
PlaceholderGenerator::__construct public function Constructs a new Placeholder service.