Skip to content
Snippets Groups Projects
Commit eb5a842d authored by catch's avatar catch
Browse files

Issue #3463038 by longwave: Add helper method to generate HTML placeholders

parent 3219336f
No related branches found
No related tags found
No related merge requests found
......@@ -111,16 +111,13 @@ public function createPlaceholder(array $element) {
// 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<<<
// 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 = Crypt::hashBase64(serialize($placeholder_render_array));
$placeholder_markup = '<drupal-render-placeholder callback="' . Html::escape($callback) . '"';
if ($arguments !== '') {
$placeholder_markup .= ' arguments="' . Html::escape($arguments) . '"';
}
$placeholder_markup .= ' token="' . Html::escape($token) . '"></drupal-render-placeholder>';
$placeholder_markup = static::createPlaceholderTag('drupal-render-placeholder', [
'callback' => $placeholder_render_array['#lazy_builder'][0],
'arguments' => UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]),
'token' => Crypt::hashBase64(serialize($placeholder_render_array)),
]);
// Build the placeholder element to return.
$placeholder_element = [];
......@@ -129,4 +126,18 @@ public function createPlaceholder(array $element) {
return $placeholder_element;
}
/**
* {@inheritdoc}
*/
public static function createPlaceholderTag(string $tag, array $attributes): string {
$markup = "<$tag";
foreach ($attributes as $key => $value) {
if ($value !== '') {
$markup .= ' ' . $key . '="' . Html::escape($value) . '"';
}
}
$markup .= "></$tag>";
return $markup;
}
}
......@@ -62,4 +62,17 @@ public function shouldAutomaticallyPlaceholder(array $element);
*/
public function createPlaceholder(array $element);
/**
* Generates a placeholder HTML tag.
*
* @param string $tag
* The placeholder tag.
* @param array $attributes
* An array of key-value pairs to use as tag attributes.
*
* @return string
* The HTML placeholder.
*/
public static function createPlaceholderTag(string $tag, array $attributes): string;
}
......@@ -3,9 +3,9 @@
namespace Drupal\filter;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\PlaceholderGenerator;
/**
* Used to return values from a text filter plugin's processing method.
......@@ -133,14 +133,11 @@ public function setProcessedText($processed_text) {
*/
public function createPlaceholder($callback, array $args) {
// Generate placeholder markup.
// @see \Drupal\Core\Render\PlaceholderGenerator::createPlaceholder()
$arguments = UrlHelper::buildQuery($args);
$token = Crypt::hashBase64(serialize([$callback, $args]));
$placeholder_markup = '<drupal-filter-placeholder callback="' . Html::escape($callback) . '"';
if ($arguments !== '') {
$placeholder_markup .= ' arguments="' . Html::escape($arguments) . '"';
}
$placeholder_markup .= ' token="' . Html::escape($token) . '"></drupal-filter-placeholder>';
$placeholder_markup = PlaceholderGenerator::createPlaceholderTag('drupal-filter-placeholder', [
'callback' => $callback,
'arguments' => UrlHelper::buildQuery($args),
'token' => Crypt::hashBase64(serialize([$callback, $args])),
]);
// Add the placeholder attachment.
$this->addAttachments([
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment