function FilterProcessResult::createPlaceholder

Same name and namespace in other branches
  1. 9 core/modules/filter/src/FilterProcessResult.php \Drupal\filter\FilterProcessResult::createPlaceholder()
  2. 10 core/modules/filter/src/FilterProcessResult.php \Drupal\filter\FilterProcessResult::createPlaceholder()
  3. 11.x core/modules/filter/src/FilterProcessResult.php \Drupal\filter\FilterProcessResult::createPlaceholder()

Creates a placeholder.

This generates its own placeholder markup for one major reason: to not have FilterProcessResult depend on the Renderer service, because this is a value object. As a side-effect and added benefit, this makes it easier to distinguish placeholders for filtered text versus generic render system placeholders.

Parameters

string $callback: The #lazy_builder callback that will replace the placeholder with its eventual markup.

array $args: The arguments for the #lazy_builder callback.

Return value

string The placeholder markup.

File

core/modules/filter/src/FilterProcessResult.php, line 134

Class

FilterProcessResult
Used to return values from a text filter plugin's processing method.

Namespace

Drupal\filter

Code

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) . '" arguments="' . Html::escape($arguments) . '" token="' . Html::escape($token) . '"></drupal-filter-placeholder>';
    // Add the placeholder attachment.
    $this->addAttachments([
        'placeholders' => [
            $placeholder_markup => [
                '#lazy_builder' => [
                    $callback,
                    $args,
                ],
            ],
        ],
    ]);
    // Return the placeholder markup, so that the filter wanting to use a
    // placeholder can actually insert the placeholder markup where it needs the
    // placeholder to be replaced.
    return $placeholder_markup;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.