diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index a0d61ee..6ef0b5d 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -381,6 +381,52 @@ public static function urlGenerator() { } /** + * 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. * * @return \Drupal\Core\Utility\LinkGeneratorInterface @@ -390,6 +436,68 @@ public static function linkGenerator() { } /** + * 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 content types 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. * * @return \Drupal\Core\StringTranslation\TranslationManager diff --git a/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php b/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php index 2157a5f..dba34cd 100644 --- a/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php +++ b/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php @@ -22,7 +22,7 @@ * 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 content types page', array('@url' => Drupal::urlGenerator()->generate('node_overview_types'))); + * t('Visit the content types 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 @@ * @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()); diff --git a/core/tests/Drupal/Tests/DrupalTest.php b/core/tests/Drupal/Tests/DrupalTest.php new file mode 100644 index 0000000..77ea6e4 --- /dev/null +++ b/core/tests/Drupal/Tests/DrupalTest.php @@ -0,0 +1,70 @@ + 'Drupal class', + 'description' => 'Tests the drupal class.', + 'group' => 'Drupal', + ); + } + + protected function setUp() { + $this->container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container') + ->disableOriginalConstructor() + ->getMock(); + $this->drupal = new Drupal(); + $this->drupal->setContainer($this->container); + } + + /** + * Tests the l method. + * + * @see \Drupal::l() + */ + public function testL() { + $link_generator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface'); + $link = 'test_text'; + $link_generator->expects($this->once()) + ->method('generate') + ->with('test_text', 'test_route', array('key' => 'value'), array('html' => TRUE)) + ->will($this->returnValue($link)); + + $this->container->expects($this->once()) + ->method('get') + ->will($this->returnValue($link_generator)); + + $result = $this->drupal->l('test_text', 'test_route', array('key' => 'value'), array('html' => TRUE)); + $this->assertEquals($link, $result); + } + +}