class RenderPlaceholderMessageTestController

Same name in other branches
  1. 9 core/modules/system/tests/modules/render_placeholder_message_test/src/RenderPlaceholderMessageTestController.php \Drupal\render_placeholder_message_test\RenderPlaceholderMessageTestController
  2. 8.9.x core/modules/system/tests/modules/render_placeholder_message_test/src/RenderPlaceholderMessageTestController.php \Drupal\render_placeholder_message_test\RenderPlaceholderMessageTestController
  3. 10 core/modules/system/tests/modules/render_placeholder_message_test/src/RenderPlaceholderMessageTestController.php \Drupal\render_placeholder_message_test\RenderPlaceholderMessageTestController

Hierarchy

Expanded class hierarchy of RenderPlaceholderMessageTestController

1 string reference to 'RenderPlaceholderMessageTestController'
render_placeholder_message_test.services.yml in core/modules/system/tests/modules/render_placeholder_message_test/render_placeholder_message_test.services.yml
core/modules/system/tests/modules/render_placeholder_message_test/render_placeholder_message_test.services.yml
1 service uses RenderPlaceholderMessageTestController
render_placeholder_message_test.controller in core/modules/system/tests/modules/render_placeholder_message_test/render_placeholder_message_test.services.yml
Drupal\render_placeholder_message_test\RenderPlaceholderMessageTestController

File

core/modules/system/tests/modules/render_placeholder_message_test/src/RenderPlaceholderMessageTestController.php, line 13

Namespace

Drupal\render_placeholder_message_test
View source
class RenderPlaceholderMessageTestController implements TrustedCallbackInterface, ContainerInjectionInterface {
    
