FilterTestPlaceholders.php

Same filename in other branches
  1. 9 core/modules/filter/tests/filter_test/src/Plugin/Filter/FilterTestPlaceholders.php
  2. 8.9.x core/modules/filter/tests/filter_test/src/Plugin/Filter/FilterTestPlaceholders.php
  3. 11.x core/modules/filter/tests/filter_test/src/Plugin/Filter/FilterTestPlaceholders.php

Namespace

Drupal\filter_test\Plugin\Filter

File

core/modules/filter/tests/filter_test/src/Plugin/Filter/FilterTestPlaceholders.php

View source
<?php

namespace Drupal\filter_test\Plugin\Filter;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\filter\Attribute\Filter;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\filter\Plugin\FilterInterface;

/**
 * Provides a test filter to use placeholders.
 */
class FilterTestPlaceholders extends FilterBase implements TrustedCallbackInterface {
    
    /**
     * {@inheritdoc}
     */
    public function process($text, $langcode) {
        $result = new FilterProcessResult($text);
        $placeholder_with_argument = $result->createPlaceholder('\\Drupal\\filter_test\\Plugin\\Filter\\FilterTestPlaceholders::renderDynamicThing', [
            'llama',
        ]);
        $placeholder_without_arguments = $result->createPlaceholder('\\Drupal\\filter_test\\Plugin\\Filter\\FilterTestPlaceholders::renderStaticThing', []);
        $result->setProcessedText($text . '<p>' . $placeholder_with_argument . '</p>' . '<p>' . $placeholder_without_arguments . '</p>');
        return $result;
    }
    
    /**
     * #lazy_builder callback; builds a render array containing the dynamic thing.
     *
     * @param string $thing
     *   A "thing" string.
     *
     * @return array
     *   A renderable array.
     */
    public static function renderDynamicThing($thing) {
        return [
            '#markup' => new FormattableMarkup('This is a dynamic @thing.', [
                '@thing' => $thing,
            ]),
        ];
    }
    
    /**
     * #lazy_builder callback; builds a render array.
     *
     * @return array
     *   A renderable array.
     */
    public static function renderStaticThing() : array {
        return [
            '#markup' => 'This is a static llama.',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public static function trustedCallbacks() {
        return [
            'renderDynamicThing',
            'renderStaticThing',
        ];
    }

}

Classes

Title Deprecated Summary
FilterTestPlaceholders Provides a test filter to use placeholders.

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