diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php index a88944a64fcfc8ae2b27dda4d6753765aca6e39d..0c198da65a98d7787383625333463759d2c682ff 100644 --- a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php +++ b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php @@ -7,57 +7,16 @@ namespace Drupal\Core\Breadcrumb; +use Drupal\Core\Routing\LinkGeneratorTrait; use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Defines a common base class for breadcrumb builders adding a link generator. * - * @todo Use traits once we have a PHP 5.4 requirement. + * @todo This class is now vestigial. Remove it and use the traits in + * breadcrumb builders directly. */ abstract class BreadcrumbBuilderBase implements BreadcrumbBuilderInterface { use StringTranslationTrait; - - /** - * The link generator. - * - * @var \Drupal\Core\Utility\LinkGeneratorInterface - */ - protected $linkGenerator; - - /** - * Returns the service container. - * - * @return \Symfony\Component\DependencyInjection\ContainerInterface $container - * The service container. - */ - protected function container() { - return \Drupal::getContainer(); - } - - /** - * Renders a link to a route given a route name and its parameters. - * - * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details - * on the arguments, usage, and possible exceptions. - * - * @return string - * An HTML string containing a link to the given route and parameters. - */ - protected function l($text, $route_name, array $parameters = array(), array $options = array()) { - return $this->linkGenerator()->generate($text, $route_name, $parameters, $options); - } - - /** - * Returns the link generator. - * - * @return \Drupal\Core\Utility\LinkGeneratorInterface - * The link generator - */ - protected function linkGenerator() { - if (!isset($this->linkGenerator)) { - $this->linkGenerator = $this->container()->get('link_generator'); - } - return $this->linkGenerator; - } - + use LinkGeneratorTrait; } diff --git a/core/lib/Drupal/Core/Controller/ControllerBase.php b/core/lib/Drupal/Core/Controller/ControllerBase.php index 488e982c3c83bd82867172689a10012f5cf36c8a..62fab4a1e642f82bf59e2e4fcdfb48485416e16a 100644 --- a/core/lib/Drupal/Core/Controller/ControllerBase.php +++ b/core/lib/Drupal/Core/Controller/ControllerBase.php @@ -8,6 +8,8 @@ namespace Drupal\Core\Controller; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Routing\LinkGeneratorTrait; +use Drupal\Core\Routing\UrlGeneratorTrait; use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -35,6 +37,8 @@ */ abstract class ControllerBase implements ContainerInjectionInterface { use StringTranslationTrait; + use LinkGeneratorTrait; + use UrlGeneratorTrait; /** * The entity manager. @@ -71,13 +75,6 @@ abstract class ControllerBase implements ContainerInjectionInterface { */ protected $keyValue; - /** - * The url generator. - * - * @var \Drupal\Core\Routing\UrlGeneratorInterface - */ - protected $urlGenerator; - /** * The current user service. * @@ -233,32 +230,6 @@ protected function formBuilder() { return $this->formBuilder; } - /** - * Returns the URL generator service. - * - * @return \Drupal\Core\Routing\UrlGeneratorInterface - * The URL generator service. - */ - protected function urlGenerator() { - if (!$this->urlGenerator) { - $this->urlGenerator = $this->container()->get('url_generator'); - } - return $this->urlGenerator; - } - - /** - * Renders a link to a route given a route name and its parameters. - * - * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details - * on the arguments, usage, and possible exceptions. - * - * @return string - * An HTML string containing a link to the given route and parameters. - */ - public function l($text, $route_name, array $parameters = array(), array $options = array()) { - return $this->container()->get('link_generator')->generate($text, $route_name, $parameters, $options); - } - /** * Returns the current user. * @@ -314,21 +285,7 @@ private function container() { * A redirect response object that may be returned by the controller. */ public function redirect($route_name, array $route_parameters = array(), $status = 302) { - $url = $this->urlGenerator()->generate($route_name, $route_parameters, TRUE); + $url = $this->url($route_name, $route_parameters, ['absolute' => TRUE]); return new RedirectResponse($url, $status); } - - /** - * Generates a URL or path for a specific route based on the given parameters. - * - * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for - * details on the arguments, usage, and possible exceptions. - * - * @return string - * The generated URL for the given route. - */ - public function url($route_name, $route_parameters = array(), $options = array()) { - return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options); - } - } diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php index b94a3ed2cd37dfd191627df93cee12dfdbe7817d..751ad9f6a3d822a3b00cc9636cbe80a2da899277 100644 --- a/core/lib/Drupal/Core/Form/FormBase.php +++ b/core/lib/Drupal/Core/Form/FormBase.php @@ -10,7 +10,8 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\DependencySerializationTrait; -use Drupal\Core\Routing\UrlGeneratorInterface; +use Drupal\Core\Routing\LinkGeneratorTrait; +use Drupal\Core\Routing\UrlGeneratorTrait; use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -21,6 +22,8 @@ abstract class FormBase implements FormInterface, ContainerInjectionInterface { use StringTranslationTrait; use DependencySerializationTrait; + use LinkGeneratorTrait; + use UrlGeneratorTrait; /** * The current request. @@ -29,13 +32,6 @@ abstract class FormBase implements FormInterface, ContainerInjectionInterface { */ protected $request; - /** - * The URL generator. - * - * @var \Drupal\Core\Routing\UrlGeneratorInterface - */ - protected $urlGenerator; - /** * The config factory. * @@ -69,19 +65,6 @@ public function validateForm(array &$form, array &$form_state) { // Validation is optional. } - /** - * Generates a URL or path for a specific route based on the given parameters. - * - * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for - * details on the arguments, usage, and possible exceptions. - * - * @return string - * The generated URL for the given route. - */ - public function url($route_name, $route_parameters = array(), $options = array()) { - return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options); - } - /** * Retrieves a configuration object. * @@ -173,32 +156,6 @@ protected function currentUser() { return \Drupal::currentUser(); } - /** - * Gets the URL generator. - * - * @return \Drupal\Core\Routing\UrlGeneratorInterface - * The URL generator. - */ - protected function urlGenerator() { - if (!$this->urlGenerator) { - $this->urlGenerator = \Drupal::urlGenerator(); - } - return $this->urlGenerator; - } - - /** - * Sets the URL generator. - * - * @param \Drupal\Core\Routing\UrlGeneratorInterface - * The URL generator. - * - * @return $this - */ - public function setUrlGenerator(UrlGeneratorInterface $url_generator) { - $this->urlGenerator = $url_generator; - return $this; - } - /** * Returns the service container. * diff --git a/core/lib/Drupal/Core/Routing/LinkGeneratorTrait.php b/core/lib/Drupal/Core/Routing/LinkGeneratorTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..4dda543ce03fb07d6b46f6504154d1dc5eede9c7 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/LinkGeneratorTrait.php @@ -0,0 +1,69 @@ +<?php + +/** + * @file Contains Drupal\Core\Routing\LinkGeneratorTrait. + */ + +namespace Drupal\Core\Routing; + + +use Drupal\Core\Utility\LinkGeneratorInterface; +use Drupal\Core\Routing\UrlGeneratorInterface; + +/** + * Wrapper methods for the Link Generator. + * + * This utility trait should only be used in application-level code, such as + * classes that would implement ContainerInjectionInterface. Services registered + * in the Container should not use this trait but inject the appropriate service + * directly for easier testing. + */ +trait LinkGeneratorTrait { + + /** + * The link generator. + * + * @var \Drupal\Core\Utility\LinkGeneratorInterface + */ + protected $linkGenerator; + + /** + * Renders a link to a route given a route name and its parameters. + * + * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details + * on the arguments, usage, and possible exceptions. + * + * @return string + * An HTML string containing a link to the given route and parameters. + */ + protected function l($text, $route_name, array $parameters = array(), array $options = array()) { + return $this->getLinkGenerator()->generate($text, $route_name, $parameters, $options); + } + + /** + * Returns the link generator. + * + * @return \Drupal\Core\Utility\LinkGeneratorInterface + * The link generator + */ + protected function getLinkGenerator() { + if (!isset($this->linkGenerator)) { + $this->linkGenerator = \Drupal::service('link_generator'); + } + return $this->linkGenerator; + } + + /** + * Sets the link generator service. + * + * @param \Drupal\Core\Utility\LinkGeneratorInterface $generator + * The link generator service. + * + * @return $this + */ + public function setLinkGenerator(LinkGeneratorInterface $generator) { + $this->linkGenerator = $generator; + + return $this; + } +} diff --git a/core/lib/Drupal/Core/Routing/UrlGeneratorTrait.php b/core/lib/Drupal/Core/Routing/UrlGeneratorTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..a6ed3bdc8039e9a8d0a8726a0fbd65fe7c1d0c2d --- /dev/null +++ b/core/lib/Drupal/Core/Routing/UrlGeneratorTrait.php @@ -0,0 +1,69 @@ +<?php + +/** + * @file Contains Drupal\Core\Routing\LinkGeneratorTrait. + */ + +namespace Drupal\Core\Routing; + + +use Drupal\Core\Utility\LinkGeneratorInterface; +use Drupal\Core\Routing\UrlGeneratorInterface; + +/** + * Wrapper methods for the Url Generator. + * + * This utility trait should only be used in application-level code, such as + * classes that would implement ContainerInjectionInterface. Services registered + * in the Container should not use this trait but inject the appropriate service + * directly for easier testing. + */ +trait UrlGeneratorTrait { + + /** + * The url generator. + * + * @var \Drupal\Core\Routing\UrlGeneratorInterface + */ + protected $urlGenerator; + + /** + * Generates a URL or path for a specific route based on the given parameters. + * + * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for + * details on the arguments, usage, and possible exceptions. + * + * @return string + * The generated URL for the given route. + */ + protected function url($route_name, $route_parameters = array(), $options = array()) { + return $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options); + } + + /** + * Returns the URL generator service. + * + * @return \Drupal\Core\Routing\UrlGeneratorInterface + * The URL generator service. + */ + protected function getUrlGenerator() { + if (!$this->urlGenerator) { + $this->urlGenerator = \Drupal::service('url_generator'); + } + return $this->urlGenerator; + } + + /** + * Sets the URL generator service. + * + * @param \Drupal\Core\Routing\UrlGeneratorInterface $generator + * The url generator service. + * + * @return $this + */ + public function setUrlGenerator(UrlGeneratorInterface $generator) { + $this->linkGenerator = $generator; + + return $this; + } +} diff --git a/core/modules/aggregator/src/Controller/AggregatorController.php b/core/modules/aggregator/src/Controller/AggregatorController.php index d590b082b301cee6667973ea7acdf1514dadfe73..648f8eb2c545c15cf05d414b4ca427d4ad103659 100644 --- a/core/modules/aggregator/src/Controller/AggregatorController.php +++ b/core/modules/aggregator/src/Controller/AggregatorController.php @@ -152,7 +152,7 @@ public function adminOverview() { '#type' => 'table', '#header' => $header, '#rows' => $rows, - '#empty' => $this->t('No feeds available. <a href="@link">Add feed</a>.', array('@link' => $this->urlGenerator()->generate('aggregator.feed_add'))), + '#empty' => $this->t('No feeds available. <a href="@link">Add feed</a>.', array('@link' => $this->url('aggregator.feed_add'))), ); return $build; diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php index b56600d62869166f94a851a9f6b8c0359fce94b3..a20335ec565e10a651fd7c0d00f18d97ba01a9b7 100644 --- a/core/modules/comment/src/Controller/CommentController.php +++ b/core/modules/comment/src/Controller/CommentController.php @@ -293,7 +293,7 @@ public function renderNewCommentsNodeLinks(Request $request) { $query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node); $links[$nid] = array( 'new_comment_count' => (int) $new, - 'first_new_comment_link' => $this->urlGenerator()->generateFromPath('node/' . $node->id(), array('query' => $query, 'fragment' => 'new')), + 'first_new_comment_link' => $this->getUrlGenerator()->generateFromPath('node/' . $node->id(), array('query' => $query, 'fragment' => 'new')), ); } diff --git a/core/modules/contact/src/Controller/ContactController.php b/core/modules/contact/src/Controller/ContactController.php index d5db7e3d868c49b49664c8df4f80c8ba431215dd..df720bc321ff511ac20c3c48354511b4f9a7cdf7 100644 --- a/core/modules/contact/src/Controller/ContactController.php +++ b/core/modules/contact/src/Controller/ContactController.php @@ -75,7 +75,7 @@ public function contactSitePage(CategoryInterface $contact_category = NULL) { if (empty($contact_category)) { if ($this->currentUser()->hasPermission('administer contact forms')) { drupal_set_message($this->t('The contact form has not been configured. <a href="@add">Add one or more categories</a> to the form.', array( - '@add' => $this->urlGenerator()->generateFromRoute('contact.category_add'))), 'error'); + '@add' => $this->url('contact.category_add'))), 'error'); return array(); } else { diff --git a/core/modules/locale/src/Controller/LocaleController.php b/core/modules/locale/src/Controller/LocaleController.php index 36e693887acec7efd3590466121ed52f171f541c..eb25c4b50c3a176feedfa2fd7b6bb579bfe7e0a3 100644 --- a/core/modules/locale/src/Controller/LocaleController.php +++ b/core/modules/locale/src/Controller/LocaleController.php @@ -38,7 +38,7 @@ public function checkTranslation() { } // @todo Use $this->redirect() after https://drupal.org/node/1978926. - return new RedirectResponse($this->urlGenerator()->generateFromPath('admin/reports/translations', array('absolute' => TRUE))); + return new RedirectResponse($this->getUrlGenerator()->generateFromPath('admin/reports/translations', array('absolute' => TRUE))); } /** diff --git a/core/modules/shortcut/src/Form/SetCustomize.php b/core/modules/shortcut/src/Form/SetCustomize.php index 716f7eae4c8db320b097e8d623741f87b8c0506d..a306f505a33fdb7abfdb07bd88ff493b9fb65410 100644 --- a/core/modules/shortcut/src/Form/SetCustomize.php +++ b/core/modules/shortcut/src/Form/SetCustomize.php @@ -37,7 +37,7 @@ public function form(array $form, array &$form_state) { $form['shortcuts']['links'] = array( '#type' => 'table', '#header' => array(t('Name'), t('Weight'), t('Operations')), - '#empty' => $this->t('No shortcuts available. <a href="@link">Add a shortcut</a>', array('@link' => $this->urlGenerator()->generateFromRoute('shortcut.link_add', array('shortcut_set' => $this->entity->id())))), + '#empty' => $this->t('No shortcuts available. <a href="@link">Add a shortcut</a>', array('@link' => $this->url('shortcut.link_add', array('shortcut_set' => $this->entity->id())))), '#attributes' => array('id' => 'shortcuts'), '#tabledrag' => array( array( diff --git a/core/modules/system/src/Form/ModulesListConfirmForm.php b/core/modules/system/src/Form/ModulesListConfirmForm.php index 298b8ae8f3ca6eb078c85ad171478aa1efceed71..ceb78cb4fa634d74bec1f8637f32f3476bca4e95 100644 --- a/core/modules/system/src/Form/ModulesListConfirmForm.php +++ b/core/modules/system/src/Form/ModulesListConfirmForm.php @@ -107,7 +107,7 @@ public function buildForm(array $form, array &$form_state) { // Redirect to the modules list page if the key value store is empty. if (!$this->modules) { - return new RedirectResponse($this->urlGenerator()->generate('system.modules_list', array(), TRUE)); + return new RedirectResponse($this->url('system.modules_list', [], ['absolute' => TRUE])); } $items = array(); diff --git a/core/modules/user/src/Form/UserMultipleCancelConfirm.php b/core/modules/user/src/Form/UserMultipleCancelConfirm.php index 129152aec4145b1b31eae91247d92d4863f895b4..4ad6e2b77079fa03800ffd9b0139af950208a9a2 100644 --- a/core/modules/user/src/Form/UserMultipleCancelConfirm.php +++ b/core/modules/user/src/Form/UserMultipleCancelConfirm.php @@ -105,7 +105,7 @@ public function buildForm(array $form, array &$form_state) { ->get('user_user_operations_cancel') ->get($this->currentUser()->id()); if (!$accounts) { - return new RedirectResponse($this->urlGenerator()->generateFromPath('admin/people', array('absolute' => TRUE))); + return new RedirectResponse($this->url('user.admin_account', [], ['absolute' => TRUE])); } $form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE); @@ -129,7 +129,7 @@ public function buildForm(array $form, array &$form_state) { drupal_set_message($message, $redirect ? 'error' : 'warning'); // If only user 1 was selected, redirect to the overview. if ($redirect) { - return new RedirectResponse($this->urlGenerator()->generateFromPath('admin/people', array('absolute' => TRUE))); + return new RedirectResponse($this->url('user.admin_account', [], ['absolute' => TRUE])); } }