diff --git a/big_pipe.module b/big_pipe.module index 4cacef4..39dfbf2 100644 --- a/big_pipe.module +++ b/big_pipe.module @@ -6,6 +6,7 @@ */ use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy; +use Drupal\Component\Utility\Html; use Drupal\Core\Url; /** @@ -39,3 +40,43 @@ function big_pipe_page_attachments(array &$page) { ]; } } + +/** + * Implements hook_theme(). + */ +function big_pipe_theme() { + return array( + 'big_pipe_js_placeholder' => array( + 'render element' => 'elements', + ), + ); +} + +/** + * Implements hook_theme_suggestions_HOOK(). + */ +function big_pipe_theme_suggestions_big_pipe_js_placeholder(array $variables) { + $placeholder = reset($variables['elements']['#attached']['big_pipe_placeholders']); + $base_suggestion = $variables['theme_hook_original']; + $suggestions = []; + if (!empty($placeholder['#lazy_builder'])) { + $callback = $placeholder['#lazy_builder'][0]; + $suggestions[] = $base_suggestion = $base_suggestion . '__' . strtolower(str_replace(['\\', '::', ':', '.'], '_', $callback)); + } + if (!empty($placeholder['#cache']['keys'])) { + $base_suggestion = $variables['theme_hook_original']; + foreach ($placeholder['#cache']['keys'] as $key) { + $suggestions[] = $base_suggestion = $base_suggestion . '__' . $key; + } + } + return $suggestions; +} + +/** + * Prepares variables for Big Pipe placeholder templates. + * + * Default template: big-pipe-js-placeholder.html.twig. + */ +function template_preprocess_big_pipe_js_placeholder(&$variables) { + $variables['attributes']['data-big-pipe-selector'] = $variables['elements']['#big_pipe_js_selector']; +} diff --git a/src/Render/BigPipe.php b/src/Render/BigPipe.php index abe8133..63734cf 100644 --- a/src/Render/BigPipe.php +++ b/src/Render/BigPipe.php @@ -411,16 +411,13 @@ EOF; * Values are the BigPipe selectors. */ protected function getPlaceholderOrder($html) { - $fragments = explode('
', $fragment, 2); - $placeholder = $t[0]; - $order[] = $placeholder; + foreach ($elements as $element) { + $order[] = Html::escape($element->getAttribute('data-big-pipe-selector')); } - return $order; } diff --git a/src/Render/Placeholder/BigPipeStrategy.php b/src/Render/Placeholder/BigPipeStrategy.php index c98ea3d..0cf4299 100644 --- a/src/Render/Placeholder/BigPipeStrategy.php +++ b/src/Render/Placeholder/BigPipeStrategy.php @@ -179,7 +179,8 @@ class BigPipeStrategy implements PlaceholderStrategyInterface { } return [ - '#markup' => '
', + '#theme' => 'big_pipe_js_placeholder', + '#big_pipe_js_selector' => $big_pipe_js_selector, '#cache' => [ 'max-age' => 0, 'contexts' => [ diff --git a/templates/big-pipe-js-placeholder.html.twig b/templates/big-pipe-js-placeholder.html.twig new file mode 100644 index 0000000..6920950 --- /dev/null +++ b/templates/big-pipe-js-placeholder.html.twig @@ -0,0 +1,17 @@ +{# +/** + * @file + * Default theme implementation to display a Big Pipe JS placeholder. + * + * Available variables: + * - attributes: array of HTML attributes populated by modules, intended to + * be added to the main container tag of this template. + * - data-big-pipe-selector: The selector used by Big Pipe to replace this + * element with the lazy loaded content. + * + * @see template_preprocess_big_pipe_js_placeholder() + * + * @ingroup themeable + */ +#} +