Commit 675cade2 authored by webchick's avatar webchick

Issue #2078285 by pwolanin, dawehner: Add short-cut methods to the \Drupal...

Issue #2078285 by pwolanin, dawehner: Add short-cut methods to the \Drupal class for generating URLs and links from routes.
parent c2741dbc
......@@ -380,6 +380,52 @@ public static function urlGenerator() {
return static::$container->get('url_generator');
}
/**
* Generates a URL or path for a specific route based on the given parameters.
*
* Parameters that reference placeholders in the route pattern will be
* substituted for them in the pattern. Extra params are added as query
* strings to the URL.
*
* @param string $name
* The name of the route
* @param array $parameters
* An associative array of parameter names and values.
* @param array $options
* (optional) An associative array of additional options, with the following
* elements:
* - 'query': An array of query key/value-pairs (without any URL-encoding)
* to append to the URL. Merged with the parameters array.
* - 'fragment': A fragment identifier (named anchor) to append to the URL.
* Do not include the leading '#' character.
* - 'absolute': Defaults to FALSE. 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.
* - 'language': An optional language object used to look up the alias
* for the URL. If $options['language'] is omitted, the language will be
* obtained from language(Language::TYPE_URL).
* - 'https': Whether this URL should point to a secure location. If not
* defined, the current scheme is used, so the user stays on HTTP or HTTPS
* respectively. if mixed mode sessions are permitted, TRUE enforces HTTPS
* and FALSE enforces HTTP.
*
* @return string
* The generated URL for the given route.
*
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
* Thrown when the named route doesn't exist.
* @throws \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
* Thrown when some parameters are missing that are mandatory for the route.
* @throws \Symfony\Component\Routing\Exception\InvalidParameterException
* Thrown when a parameter value for a placeholder is not correct because it
* does not match the requirement.
*
* @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
*/
public static function url($route_name, $rotue_parameters = array(), $options = array()) {
return static::$container->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options);
}
/**
* Returns the link generator service.
*
......@@ -389,6 +435,68 @@ public static function linkGenerator() {
return static::$container->get('link_generator');
}
/**
* 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::url('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).
*
* @return string
* An HTML string containing a link to the given route and parameters.
*
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
* Thrown when the named route doesn't exist.
* @throws \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
* Thrown when some parameters are missing that are mandatory for the route.
* @throws \Symfony\Component\Routing\Exception\InvalidParameterException
* Thrown when a parameter value for a placeholder is not correct because it
* does not match the requirement.
*
* @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
* @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
*/
public function l($text, $route_name, array $parameters = array(), array $options = array()) {
return static::$container->get('link_generator')->generate($text, $route_name, $parameters, $options);
}
/**
* Returns the string translation service.
*
......
......@@ -22,7 +22,7 @@ interface LinkGeneratorInterface {
* 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')));
* t('Visit the <a href="@url">content types</a> page', array('@url' => Drupal::url('node_overview_types')));
* @endcode
* This keeps the context of the link title ('settings' in the example) for
* translators.
......@@ -59,6 +59,14 @@ interface LinkGeneratorInterface {
* @return string
* An HTML string containing a link to the given route and parameters.
*
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
* Thrown when the named route doesn't exist.
* @throws \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
* Thrown when some parameters are missing that are mandatory for the route.
* @throws \Symfony\Component\Routing\Exception\InvalidParameterException
* Thrown when a parameter value for a placeholder is not correct because it
* does not match the requirement.
*
* @see \Drupal\Core\Routing\UrlGenerator::generateFromRoute()
*/
public function generate($text, $route_name, array $parameters = array(), array $options = array());
......
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