Issue #2073813 by pwolanin, dawehner, tstoeckler, twistor: Add a UrlGenerator helper to FormBase and ControllerBase.

8.0.x
Alex Pott 2013-09-05 21:40:11 +02:00
parent 34dec4b76a
commit 95c2e17ca1
4 changed files with 67 additions and 50 deletions

View File

@ -112,10 +112,10 @@ abstract class ControllerBase extends ContainerAware {
}
/**
* Returns the url generator service.
* Returns the URL generator service.
*
* @return \Drupal\Core\Routing\UrlGeneratorInterface
* The url generator service.
* The URL generator service.
*/
protected function urlGenerator() {
return $this->container->get('url_generator');
@ -124,52 +124,11 @@ abstract class ControllerBase extends ContainerAware {
/**
* Renders a link to a route given a route name and its parameters.
*
* This function correctly handles aliased paths and sanitizing text, so all
* internal links output by modules should be generated by this function if
* possible.
*
* However, for links enclosed in translatable text you should use t() and
* embed the HTML anchor tag directly in the translated string. For example:
* @code
* t('Visit the <a href="@url">content types</a> page', array('@url' => Drupal::urlGenerator()->generate('node_overview_types')));
* @endcode
* This keeps the context of the link title ('settings' in the example) for
* translators.
*
* @param string|array $text
* The link text for the anchor tag as a translated string or render array.
* @param string $route_name
* The name of the route to use to generate the link.
* @param array $parameters
* (optional) Any parameters needed to render the route path pattern.
* @param array $options
* (optional) An associative array of additional options. Defaults to an
* empty array. It may contain the following elements:
* - 'query': An array of query key/value-pairs (without any URL-encoding) to
* append to the URL.
* - absolute: Whether to force the output to be an absolute link (beginning
* with http:). Useful for links that will be displayed outside the site,
* such as in an RSS feed. Defaults to FALSE.
* - attributes: An associative array of HTML attributes to apply to the
* anchor tag. If element 'class' is included, it must be an array; 'title'
* must be a string; other elements are more flexible, as they just need
* to work as an argument for the constructor of the class
* Drupal\Core\Template\Attribute($options['attributes']).
* - html: Whether $text is HTML or just plain-text. For
* example, to make an image tag into a link, this must be set to TRUE, or
* you will see the escaped HTML image tag. $text is not sanitized if
* 'html' is TRUE. The calling function must ensure that $text is already
* safe. Defaults to FALSE.
* - language: An optional language object. If the path being linked to is
* internal to the site, $options['language'] is used to determine whether
* the link is "active", or pointing to the current page (the language as
* well as the path must match).
* @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.
*
* @see \Drupal\Core\Routing\UrlGenerator::generateFromRoute()
* @see \Drupal\Core\Utility\LinkGenerator::generate()
*/
public function l($text, $route_name, array $parameters = array(), array $options = array()) {
return $this->container->get('link_generator')->generate($text, $route_name, $parameters, $options);
@ -209,15 +168,29 @@ abstract class ControllerBase extends ContainerAware {
*
* @param string $route_name
* The name of the route to which to redirect.
* @param array $parameters
* @param array $route_parameters
* Parameters for the route.
* @param int $status
* The HTTP redirect status code for the redirect. The default is 302 Found.
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*/
public function redirect($route_name, array $parameters = array(), $status = 302) {
$url = $this->container->get('url_generator')->generate($route_name, $parameters, TRUE);
public function redirect($route_name, array $route_parameters = array(), $status = 302) {
$url = $this->container->get('url_generator')->generate($route_name, $route_parameters, 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->container->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options);
}
}

View File

@ -9,6 +9,7 @@ namespace Drupal\Core\Form;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\DependencyInjection\DependencySerialization;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
@ -32,6 +33,13 @@ abstract class FormBase extends DependencySerialization implements FormInterface
*/
protected $request;
/**
* The URL generator.
*
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* {@inheritdoc}
*/
@ -55,6 +63,19 @@ abstract class FormBase extends DependencySerialization implements FormInterface
return $this->getTranslationManager()->translate($string, $args, $options);
}
/**
* 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->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
}
/**
* Gets the translation manager.
*
@ -115,4 +136,27 @@ abstract class FormBase extends DependencySerialization implements FormInterface
return $this->getRequest()->attributes->get('_account');
}
/**
* Gets the URL generator.
*
* @return \Drupal\Core\Routing\UrlGeneratorInterface
* The URL generator.
*/
protected function getUrlGenerator() {
if (!$this->urlGenerator) {
$this->urlGenerator = \Drupal::urlGenerator();
}
return $this->urlGenerator;
}
/**
* Sets the URL generator.
*
* @param \Drupal\Core\Routing\UrlGeneratorInterface
* The URL generator.
*/
public function setUrlGenerator(UrlGeneratorInterface $url_generator) {
$this->urlGenerator = $url_generator;
}
}

View File

@ -126,7 +126,7 @@ class SearchSettingsForm extends SystemConfigFormBase {
'#title' => t('Number of items to index per cron run'),
'#default_value' => $config->get('index.cron_limit'),
'#options' => $items,
'#description' => t('The maximum number of items indexed in each pass of a <a href="@cron">cron maintenance task</a>. If necessary, reduce the number of items to prevent timeouts and memory errors while indexing.', array('@cron' => url('admin/reports/status')))
'#description' => t('The maximum number of items indexed in each pass of a <a href="@cron">cron maintenance task</a>. If necessary, reduce the number of items to prevent timeouts and memory errors while indexing.', array('@cron' => $this->url('system_status')))
);
// Indexing settings:
$form['indexing_settings'] = array(

View File

@ -50,7 +50,7 @@ class ShortcutSetController extends ControllerBase {
else {
drupal_set_message(t('Unable to add a shortcut for %title.', array('%title' => $link['link_title'])));
}
return new RedirectResponse($this->urlGenerator()->generateFromPath('<front>', array('absolute' => TRUE)));
return $this->redirect('<front>');
}
throw new AccessDeniedHttpException();