Since we now have #2047619: Add a link generator service for route-based links , we should provide a short-cut that's nearly as easy for developers to use as l() and url() for generating a link or url from a route and parameters.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pwolanin’s picture

Title: Add short-cut methods to the \Drupal class for the url generator and link generator » Add short-cut methods to the \Drupal class for generating URLs and links from routes
Status: Active » Needs review
FileSize
1.27 KB
pwolanin’s picture

pwolanin’s picture

FileSize
658 bytes
1.28 KB

missing text for the link...

dawehner’s picture

Issue tags: +PHPUnit
FileSize
5.06 KB

+++ b/core/lib/Drupal.php
@@ -381,6 +381,17 @@ public static function urlGenerator() {
+ public static function url($route_name, $rotue_parameters = array(), $options = array()) {
+ return static::$container->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options);

@@ -390,6 +401,17 @@ public static function linkGenerator() {
+ public static function l($route_name, $rotue_parameters = array(), $options = array()) {
+ return static::$container->get('link_generator')->generate($route_name, $route_parameters, $options);

I tried to find funny images of rotue but I couldn't find one :(

Let's do the url() bits into #2073813: Add a UrlGenerator helper to FormBase and ControllerBase and concentrate on the link generator here.

Yes this unit test is kind of pointless, but nevertheless I want to show people that they should use unit tests where possible.

pwolanin’s picture

Since Drupal::url() doesn't' seem to be in the FormBase patch, adding it back here plus more doxygen and oxygen fixes.

I thin a DX question here is whether we want to catch some or all of the possible exceptions - e.g. in the way url() does if the generator is not initialized.

dawehner’s picture

+++ b/core/lib/Drupal.php
@@ -381,6 +381,49 @@ public static function urlGenerator() {
+   * @throws RouteNotFoundException              If the named route doesn't exist
+   * @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
+   * @throws InvalidParameterException           When a parameter value for a placeholder is not correct because
+   *                                             it does not match the requirement
+   *

@@ -390,6 +433,65 @@ public static function linkGenerator() {
+   *
+   * @throws RouteNotFoundException              If the named route doesn't exist
+   * @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
+   * @throws InvalidParameterException           When a parameter value for a placeholder is not correct because
+   *                                             it does not match the requirement
+   *

+++ b/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php
@@ -59,6 +59,11 @@
    *
+   * @throws RouteNotFoundException              If the named route doesn't exist
+   * @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
+   * @throws InvalidParameterException           When a parameter value for a placeholder is not correct because
+   *                                             it does not match the requirement
+   *

Please copy the docs from the other issue, they are alligned properly.

pwolanin’s picture

@dawehner - ha, indeed - when I looked at your other patch I figured we'd need to copy that version to here.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Well we don't test the url() methods but I don't really care.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

I'm guessing return static::$container->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options); is already tested elsewhere. If so, I don't see an overly compelling reason to test a wrapper function. Especially only one of them and not both.

-   * 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')));

That fills my soul with happiness tho.

webchick’s picture

Status: Needs work » Fixed

Actually, I can just commit it without that hunk.

Committed and pushed to 8.x. Thanks!

pwolanin’s picture

tim.plunkett’s picture

Category: task » bug
Priority: Normal » Major
Status: Fixed » Needs work
FileSize
1.06 KB

This is broken.

tim.plunkett’s picture

Status: Needs work » Needs review
brianV’s picture

Status: Needs review » Reviewed & tested by the community

RTBC, perfect

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 21eaf37 and pushed to 8.x. Thanks!

damiankloip’s picture

In light of that, I opened #2089787: Unit test the \Drupal class.

Automatically closed -- issue fixed for 2 weeks with no activity.