diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index 04fac02..0411200 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -390,6 +390,60 @@ 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::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). + * + * @return string + * An HTML string containing a link to the given route and parameters. + * + * @see \Drupal\Core\Routing\UrlGenerator::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/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); + } + +}