    /**
     * Constructs a new RenderPlaceholderMessageTestController object.
     *
     * @param \Drupal\Core\Render\RendererInterface $renderer
     *   The renderer service.
     */
    public function __construct(RendererInterface $renderer) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('renderer'));
    }
    
    /**
     * @return array
     */
    public function messagesPlaceholderFirst() {
        return $this->build([
            // cspell:disable
'<drupal-render-placeholder callback="Drupal\\Core\\Render\\Element\\StatusMessages::renderMessages" arguments="0" token="_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"></drupal-render-placeholder>',
            '<drupal-render-placeholder callback="\\Drupal\\render_placeholder_message_test\\RenderPlaceholderMessageTestController::setAndLogMessage" arguments="0=P1" token="JBp04zOwNhYqMBgRkyBnPdma8m4l2elDnXMJ9tEsP6k"></drupal-render-placeholder>',
            '<drupal-render-placeholder callback="\\Drupal\\render_placeholder_message_test\\RenderPlaceholderMessageTestController::setAndLogMessage" arguments="0=P2" token="JnoubSJT1l92Dm4fJw4EPsSzRsmE88H6Q1zu9-OzDh4"></drupal-render-placeholder>',
        ]);
    }
    
    /**
     * @return array
     */
    public function messagesPlaceholderMiddle() {
        return $this->build([
            // cspell:disable
'<drupal-render-placeholder callback="\\Drupal\\render_placeholder_message_test\\RenderPlaceholderMessageTestController::setAndLogMessage" arguments="0=P1" token="JBp04zOwNhYqMBgRkyBnPdma8m4l2elDnXMJ9tEsP6k"></drupal-render-placeholder>',
            '<drupal-render-placeholder callback="Drupal\\Core\\Render\\Element\\StatusMessages::renderMessages" arguments="0" token="_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"></drupal-render-placeholder>',
            '<drupal-render-placeholder callback="\\Drupal\\render_placeholder_message_test\\RenderPlaceholderMessageTestController::setAndLogMessage" arguments="0=P2" token="JnoubSJT1l92Dm4fJw4EPsSzRsmE88H6Q1zu9-OzDh4"></drupal-render-placeholder>',
        ]);
    }
    
    /**
     * @return array
     */
    public function messagesPlaceholderLast() {
        return $this->build([
            // cspell:disable
'<drupal-render-placeholder callback="\\Drupal\\render_placeholder_message_test\\RenderPlaceholderMessageTestController::setAndLogMessage" arguments="0=P1" token="JBp04zOwNhYqMBgRkyBnPdma8m4l2elDnXMJ9tEsP6k"></drupal-render-placeholder>',
            '<drupal-render-placeholder callback="\\Drupal\\render_placeholder_message_test\\RenderPlaceholderMessageTestController::setAndLogMessage" arguments="0=P2" token="JnoubSJT1l92Dm4fJw4EPsSzRsmE88H6Q1zu9-OzDh4"></drupal-render-placeholder>',
            '<drupal-render-placeholder callback="Drupal\\Core\\Render\\Element\\StatusMessages::renderMessages" arguments="0" token="_HAdUpwWmet0TOTe2PSiJuMntExoshbm1kh2wQzzzAA"></drupal-render-placeholder>',
        ]);
    }
    
    /**
     * @return array
     */
    public function queuedMessages() {
        return [
            '#type' => 'status_messages',
        ];
    }
    
    /**
     * @return array
     */
    protected function build(array $placeholder_order) {
        $build = [];
        $build['messages'] = [
            '#type' => 'status_messages',
        ];
        $build['p1'] = [
            '#lazy_builder' => [
                '\\Drupal\\render_placeholder_message_test\\RenderPlaceholderMessageTestController::setAndLogMessage',
                [
                    'P1',
                ],
            ],
            '#create_placeholder' => TRUE,
        ];
        $build['p2'] = [
            '#lazy_builder' => [
                '\\Drupal\\render_placeholder_message_test\\RenderPlaceholderMessageTestController::setAndLogMessage',
                [
                    'P2',
                ],
            ],
            '#create_placeholder' => TRUE,
        ];
        $renderer = $this->renderer;
        $renderer->executeInRenderContext(new RenderContext(), function () use (&$build, $renderer) {
            return $renderer->render($build, FALSE);
        });
        $reordered = [];
        foreach ($placeholder_order as $placeholder) {
            $reordered[$placeholder] = $build['#attached']['placeholders'][$placeholder];
        }
        $build['#attached']['placeholders'] = $reordered;
        return $build;
    }
    
    /**
     * #lazy_builder callback; sets and prints a message.
     *
     * @param string $message
     *   The message to send.
     *
     * @return array
     *   A renderable array containing the message.
     */
    public static function setAndLogMessage($message) {
        // Ensure that messages are rendered last even when earlier placeholders
        // suspend the Fiber, this will cause BigPipe::renderPlaceholders() to loop
        // around all of the fibers before resuming this one, then finally rendering
        // the messages when there are no other placeholders left.
        if (\Fiber::getCurrent() !== NULL) {
            \Fiber::suspend();
        }
        // Set message.
        \Drupal::messenger()->addStatus($message);
        // Print which message is expected.
        return [
            '#markup' => '<p class="logged-message">Message: ' . $message . '</p>',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public static function trustedCallbacks() {
        return [
            'setAndLogMessage',
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
RenderPlaceholderMessageTestController::build protected function
RenderPlaceholderMessageTestController::create public static function Overrides ContainerInjectionInterface::create
RenderPlaceholderMessageTestController::messagesPlaceholderFirst public function
RenderPlaceholderMessageTestController::messagesPlaceholderLast public function
RenderPlaceholderMessageTestController::messagesPlaceholderMiddle public function
RenderPlaceholderMessageTestController::queuedMessages public function
RenderPlaceholderMessageTestController::setAndLogMessage public static function #lazy_builder callback; sets and prints a message.
RenderPlaceholderMessageTestController::trustedCallbacks public static function Overrides TrustedCallbackInterface::trustedCallbacks
RenderPlaceholderMessageTestController::__construct public function Constructs a new RenderPlaceholderMessageTestController object.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.

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