Commit c072353d authored by alexpott's avatar alexpott

Issue #2282161 by Crell: Split off link/url generation trait.

parent 8bc9c2e5
......@@ -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;
}
......@@ -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);
}
}
......@@ -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.
*
......
<?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;
}
}
<?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;
}
}
......@@ -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;
......
......@@ -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')),
);
}
......
......@@ -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 {
......
......@@ -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)));
}
/**
......
......@@ -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(
......
......@@ -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();
......
......@@ -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]));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment