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.

Files: 
CommentFileSizeAuthor
#12 drupal-url-2078285-12.patch1.06 KBtim.plunkett
PASSED: [[SimpleTest]]: [MySQL] 59,108 pass(es).
[ View ]
#7 drupal-l-2078285-7.patch9.66 KBpwolanin
PASSED: [[SimpleTest]]: [MySQL] 58,093 pass(es).
[ View ]
#7 2078285-5-7.increment.txt3.74 KBpwolanin
#5 2078285-4-5.increment.txt5.51 KBpwolanin
#5 drupal-l-2078285-5.patch9.37 KBpwolanin
PASSED: [[SimpleTest]]: [MySQL] 58,030 pass(es).
[ View ]
#4 drupal-link_generator-2078285-2.patch5.06 KBdawehner
PASSED: [[SimpleTest]]: [MySQL] 58,448 pass(es).
[ View ]
#3 2078285-2.patch1.28 KBpwolanin
PASSED: [[SimpleTest]]: [MySQL] 58,435 pass(es).
[ View ]
#3 2078285-1-2.increment.txt658 bytespwolanin
#1 2078285-1.patch1.27 KBpwolanin
PASSED: [[SimpleTest]]: [MySQL] 58,378 pass(es).
[ View ]

Comments

Title:Add short-cut methods to the \Drupal class for the url generator and link generatorAdd short-cut methods to the \Drupal class for generating URLs and links from routes
Status:Active» Needs review
StatusFileSize
new1.27 KB
PASSED: [[SimpleTest]]: [MySQL] 58,378 pass(es).
[ View ]

StatusFileSize
new658 bytes
new1.28 KB
PASSED: [[SimpleTest]]: [MySQL] 58,435 pass(es).
[ View ]

missing text for the link...

Issue tags:+phpunit
StatusFileSize
new5.06 KB
PASSED: [[SimpleTest]]: [MySQL] 58,448 pass(es).
[ View ]

+++ 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.

StatusFileSize
new9.37 KB
PASSED: [[SimpleTest]]: [MySQL] 58,030 pass(es).
[ View ]
new5.51 KB

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.

+++ 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.

StatusFileSize
new3.74 KB
new9.66 KB
PASSED: [[SimpleTest]]: [MySQL] 58,093 pass(es).
[ View ]

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

Status:Needs review» Reviewed & tested by the community

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

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.

Status:Needs work» Fixed

Actually, I can just commit it without that hunk.

Committed and pushed to 8.x. Thanks!

Category:task» bug
Priority:Normal» Major
Status:Fixed» Needs work
StatusFileSize
new1.06 KB
PASSED: [[SimpleTest]]: [MySQL] 59,108 pass(es).
[ View ]

This is broken.

Status:Needs work» Needs review

Status:Needs review» Reviewed & tested by the community

RTBC, perfect

Status:Reviewed & tested by the community» Fixed

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

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

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