Problem/Motivation

The Token module provides a [commerce_order:url] token. The problem is that this goes to the canonical URL:

        case 'url':
          if (_token_module($type, 'url') === 'token' && !$entity->isNew() && $entity->hasLinkTemplate('canonical')) {
            $replacements[$original] = $entity->toUrl('canonical')->toString();
          }

For orders, the canonical URL is the administrative page.

Proposed resolution

We should adjust this so that the url token goes to user view. We should then provide a new admin-url token for linking to the admin page.

Remaining tasks

  • Review if we can modify the default [commerce_order:url] token
  • What did 1.x do? Avoid [commerce_order:url]?
  • Do it

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#7 3156489-7.patch4.36 KBmglaman
#4 3156489-4.patch4.43 KBmglaman
#3 3156489-3.patch4.42 KBmglaman

Issue fork commerce-3156489

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

  • 3156489-url-tokens Comparecompare

Comments

mglaman created an issue. See original summary.

mglaman’s picture

Okay, this is feasible. Token adds the URL tokens via an alter:

token_token_info_alter

    // Add [entity:url] tokens if they do not already exist.
    // @todo Support entity:label
    if (!isset($info['tokens'][$token_type]['url'])) {
      $info['tokens'][$token_type]['url'] = [
        'name' => t('URL'),
        'description' => t('The URL of the @entity.', ['@entity' => mb_strtolower($entity_info->getLabel())]),
        'module' => 'token',
        'type' => 'url',
      ];
    }
mglaman’s picture

Status: Active » Needs review
StatusFileSize
new4.42 KB

Here are the patch and test!

The assertion:

    yield [
      '[commerce_order:url]',
      'http://localhost/user/456/orders/123',
    ];
    yield [
      '[commerce_order:admin-url]',
      'http://localhost/admin/commerce/orders/123',
    ];

  1. +++ b/modules/order/tests/src/Kernel/OrderTokensTest.php
    @@ -0,0 +1,70 @@
    +class OrderTokensTest extends OrderKernelTestBase {
    

    Forgot to define @group

  2. +++ b/modules/order/tests/src/Kernel/OrderTokensTest.php
    @@ -0,0 +1,70 @@
    +    $token_options = ['langcode' => 'en'];
    

    This was added when the order was mocked. It's actually not required, anymore.

mglaman’s picture

StatusFileSize
new4.43 KB

Fixed the notes in #3.

Status: Needs review » Needs work

The last submitted patch, 4: 3156489-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mglaman’s picture

Looks like providing this broke the Token integration:

You will pay for order #[commerce_order:order_id] in USD.

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new4.36 KB
+++ b/modules/order/commerce_order.tokens.inc
@@ -0,0 +1,76 @@
+    'module' => 'commerce_order',

We need to keep the module at token for Token to keep generating things for us. In fact, we can actually drop defining the type and let Token continue to do that.

recidive’s picture

Looks good to me, though I haven't tested the patch.

mglaman’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @recidive. I'm going to merge and unblock #3155132: Allow customizing the checkout completion message from the user interface. We've got plenty of time before the next release to adjust any changes for this issue.

  • mglaman committed fb4eec1 on 8.x-2.x
    Issue #3156489 by mglaman, recidive: Provide an `admin-url` token for...
mglaman’s picture

Status: Reviewed & tested by the community » Fixed

Committed!

Status: Fixed » Closed (fixed)

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