diff --git a/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php index fa4be988322..ae1d0834ba7 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php @@ -71,6 +71,12 @@ class ViewSubscriber implements EventSubscriberInterface { '#markup' => $page_result, ); } + + // If no title was returned fall back to one defined in the route. + if (!isset($page_result['#title']) && $request->attributes->has('_title')) { + $page_result['#title'] = $request->attributes->get('_title'); + } + $event->setResponse(new Response(drupal_render_page($page_result))); } else { @@ -83,6 +89,12 @@ class ViewSubscriber implements EventSubscriberInterface { '#markup' => $page_result, ); } + + // If no title was returned fall back to one defined in the route. + if (!isset($page_result['#title']) && $request->attributes->has('_title')) { + $page_result['#title'] = $request->attributes->get('_title'); + } + $event->setResponse(new Response(drupal_render($page_result))); } } diff --git a/core/modules/overlay/lib/Drupal/overlay/Tests/OverlayRenderTest.php b/core/modules/overlay/lib/Drupal/overlay/Tests/OverlayRenderTest.php new file mode 100644 index 00000000000..b32c4d6c41c --- /dev/null +++ b/core/modules/overlay/lib/Drupal/overlay/Tests/OverlayRenderTest.php @@ -0,0 +1,44 @@ + 'Overlay child page rendering', + 'description' => 'Tests the rendering of a page in an overlay.', + 'group' => 'Overlay', + ); + } + + /** + * Tests the title of a page in an overlay. + */ + public function testOverlayTitle() { + $account = $this->drupalCreateUser(array('access overlay')); + $this->drupalLogin($account); + + $this->drupalGet('admin/test-render-title', array('query' => array('render' => 'overlay'))); + $result = $this->xpath('//h1[@id = "overlay-title"]'); + $this->assertEqual((string) $result[0], 'Foo'); + } + +} diff --git a/core/modules/overlay/overlay.module b/core/modules/overlay/overlay.module index efa5280d937..3788535208e 100644 --- a/core/modules/overlay/overlay.module +++ b/core/modules/overlay/overlay.module @@ -5,6 +5,7 @@ * Displays the Drupal administration interface in an overlay. */ +use Drupal\Core\Template\RenderWrapper; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; @@ -360,7 +361,13 @@ function overlay_preprocess_maintenance_page(&$variables) { */ function template_preprocess_overlay(&$variables) { $variables['tabs'] = menu_primary_local_tasks(); - $variables['title'] = drupal_get_title(); + + if (isset($variables['page']['#title'])) { + $variables['title'] = $variables['page']['#title']; + } + else { + $variables['title'] = new RenderWrapper('drupal_get_title'); + } $variables['disable_overlay'] = overlay_disable_message(); // Add attributes for the overlay container. diff --git a/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php b/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php index 942fa9e9c70..d5fead51d38 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php @@ -19,7 +19,7 @@ class PageTitleTest extends WebTestBase { * * @var array */ - public static $modules = array('node', 'test_page_test'); + public static $modules = array('node', 'test_page_test', 'form_test'); protected $content_user; protected $saved_title; @@ -136,6 +136,20 @@ class PageTitleTest extends WebTestBase { $this->assertTitle('Foo | Drupal'); $result = $this->xpath('//h1'); $this->assertEqual('Foo', (string) $result[0]); + + // Test a controller using _controller instead of _content. + $this->drupalGet('test-render-title-controller'); + + $this->assertTitle('Foo | Drupal'); + $result = $this->xpath('//h1'); + $this->assertEqual('Foo', (string) $result[0]); + + // Test forms + $this->drupalGet('form-test/object-builder'); + + $this->assertTitle('Test dynamic title | Drupal'); + $result = $this->xpath('//h1'); + $this->assertEqual('Test dynamic title', (string) $result[0]); } } diff --git a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php index 1ec5f71bc91..4798b44f72a 100644 --- a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php +++ b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php @@ -37,6 +37,9 @@ class FormTestObject extends FormBase { '#type' => 'submit', '#value' => $this->t('Save'), ); + + $form['#title'] = 'Test dynamic title'; + return $form; } diff --git a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml index 0f45d10527b..2de2a4d3e37 100644 --- a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml +++ b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml @@ -4,3 +4,18 @@ test_page_render_title: _content: 'Drupal\test_page_test\Controller\Test::renderTitle' requirements: _access: 'TRUE' + +admin_test_page_render_title: + pattern: "/admin/test-render-title" + defaults: + _content: 'Drupal\test_page_test\Controller\Test::renderTitle' + requirements: + _access: 'TRUE' + +test_page_render_title_controller: + pattern: "/test-render-title-controller" + defaults: + _controller: 'Drupal\test_page_test\Controller\Test::renderTitle' + requirements: + _access: 'TRUE' +