diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php index a88944a64fc..0c198da65a9 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 488e982c3c8..62fab4a1e64 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 @@ use Symfony\Component\HttpFoundation\RedirectResponse; */ 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 @@ abstract class ControllerBase implements ContainerInjectionInterface { 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 @@ abstract class ControllerBase implements ContainerInjectionInterface { * 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 b94a3ed2cd3..751ad9f6a3d 100644 --- a/core/lib/Drupal/Core/Form/FormBase.php +++ b/core/lib/Drupal/Core/Form/FormBase.php @@ -10,7 +10,8 @@ namespace Drupal\Core\Form; 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 @@ use Symfony\Component\HttpFoundation\Request; 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 @@ abstract class FormBase implements FormInterface, ContainerInjectionInterface { // 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 @@ abstract class FormBase implements FormInterface, ContainerInjectionInterface { 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 00000000000..4dda543ce03 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/LinkGeneratorTrait.php @@ -0,0 +1,69 @@ +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 00000000000..a6ed3bdc803 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/UrlGeneratorTrait.php @@ -0,0 +1,69 @@ +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 d590b082b30..648f8eb2c54 100644 --- a/core/modules/aggregator/src/Controller/AggregatorController.php +++ b/core/modules/aggregator/src/Controller/AggregatorController.php @@ -152,7 +152,7 @@ class AggregatorController extends ControllerBase { '#type' => 'table', '#header' => $header, '#rows' => $rows, - '#empty' => $this->t('No feeds available. Add feed.', array('@link' => $this->urlGenerator()->generate('aggregator.feed_add'))), + '#empty' => $this->t('No feeds available. Add feed.', 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 b56600d6286..a20335ec565 100644 --- a/core/modules/comment/src/Controller/CommentController.php +++ b/core/modules/comment/src/Controller/CommentController.php @@ -293,7 +293,7 @@ class CommentController extends ControllerBase { $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 d5db7e3d868..df720bc321f 100644 --- a/core/modules/contact/src/Controller/ContactController.php +++ b/core/modules/contact/src/Controller/ContactController.php @@ -75,7 +75,7 @@ class ContactController extends ControllerBase { if (empty($contact_category)) { if ($this->currentUser()->hasPermission('administer contact forms')) { drupal_set_message($this->t('The contact form has not been configured. Add one or more categories 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 36e693887ac..eb25c4b50c3 100644 --- a/core/modules/locale/src/Controller/LocaleController.php +++ b/core/modules/locale/src/Controller/LocaleController.php @@ -38,7 +38,7 @@ class LocaleController extends ControllerBase { } // @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 716f7eae4c8..a306f505a33 100644 --- a/core/modules/shortcut/src/Form/SetCustomize.php +++ b/core/modules/shortcut/src/Form/SetCustomize.php @@ -37,7 +37,7 @@ class SetCustomize extends EntityForm { $form['shortcuts']['links'] = array( '#type' => 'table', '#header' => array(t('Name'), t('Weight'), t('Operations')), - '#empty' => $this->t('No shortcuts available. Add a shortcut', array('@link' => $this->urlGenerator()->generateFromRoute('shortcut.link_add', array('shortcut_set' => $this->entity->id())))), + '#empty' => $this->t('No shortcuts available. Add a shortcut', 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 298b8ae8f3c..ceb78cb4fa6 100644 --- a/core/modules/system/src/Form/ModulesListConfirmForm.php +++ b/core/modules/system/src/Form/ModulesListConfirmForm.php @@ -107,7 +107,7 @@ class ModulesListConfirmForm extends ConfirmFormBase { // 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 129152aec41..4ad6e2b7707 100644 --- a/core/modules/user/src/Form/UserMultipleCancelConfirm.php +++ b/core/modules/user/src/Form/UserMultipleCancelConfirm.php @@ -105,7 +105,7 @@ class UserMultipleCancelConfirm extends ConfirmFormBase { ->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' => '', '#tree' => TRUE); @@ -129,7 +129,7 @@ class UserMultipleCancelConfirm extends ConfirmFormBase { 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])); } }