diff --git a/core/core.services.yml b/core/core.services.yml index 70810a5..4c55baf 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -332,8 +332,7 @@ services: - { name: route_enhancer, priority: 10 } controller.page: class: Drupal\Core\Controller\HtmlPageController - calls: - - [setContainer, ['@service_container']] + arguments: ['@controller_resolver'] controller.dialog: class: Drupal\Core\Controller\DialogController arguments: ['@http_kernel'] diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 37ef268..46189b1 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -10,6 +10,7 @@ use Drupal\Core\DrupalKernel; use Drupal\Core\Database\Database; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Utility\Title; use Symfony\Component\ClassLoader\ClassLoader; use Symfony\Component\ClassLoader\ApcClassLoader; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -203,6 +204,8 @@ /** * Flag for drupal_set_title(); text has already been sanitized. + * + * @todo Move to the Title class. */ const PASS_THROUGH = -1; @@ -1709,7 +1712,7 @@ function drupal_get_title() { * Optional string value to assign to the page title; or if set to NULL * (default), leaves the current title unchanged. * @param $output - * Optional flag - normally should be left as CHECK_PLAIN. Only set to + * Optional flag - normally should be left as Title::CHECK_PLAIN. Only set to * PASS_THROUGH if you have already removed any possibly dangerous code * from $title using a function like check_plain() or filter_xss(). With this * flag the string will be passed through unchanged. @@ -1717,7 +1720,7 @@ function drupal_get_title() { * @return * The updated title of the current page. */ -function drupal_set_title($title = NULL, $output = String::CHECK_PLAIN) { +function drupal_set_title($title = NULL, $output = Title::CHECK_PLAIN) { $stored_title = &drupal_static(__FUNCTION__); if (isset($title)) { diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 4b6f09a..300eacd 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -8,6 +8,7 @@ * customized by user themes. */ +use Drupal\Component\Utility\String; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\Config; use Drupal\Core\Language\Language; @@ -2625,17 +2626,18 @@ function template_preprocess_html(&$variables) { } $site_config = config('system.site'); - // Construct page title + + // Construct the page title. if (isset($variables['page']['#title'])) { $head_title = array( 'title' => strip_tags($variables['page']['#title']), - 'name' => check_plain($site_config->get('name')), + 'name' => String::checkPlain($site_config->get('name')), ); } - elseif(drupal_get_title()) { + elseif (drupal_get_title()) { $head_title = array( 'title' => strip_tags(drupal_get_title()), - 'name' => check_plain($site_config->get('name')), + 'name' => String::checkPlain($site_config->get('name')), ); } else { @@ -2644,6 +2646,7 @@ function template_preprocess_html(&$variables) { $head_title['slogan'] = strip_tags(filter_xss_admin($site_config->get('slogan'))); } } + $variables['head_title_array'] = $head_title; $variables['head_title'] = implode(' | ', $head_title); diff --git a/core/lib/Drupal/Component/Utility/String.php b/core/lib/Drupal/Component/Utility/String.php index 44d6915..3cd6472 100644 --- a/core/lib/Drupal/Component/Utility/String.php +++ b/core/lib/Drupal/Component/Utility/String.php @@ -13,11 +13,6 @@ class String { /** - * Flag for drupal_set_title(); text is not sanitized, so run String::checkPlain(). - */ - const CHECK_PLAIN = 0; - - /** * Encodes special characters in a plain-text string for display as HTML. * * Also validates strings as UTF-8. diff --git a/core/lib/Drupal/Core/Controller/HtmlPageController.php b/core/lib/Drupal/Core/Controller/HtmlPageController.php index 5c88e59..3bce137 100644 --- a/core/lib/Drupal/Core/Controller/HtmlPageController.php +++ b/core/lib/Drupal/Core/Controller/HtmlPageController.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Controller; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -16,23 +15,23 @@ /** * Default controller for most HTML pages. */ -class HtmlPageController implements ContainerAwareInterface { +class HtmlPageController { /** - * The injection container for this object. + * The controller resolver. * - * @var \Symfony\Component\DependencyInjection\ContainerInterface + * @var \Drupal\Core\Controller\ControllerResolver */ - protected $container; + protected $controllerResolver; /** - * Injects the service container used by this object. + * Constructs a HtmlPageController instance. * - * @param \Symfony\Component\DependencyInjection\ContainerInterface $container - * The service container this object should use. + * @param \Drupal\Core\Controller\ControllerResolver $controller_resolver + * The controller resolver. */ - public function setContainer(ContainerInterface $container = NULL) { - $this->container = $container; + public function __construct(ControllerResolver $controller_resolver) { + $this->controllerResolver = $controller_resolver; } /** @@ -47,9 +46,8 @@ public function setContainer(ContainerInterface $container = NULL) { * A response object. */ public function content(Request $request, $_content) { - $resolver = $this->container->get('controller_resolver'); - $callable = $resolver->createController($_content); - $arguments = $resolver->getArguments($request, $callable); + $callable = $this->controllerResolver->createController($_content); + $arguments = $this->controllerResolver->getArguments($request, $callable); $page_content = call_user_func_array($callable, $arguments); if ($page_content instanceof Response) { return $page_content; @@ -59,10 +57,16 @@ public function content(Request $request, $_content) { '#markup' => $page_content, ); } - if (!isset($page_content['#title']) && $request->attributes->has('_title')) { + + if (is_object($callable) && $callable instanceof TitleControllerInterface) { + $page_content['#title'] = $callable->getTitle($request); + } + elseif ($request->attributes->has('_title')) { $page_content['#title'] = $request->attributes->get('_title'); } + $response = new Response(drupal_render_page($page_content)); return $response; } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/System/PageTitleFilteringTest.php b/core/modules/system/lib/Drupal/system/Tests/System/PageTitleFilteringTest.php index 22b26f8..77cd848 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/PageTitleFilteringTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/PageTitleFilteringTest.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\String; use Drupal\Core\Language\Language; +use Drupal\Core\Utility\Title; use Drupal\simpletest\WebTestBase; class PageTitleTest extends WebTestBase { @@ -62,10 +63,10 @@ function tearDown() { */ function testTitleTags() { $title = "string with HTML"; - // drupal_set_title's $filter is CHECK_PLAIN by default, so the title should be + // drupal_set_title's $filter is Title::CHECK_PLAIN by default, so the title should be // returned with check_plain(). - drupal_set_title($title, String::CHECK_PLAIN); - $this->assertTrue(strpos(drupal_get_title(), '') === FALSE, 'Tags in title converted to entities when $output is CHECK_PLAIN.'); + drupal_set_title($title, Title::CHECK_PLAIN); + $this->assertTrue(strpos(drupal_get_title(), '') === FALSE, 'Tags in title converted to entities when $output is Title::CHECK_PLAIN.'); // drupal_set_title's $filter is passed as PASS_THROUGH, so the title should be // returned with HTML. drupal_set_title($title, PASS_THROUGH); diff --git a/core/modules/system/tests/modules/test_page_test/lib/Drupal/test_page_test/Controller/Test.php b/core/modules/system/tests/modules/test_page_test/lib/Drupal/test_page_test/Controller/Test.php index 611b53f..e82d97a 100644 --- a/core/modules/system/tests/modules/test_page_test/lib/Drupal/test_page_test/Controller/Test.php +++ b/core/modules/system/tests/modules/test_page_test/lib/Drupal/test_page_test/Controller/Test.php @@ -7,7 +7,13 @@ namespace Drupal\test_page_test\Controller; -class Test { +use Drupal\Core\Controller\TitleControllerInterface; +use Symfony\Component\HttpFoundation\Request; + +/** + * Defines a test controller for page titles. + */ +class Test implements TitleControllerInterface { /** * Renders a page with a title. @@ -18,9 +24,15 @@ class Test { public function renderTitle() { $build = array(); $build['#markup'] = t('Hello Drupal'); - $build['#title'] = t('Foo'); return $build; } + /** + * {@inheritdoc} + */ + public function getTitle(Request $request) { + return t('Foo'); + } + }