diff --git a/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php index fa4be98..2d89562 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ViewSubscriber.php @@ -71,6 +71,7 @@ public function onView(GetResponseForControllerResultEvent $event) { '#markup' => $page_result, ); } + $event->setResponse(new Response(drupal_render_page($page_result))); } else { @@ -83,6 +84,20 @@ public function onView(GetResponseForControllerResultEvent $event) { '#markup' => $page_result, ); } + // If no title was returned fall back to one defined in the route. + if (!isset($page_content['#title'])) { + // A dynamic title takes priority. + if ($request->attributes->has('_title_callback')) { + $callback = $request->attributes->get('_title_callback'); + $callable = $this->controllerResolver->getControllerFromDefinition($callback); + $arguments = $this->controllerResolver->getArguments($request, $callable); + $page_content['#title'] = call_user_func_array($callable, $arguments); + } + elseif ($request->attributes->has('_title')) { + // Fall back to a static string from the route. + $page_content['#title'] = $request->attributes->get('_title'); + } + } $event->setResponse(new Response(drupal_render($page_result))); } } 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 942fa9e..c7c90e5 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,12 @@ public function testRenderTitle() { $this->assertTitle('Foo | Drupal'); $result = $this->xpath('//h1'); $this->assertEqual('Foo', (string) $result[0]); + + $this->drupalGet('form-test/object-builder'); + + $this->assertTitle('Test title | Drupal'); + $result = $this->xpath('//h1'); + $this->assertEqual('Test 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 8106fa1..cd1fa25 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 @@ public function buildForm(array $form, array &$form_state) { '#type' => 'submit', '#value' => t('Save'), ); + + $form['#title'] = 'Test title'; + return $form; }