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

8.0.x
Alex Pott 2014-07-07 14:52:01 +01:00
parent 8bc9c2e524
commit c072353df4
12 changed files with 159 additions and 148 deletions

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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.
*

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -152,7 +152,7 @@ class AggregatorController extends ControllerBase {
'#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;

View File

@ -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')),
);
}

View File

@ -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. <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 {

View File

@ -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)));
}
/**

View File

@ -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. <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(

View File

@ -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();

View File

@ -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' => '<ul>', '#suffix' => '</ul>', '#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]));
}
}