Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Matroskeen created an issue. See original summary.

Matroskeen’s picture

Title: url() and urlIfno() are deprecated » url() and urlInfo() are deprecated
Matroskeen’s picture

Assigned: Matroskeen » Unassigned
Status: Active » Needs review
FileSize
9.38 KB

Patch is attached.

Let's run the tests.

Status: Needs review » Needs work

The last submitted patch, 3: token-url-related-deprecates-2932272-3-D8.patch, failed testing. View results

Berdir’s picture

+++ b/src/Tests/TokenCurrentPageTest.php
@@ -43,13 +43,13 @@ class TokenCurrentPageTest extends TokenTestBase {
-      '[current-page:url:absolute]' => $node->url('canonical', array('absolute' => TRUE)),
-      '[current-page:url:relative]' => $node->url(),
+      '[current-page:url]' => $node->toUrl('canonical', array('absolute' => TRUE)),
+      '[current-page:url:absolute]' => $node->toUrl('canonical', array('absolute' => TRUE)),
+      '[current-page:url:relative]' => $node->toUrl(),

url() returns a string and falls back to empty if not set. toUrl() returns a Url object on which you need to call toString() and it can throw an exception. Here in the tests not an issue but we need to be careful with the actual hook implementations.

Matroskeen’s picture

Status: Needs work » Needs review
FileSize
9.46 KB

Thanks for the review, I didn't notice that.

Updated patch is attached. Some local tests were failed but they look not related to these changes.

The last submitted patch, 3: token-url-related-deprecates-2932272-3-D8.patch, failed testing. View results

Berdir’s picture

Status: Needs review » Needs work
+++ b/token.tokens.inc
@@ -854,7 +854,7 @@ function token_tokens($type, array $tokens, array $data = array(), array $option
       switch ($name) {
         case 'url':
-          if (_token_module($type, 'url') == 'token' && $url = $entity->url()) {
+          if (_token_module($type, 'url') == 'token' && $url = $entity->toUrl()->toString()) {
             $replacements[$original] = $url;

Do we already only expose the url token for entity types that have a canonical link template? If not then we should add a hasLinkTemplate('canonical') around it to prevent an exception when using that on an entity type that has no canonical route. And possibly also an !$entity->isNew(), imagine a node:url token being used on a node preview for example.

Matroskeen’s picture

Status: Needs work » Needs review
FileSize
0 bytes

I tested both scenarios (empty link template and new entity) with my custom entity.
I can confirm that in both we will have an exception.

Here is an updated patch with extra conditions.

I also passed 'canonical' to toUrl() method because entity types may override it and set another default value.

Matroskeen’s picture

The last submitted patch, 9: token-url-related-deprecates-2932272-9-D8.patch, failed testing. View results

Berdir’s picture

Status: Needs review » Fixed

Thanks, committed.

  • Berdir committed 7024b05 on 8.x-1.x authored by Matroskeen
    Issue #2932272 by Matroskeen: url() and urlInfo() are deprecated
    

Status: Fixed » Closed (fixed)

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