You are here

BigPipeRenderStrategy.php in Render cache 7.2

File

modules/renderer/render_cache_big_pipe/src/RenderCache/RenderStrategy/BigPipeRenderStrategy.php
View source
<?php

/**
 * @file
 * Contains \Drupal\render_cache_big_pipe\RenderCache\RenderStrategy\BigPipeRenderStrategy
 */
namespace Drupal\render_cache_big_pipe\RenderCache\RenderStrategy;

use Drupal\render_cache\RenderCache\RenderStrategy\BaseRenderStrategy;

/**
 * Big Pipe RenderStrategy - Provides big pipe processing for placeholders.
 *
 * @ingroup rendercache
 */
class BigPipeRenderStrategy extends BaseRenderStrategy {
  protected static $placeholders = array();
  public static function getPlaceholders() {
    return static::$placeholders;
  }

  /**
   * {@inheritdoc}
   */
  public function render(array $args) {
    $placeholders = array();
    foreach ($args as $placeholder => $ph_object) {

      // @todo Replace with a nice loading theme.
      $placeholders[$placeholder] = array();
      $id = drupal_html_id('render-cache-big-pipe-' . $ph_object['type'] . '-' . $ph_object['id']);
      $placeholders[$placeholder]['#markup'] = '<div id="' . $id . '"></div>';

      // Store the data for later usage.
      static::$placeholders[$id] = $ph_object;
    }
    return $placeholders;
  }
  public function renderPlaceholder($placeholder, $ph_object) {
    $rcc = render_cache_get_controller($ph_object['type']);
    $rcc
      ->setContext($ph_object['context']);
    $objects = array(
      $ph_object['id'] => $ph_object['object'],
    );
    $build = $rcc
      ->viewPlaceholders($objects);
    $output = drupal_render($build);
    $html = drupal_json_encode($output);

    // @todo Add helper function.
    $markup = <<<EOF
<script type="text/javascript">
var element = document.getElementById("{<span class="php-variable">$placeholder</span>}");
var newElement = document.createElement("span");
newElement.innerHTML = {<span class="php-variable">$html</span>};
var newChild = newElement.firstChild;
element.parentNode.replaceChild(newChild, element);
</script>
EOF;
    return $markup;
  }

}

Classes

Namesort descending Description
BigPipeRenderStrategy Big Pipe RenderStrategy - Provides big pipe processing for placeholders.