Commit b0f24639 authored by alexpott's avatar alexpott

Issue #2362987 by Wim Leers, Codenator, Pinolo: Remove hook_page_build() and hook_page_alter()

parent 5ddf7c26
......@@ -33,24 +33,6 @@ function testHookPageAlter() {
$this->assertPageRenderHookExceptions('common_test', 'hook_page_attachments_alter');
}
/**
* Tests hook_page_build() exceptions, a deprecated hook kept around for BC.
*/
function testHookPageBuildExceptions() {
// Also enable the system module, because that module invokes the BC hooks.
$this->enableModules(['bc_test', 'system']);
$this->assertPageRenderHookExceptions('bc_test', 'hook_page_build');
}
/**
* Tests hook_page_alter(), a deprecated hook kept around for BC.
*/
function testHookPageAttachmentsAlter() {
// Also enable the system module, because that module invokes the BC hooks.
$this->enableModules(['bc_test', 'system']);
$this->assertPageRenderHookExceptions('bc_test', 'hook_page_alter');
}
/**
* Asserts whether expected exceptions are thrown for invalid hook implementations.
*
......
......@@ -632,28 +632,6 @@ function system_page_attachments(array &$page) {
)
);
}
// Invoke hook_page_build() for modules and hook_page_alter() for both modules
// and themes, for backwards compatibility.
$attachments = [];
foreach (\Drupal::moduleHandler()->getImplementations('page_build') as $module) {
$function = $module . '_page_build';
$function($attachments);
}
if (array_diff(array_keys($attachments), ['#attached', '#post_render_cache']) !== []) {
throw new \LogicException('Only #attached and #post_render_cache may be set in hook_page_build().');
}
\Drupal::moduleHandler()->alter('page', $attachments);
\Drupal::theme()->alter('page', $attachments);
if (array_diff(array_keys($attachments), ['#attached', '#post_render_cache']) !== []) {
throw new \LogicException('Only #attached and #post_render_cache may be set in hook_page_alter().');
}
if (isset($attachments['#attached'])) {
$page['#attached'] = $attachments['#attached'];
}
if (isset($attachments['#post_render_cache'])) {
$page['#post_render_cache'] = $attachments['#post_render_cache'];
}
}
/**
......
name: 'Backwards Compatibility Test'
type: module
description: 'Support module for backwards compatibility tests.'
package: Testing
version: VERSION
core: 8.x
<?php
/**
* @file
* Helper module for backwards compatibility (BC) tests.
*/
/**
* Implements hook_page_build().
*
* @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
*/
function bc_test_page_build(&$page) {
$page['#attached']['library'][] = 'core/jquery';
if (\Drupal::state()->get('bc_test.hook_page_build.descendant_attached', FALSE)) {
$page['content']['#attached']['library'][] = 'core/jquery';
}
if (\Drupal::state()->get('bc_test.hook_page_build.render_array', FALSE)) {
$page['something'] = [
'#markup' => 'test',
];
}
}
/**
* Implements hook_page_alter().
*
* @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
*/
function bc_test_page_alter(&$page) {
$page['#attached']['library'][] = 'core/jquery';
if (\Drupal::state()->get('bc_test.hook_page_alter.descendant_attached', FALSE)) {
$page['content']['#attached']['library'][] = 'core/jquery';
}
if (\Drupal::state()->get('bc_test.hook_page_alter.render_array', FALSE)) {
$page['something'] = [
'#markup' => 'test',
];
}
}
......@@ -260,7 +260,8 @@ function common_test_post_render_cache_placeholder(array $element, array $contex
* @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
*/
function common_test_page_attachments(array &$page) {
$page['#attached']['library'][] = 'core/jquery';
$page['#attached']['library'][] = 'core/foo';
$page['#attached']['library'][] = 'core/bar';
if (\Drupal::state()->get('common_test.hook_page_attachments.descendant_attached', FALSE)) {
$page['content']['#attached']['library'][] = 'core/jquery';
......@@ -279,7 +280,12 @@ function common_test_page_attachments(array &$page) {
* @see \Drupal\system\Tests\Common\PageRenderTest::assertPageRenderHookExceptions()
*/
function common_test_page_attachments_alter(array &$page) {
$page['#attached']['library'][] = 'core/jquery';
// Remove a library that was added in common_test_page_attachments(), to test
// that this hook can do what it claims to do.
if (isset($page['#attached']['library']) && ($index = array_search('core/bar', $page['#attached']['library'])) && $index !== FALSE) {
unset($page['#attached']['library'][$index]);
}
$page['#attached']['library'][] = 'core/baz';
if (\Drupal::state()->get('common_test.hook_page_attachments_alter.descendant_attached', FALSE)) {
$page['content']['#attached']['library'][] = 'core/jquery';
......
......@@ -810,60 +810,6 @@ function hook_css_alter(&$css) {
unset($css[drupal_get_path('module', 'system') . '/defaults.css']);
}
/**
* Add attachments (typically assets) to a page before it is rendered.
*
* Kept around for backwards compatibility, but now allows only attachments to
* be added, adding renderable arrays is no longer allowed.
*
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0. Successor:
* hook_page_attachments(). Is now effectively an alias of that hook.
*
* @param $page
* The page to which to add attachments.
*
* @see hook_page_attachments()
*/
function hook_page_build(&$page) {
$path = drupal_get_path('module', 'foo');
// Add JavaScript/CSS assets to all pages.
// @see drupal_process_attached()
$page['#attached']['js'][$path . '/foo.js'] = array('every_page' => TRUE);
$page['#attached']['css'][$path . '/foo.base.css'] = array('every_page' => TRUE);
$page['#attached']['css'][$path . '/foo.theme.css'] = array('every_page' => TRUE);
// Add a special CSS file to a certain page only.
if (drupal_is_front_page()) {
$page['#attached']['css'][] = $path . '/foo.front.css';
}
}
/**
* Perform alterations before a page is rendered.
*
* Kept around for backwards compatibility, but now allows only attachments to
* be added, altering the renderable array for the page is no longer allowed.
*
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0. Successor:
* hook_page_attachments_alter(). Is now effectively an alias of that hook.
*
* Use this hook when you want to remove or alter attachments at the page
* level, or add attachments at the page level that depend on an other module's
* attachments (this hook runs after hook_page_build().
*
* @param $page
* An empty renderable array representing the page.
*
* @see hook_page_build()
*/
function hook_page_alter(&$page) {
// Conditionally remove an asset.
if (in_array('core/jquery', $page['#attached']['library'])) {
$index = array_search('core/jquery', $page['#attached']['library']);
unset($page['#attached']['library'][$index]);
}
}
/**
* Add attachments (typically assets) to a page before it is rendered.
*
......
......@@ -388,7 +388,7 @@ function views_preprocess_html(&$variables) {
* The ID of the display within $view whose contextual links will be added.
*
* @see \Drupal\views\Plugin\block\block\ViewsBlock::addContextualLinks()
* @see views_page_alter()
* @see views_preprocess_page()
* @see template_preprocess_views_view()
*/
function views_add_contextual_links(&$render_element, $location, ViewExecutable $view, $display_id) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment