From 95c2e17ca1bfe104c56f02384893b9d177c6a347 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 5 Sep 2013 21:40:11 +0200
Subject: [PATCH] Issue #2073813 by pwolanin, dawehner, tstoeckler, twistor:
 Add a UrlGenerator helper to FormBase and ControllerBase.

---
 .../Drupal/Core/Controller/ControllerBase.php | 69 ++++++-------------
 core/lib/Drupal/Core/Form/FormBase.php        | 44 ++++++++++++
 .../Drupal/search/Form/SearchSettingsForm.php |  2 +-
 .../Controller/ShortcutSetController.php      |  2 +-
 4 files changed, 67 insertions(+), 50 deletions(-)

diff --git a/core/lib/Drupal/Core/Controller/ControllerBase.php b/core/lib/Drupal/Core/Controller/ControllerBase.php
index 088127193ef5..aaa6b33ccf9f 100644
--- a/core/lib/Drupal/Core/Controller/ControllerBase.php
+++ b/core/lib/Drupal/Core/Controller/ControllerBase.php
@@ -112,10 +112,10 @@ protected function moduleHandler() {
   }
 
   /**
-   * 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 @@ protected function urlGenerator() {
   /**
    * 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 @@ protected function languageManager() {
    *
    * @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);
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php
index 0b677cd699f3..9c3c89034f29 100644
--- a/core/lib/Drupal/Core/Form/FormBase.php
+++ b/core/lib/Drupal/Core/Form/FormBase.php
@@ -9,6 +9,7 @@
 
 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 @@ protected function t($string, array $args = array(), array $options = array()) {
     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 @@ protected function getCurrentUser() {
     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;
+  }
+
 }
diff --git a/core/modules/search/lib/Drupal/search/Form/SearchSettingsForm.php b/core/modules/search/lib/Drupal/search/Form/SearchSettingsForm.php
index eb6389497ddd..5f7d1da23bf0 100644
--- a/core/modules/search/lib/Drupal/search/Form/SearchSettingsForm.php
+++ b/core/modules/search/lib/Drupal/search/Form/SearchSettingsForm.php
@@ -126,7 +126,7 @@ public function buildForm(array $form, array &$form_state) {
       '#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(
diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutSetController.php b/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutSetController.php
index 0e275f7350bb..7123ae57e20b 100644
--- a/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutSetController.php
+++ b/core/modules/shortcut/lib/Drupal/shortcut/Controller/ShortcutSetController.php
@@ -50,7 +50,7 @@ public function addShortcutLinkInline(ShortcutSetInterface $shortcut_set, Reques
       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();
-- 
GitLab