.../Drupal/Core/Render/Element/StatusMessages.php | 122 +++++++++++++++++++++ .../Plugin/DisplayVariant/SimplePageVariant.php | 3 + .../BlockPageDisplayVariantSubscriber.php | 2 +- .../ckeditor/src/Tests/CKEditorAdminTest.php | 7 +- .../contact/src/Tests/ContactPersonalTest.php | 2 +- .../modules/system/src/MessagesPostRenderCache.php | 74 ------------- .../src/Plugin/Block/SystemPageMessagesBlock.php | 24 +--- .../src/Tests/System/SiteMaintenanceTest.php | 4 +- 8 files changed, 130 insertions(+), 108 deletions(-) diff --git a/core/lib/Drupal/Core/Render/Element/StatusMessages.php b/core/lib/Drupal/Core/Render/Element/StatusMessages.php new file mode 100644 index 0000000..2e145d8 --- /dev/null +++ b/core/lib/Drupal/Core/Render/Element/StatusMessages.php @@ -0,0 +1,122 @@ + [ + get_class() . '::generatePlaceholder', + ], + ]; + } + + /** + * #pre_render callback to generate a placeholder. + * + * Ensures the same token is used for all instances, hence resulting in the + * same placeholder for all places rendering the status messages for this + * request (e.g. in multiple blocks). This ensures we can put the rendered + * messages in all placeholders in one go. + * Also ensures the same context key is used for the #post_render_cache + * property, this ensures that if status messages are rendered multiple times, + * their individual (but identical!) #post_render_cache properties are merged, + * ensuring the callback is only invoked once. + * + * @see ::renderMessages() + + * @param array $element + * A renderable array. + * + * @return array + * The updated renderable array containing the placeholder. + */ + public static function generatePlaceholder(array $element) { + $plugin_id = 'status_messages'; + + $callback = get_class() . '::renderMessages'; + $context = [ + 'token' => Crypt::hmacBase64($plugin_id, Settings::getHashSalt()), + ]; + $placeholder = static::renderer()->generateCachePlaceholder($callback, $context); + $element['#post_render_cache'] = [ + $callback => [ + $plugin_id => $context, + ], + ]; + $element['#markup'] = $placeholder; + + return $element; + } + + /** + * #post_render_cache callback; replaces placeholder with messages. + * + * Note: this is designed to replace all #post_render_cache placeholders for + * messages in a single #post_render_cache callback; hence all placeholders + * must be identical. + * + * @see ::getInfo() + * + * @param array $element + * The renderable array that contains the to be replaced placeholder. + * @param array $context + * An array with any context information. + * + * @return array + * A renderable array containing the messages. + */ + public static function renderMessages(array $element, array $context) { + $renderer = static::renderer(); + + // Render the messages. + $messages = [ + '#theme' => 'status_messages', + '#message_list' => drupal_get_messages(), + '#status_headings' => [ + 'status' => t('Status message'), + 'error' => t('Error message'), + 'warning' => t('Warning message'), + ], + ]; + $markup = $renderer->render($messages); + + // Replace the placeholder. + $callback = get_class() . '::renderMessages'; + $placeholder = $renderer->generateCachePlaceholder($callback, $context); + $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']); + $element = $renderer->mergeBubbleableMetadata($element, $messages); + + return $element; + } + + /** + * Wraps the renderer. + * + * @return \Drupal\Core\Render\RendererInterface + */ + protected static function renderer() { + return \Drupal::service('renderer'); + } + +} diff --git a/core/lib/Drupal/Core/Render/Plugin/DisplayVariant/SimplePageVariant.php b/core/lib/Drupal/Core/Render/Plugin/DisplayVariant/SimplePageVariant.php index 7772875..a10bd18 100644 --- a/core/lib/Drupal/Core/Render/Plugin/DisplayVariant/SimplePageVariant.php +++ b/core/lib/Drupal/Core/Render/Plugin/DisplayVariant/SimplePageVariant.php @@ -39,6 +39,9 @@ public function setMainContent(array $main_content) { */ public function build() { $build = [ + 'messages' => [ + '#type' => 'status_messages', + ], 'content' => $this->mainContent, ]; return $build; diff --git a/core/modules/block/src/EventSubscriber/BlockPageDisplayVariantSubscriber.php b/core/modules/block/src/EventSubscriber/BlockPageDisplayVariantSubscriber.php index 21026aa..f66b968 100644 --- a/core/modules/block/src/EventSubscriber/BlockPageDisplayVariantSubscriber.php +++ b/core/modules/block/src/EventSubscriber/BlockPageDisplayVariantSubscriber.php @@ -26,7 +26,7 @@ class BlockPageDisplayVariantSubscriber implements EventSubscriberInterface { * The event to process. */ public function onSelectPageDisplayVariant(PageDisplayVariantSelectionEvent $event) { -// $event->setPluginId('block_page'); + $event->setPluginId('block_page'); } /** diff --git a/core/modules/ckeditor/src/Tests/CKEditorAdminTest.php b/core/modules/ckeditor/src/Tests/CKEditorAdminTest.php index 0c6a0c4..6849757 100644 --- a/core/modules/ckeditor/src/Tests/CKEditorAdminTest.php +++ b/core/modules/ckeditor/src/Tests/CKEditorAdminTest.php @@ -23,7 +23,7 @@ class CKEditorAdminTest extends WebTestBase { * * @var array */ - public static $modules = array('filter', 'editor', 'ckeditor', 'block'); + public static $modules = array('filter', 'editor', 'ckeditor'); /** * A user with the 'administer filters' permission. @@ -32,14 +32,9 @@ class CKEditorAdminTest extends WebTestBase { */ protected $adminUser; - /** - * {@inheritdoc} - */ protected function setUp() { parent::setUp(); - $this->drupalPlaceBlock('system_page_messages_block'); - // Create text format. $filtered_html_format = entity_create('filter_format', array( 'format' => 'filtered_html', diff --git a/core/modules/contact/src/Tests/ContactPersonalTest.php b/core/modules/contact/src/Tests/ContactPersonalTest.php index 8cc33cd..a1229d1 100644 --- a/core/modules/contact/src/Tests/ContactPersonalTest.php +++ b/core/modules/contact/src/Tests/ContactPersonalTest.php @@ -23,7 +23,7 @@ class ContactPersonalTest extends WebTestBase { * * @var array */ - public static $modules = array('contact', 'dblog', 'block'); + public static $modules = array('contact', 'dblog'); /** * A user with some administrative permissions. diff --git a/core/modules/system/src/MessagesPostRenderCache.php b/core/modules/system/src/MessagesPostRenderCache.php deleted file mode 100644 index a8b1a57..0000000 --- a/core/modules/system/src/MessagesPostRenderCache.php +++ /dev/null @@ -1,74 +0,0 @@ -renderer = $renderer; - } - - /** - * #post_render_cache callback; replaces placeholder with messages. - * - * @param array $element - * The renderable array that contains the to be replaced placeholder. - * @param array $context - * An array with any context information. - * - * @return array - * A renderable array containing the messages. - */ - public function renderMessages(array $element, array $context) { - // Render the messages. - $messages = [ - '#theme' => 'status_messages', - '#message_list' => drupal_get_messages(), - '#status_headings' => [ - 'status' => t('Status message'), - 'error' => t('Error message'), - 'warning' => t('Warning message'), - ], - ]; - $markup = $this->renderer->render($messages); - - // Replace the placeholder. - $callback = 'system.messages_post_render_cache:renderMessages'; - $placeholder = drupal_render_cache_generate_placeholder($callback, $context); - $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']); - $element = Renderer::mergeBubbleableMetadata($element, $messages); - - return $element; - } - -} diff --git a/core/modules/system/src/Plugin/Block/SystemPageMessagesBlock.php b/core/modules/system/src/Plugin/Block/SystemPageMessagesBlock.php index b31d3e4..47473e9 100644 --- a/core/modules/system/src/Plugin/Block/SystemPageMessagesBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemPageMessagesBlock.php @@ -33,31 +33,9 @@ public function defaultConfiguration() { /** * {@inheritdoc} - * - * Ensure the same token is used for all instances, hence resulting in the - * same placeholder for all message blocks. This ensures we can put the - * rendered messages in all message blocks in one go. - * Also ensure the same context key is used for the #post_render_cache - * property, this ensures that if multiple messages blocks exists, their - * individual (but identical!) #post_render_cache properties are merged, - * ensuring the callback is only invoked once. - * - * @see \Drupal\system\MessagesPostRenderCache() */ public function build() { - $callback = 'system.messages_post_render_cache:renderMessages'; - $context = [ - 'token' => Crypt::hmacBase64($this->getPluginId(), Settings::getHashSalt()), - ]; - $placeholder = drupal_render_cache_generate_placeholder($callback, $context); - return [ - '#post_render_cache' => array( - $callback => array( - $this->getPluginId() => $context, - ), - ), - '#markup' => $placeholder, - ]; + return ['#type' => 'status_messages']; } /** diff --git a/core/modules/system/src/Tests/System/SiteMaintenanceTest.php b/core/modules/system/src/Tests/System/SiteMaintenanceTest.php index 128353a..48c592d 100644 --- a/core/modules/system/src/Tests/System/SiteMaintenanceTest.php +++ b/core/modules/system/src/Tests/System/SiteMaintenanceTest.php @@ -21,15 +21,13 @@ class SiteMaintenanceTest extends WebTestBase { * * @var array */ - public static $modules = array('node', 'block'); + public static $modules = array('node'); protected $admin_user; protected function setUp() { parent::setUp(); - $this->drupalPlaceBlock('system_page_messages_block'); - // Configure 'node' as front page. $this->config('system.site')->set('page.front', 'node')->save();