class PlaceholdersTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\PlaceholdersTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\PlaceholdersTest
  3. 11.x core/tests/Drupal/Tests/Core/Render/RendererTestBase.php \Drupal\Tests\Core\Render\PlaceholdersTest

Hierarchy

Expanded class hierarchy of PlaceholdersTest

File

core/tests/Drupal/Tests/Core/Render/RendererTestBase.php, line 290

Namespace

Drupal\Tests\Core\Render
View source
class PlaceholdersTest implements TrustedCallbackInterface {
    
    /**
     * #lazy_builder callback; attaches setting, generates markup.
     *
     * @param string $animal
     *   An animal.
     * @param bool $use_animal_as_array_key
     *   TRUE if the $animal parameter should be used as an array key, FALSE
     *   if it should be used as a plain string.
     *
     * @return array
     *   A renderable array.
     */
    public static function callback($animal, $use_animal_as_array_key = FALSE) {
        $value = $animal;
        if ($use_animal_as_array_key) {
            $value = [
                $animal => TRUE,
            ];
        }
        return [
            '#markup' => '<p>This is a rendered placeholder!</p>',
            '#attached' => [
                'drupalSettings' => [
                    'dynamic_animal' => $value,
                ],
            ],
        ];
    }
    
    /**
     * #lazy_builder callback; attaches setting, generates markup, user-specific.
     *
     * @param string $animal
     *   An animal.
     *
     * @return array
     *   A renderable array.
     */
    public static function callbackPerUser($animal) {
        // As well as adding the user cache context, additionally suspend the
        // current Fiber if there is one.
        if ($fiber = \Fiber::getCurrent()) {
            $fiber->suspend();
        }
        $build = static::callback($animal);
        $build['#cache']['contexts'][] = 'user';
        return $build;
    }
    
    /**
     * #lazy_builder callback; attaches setting, generates markup, cache tag.
     *
     * @param string $animal
     *   An animal.
     *
     * @return array
     *   A renderable array.
     */
    public static function callbackTagCurrentTemperature($animal) {
        $build = static::callback($animal);
        $build['#cache']['tags'][] = 'current-temperature';
        return $build;
    }
    
    /**
     * A lazy builder callback that returns an invalid renderable.
     *
     * @return bool
     *   TRUE, which is not a valid return value for a lazy builder.
     */
    public static function callbackNonArrayReturn() {
        return TRUE;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function trustedCallbacks() {
        return [
            'callbackTagCurrentTemperature',
            'callbackPerUser',
            'callback',
            'callbackNonArrayReturn',
        ];
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title
PlaceholdersTest::callback public static function #lazy_builder callback; attaches setting, generates markup.
PlaceholdersTest::callbackNonArrayReturn public static function A lazy builder callback that returns an invalid renderable.
PlaceholdersTest::callbackPerUser public static function #lazy_builder callback; attaches setting, generates markup, user-specific.
PlaceholdersTest::callbackTagCurrentTemperature public static function #lazy_builder callback; attaches setting, generates markup, cache tag.
PlaceholdersTest::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING Deprecated constant Untrusted callbacks trigger E_USER_WARNING errors.

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