Now I get emails saying 'Array' for items Name, Street and City. Stopped working after updating the module.

This is my template:

Purchase date: [commerce-order:placed]

Contact details:
Email: [commerce-order:mail]
Phone: [commerce-order:commerce-customer-billing:commerce_customer_address:phone-number]

Billing:
Name: [commerce-order:commerce-customer-billing:commerce-customer-address:name-line]
Phone: [commerce-order:commerce-customer-billing:commerce_customer_address:phone-number]
Street: [commerce-order:commerce-customer-billing:commerce-customer-address:thoroughfare]
City: [commerce-order:commerce-customer-billing:commerce-customer-address:locality]
State: [commerce-order:commerce-customer-billing:commerce-customer-address:administrative_area]
ZIP: [commerce-order:commerce-customer-billing:commerce-customer-address:postal_code]

Shipping:
Name: [commerce-order:commerce-customer-shipping:commerce-customer-address:name-line]
Phone: [commerce-order:commerce-customer-shipping:commerce_customer_address:phone-number]
Street: [commerce-order:commerce-customer-shipping:commerce-customer-address:thoroughfare]
City: [commerce-order:commerce-customer-shipping:commerce-customer-address:locality]
State: [commerce-order:commerce-customer-shipping:commerce-customer-address:administrative_area]
ZIP: [commerce-order:commerce-customer-shipping:commerce-customer-address:postal_code]

<table width="100%">
  <tbody>
    <tr>
      <td colspan="2">[commerce-order:commerce_extra_tokens_order_line_items]</td>    
    </tr>
    <tr>
      <td>[commerce-order:commerce_extra_tokens_order_total]</td>
    </tr>
  </tbody>
</table>

Comments

vibrasphere created an issue. See original summary.

juves’s picture

I am having the same issue.

rszrama’s picture

It should be safe to revert while we debug.

rszrama’s picture

Can y'all confirm that you are not using the Token module on your sites? Also, can you confirm that reverting to 1.2 fixes the issue? The only relevant change that comes to mind is #2416997: Statically cache the list of address fields during token generation.

matas.k’s picture

You can use tokens without Token module? I'm using Token, Commerce Extra Tokens and Entity Tokens.

bojanz’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
Component: Address formats » Code
Category: Support request » Bug report
Priority: Normal » Major
matas.k’s picture

Yes, I can confirm that downgrading back to 7.x-1.2 works as supposed.

But then upgrading back to 1.3 also now works.

Before the downgrade there was some token error for rules module spitting out, but I don't remember what it was and I deleted the log, sorry. I don't know if it's 1.3 related.

But now it's working so I don't get it.

Reproduce:
1. Upgrade from 1.2 to 1.3 on production site with drush 'pm-update'
2. Tokens don't print on email for Name, Street, City.
3. Downgrade back to 1.2 (by removing the addressfield dir and unzipping old 1.3)
4. drush updatedb/clear caches and it works, correct Tokens printed.
5. Upgrade back to 1.3 with pm-update and now it also works.
6. ???

I'm testing checkout emails by triggering 'Simulate checkout completion' at e.g. admin/commerce/orders/51/edit

czigor’s picture

Status: Active » Needs review
StatusFileSize
new797 bytes

This fixes the issue for me and also makes the change in #2416997: Statically cache the list of address fields during token generation a smaller one.

jsacksick’s picture

Hm... that's weird, In that case, there's no need for the loop foreach (array($field_name, strtr($field_name, '_', '-')) as $name) {, but did you do some tests with / without the token module?

In the end, we still alter the $data['tokens'][$group][$name] array although we check for the $field_name key...

matas.k’s picture

Isn't token module required by Entity Tokens and Commerce Extra Tokens? How do you print tokens to email without token module?

jsacksick’s picture

@vibrasphere: Can you check if the token module is enabled on your install?

matas.k’s picture

Yes, it's enabled. Like I said it prints all tokens fine except for fields Name, Street and City. I tried this already on multiple sites.

jsacksick’s picture

So you're having issues with the token name that has dashes? ([commerce-order:commerce-customer-billing:commerce-customer-address:name-line]).

Because I'm trying that and it works for me... But it actually doesn't when the token module is disabled.

matas.k’s picture

The template I posted is working with 1.2, but not 1.3.

1.3 gives 'Array' for Name, Street and City, while all other fields and tokens work fine. And they also have dashes.

Anyway, like I said, when you upgrade to 1.3 from 1.2 and then downgrade back to 1.2 and upgrade back to 1.3 it is fixed. At least that's what I got in one site.

jsacksick’s picture

I did several tests and here are my findings:

Without the code in addressfield_token_info_alter(), printing the token [commerce-order:commerce-customer-billing:commerce-customer-address:name-line] (with dashes) returns "Array".

With the code, if the "token" module is enabled, both [commerce-order:commerce-customer-billing:commerce-customer-address:name-line] and [commerce-order:commerce-customer-billing:commerce_customer_address:name-line] work.
However, in the current state, printing [commerce-order:commerce-customer-billing:commerce-customer-address:name-line] when the token module isn't enabled doesn't return anything, the reason for that is that we change the token type from "struct" to "address-field".

If we keep the type to "struct", the "entity_token" module is actually in charge fo the token replacement...

So we actually have two options:

  1. Skip addressfield_token_info_alter() completely when the token module is disabled.
  2. Make sure the token replacement work properly in addressfield_tokens() both when the token module is disabled / enabled.

The attached patch is making sure the token replacement is performed by the Addressfield module, both when the token module is enabled / disabled.

@vibrasphere: Could you apply the attached patch, clear caches and make sure it works for you?

matas.k’s picture

It stopped showing State and Zip.

jsacksick’s picture

Yeah, the reason for that is that you're using administrative_area and not administrative-area (if you want the state code you can use administrative-area-raw).
Same with the zip, you're using "postal_code" instead of "postal-code".

Basically, when Addressfield doesn't alter the token type to "address-field", when it's "struct", the token is handled by the "entity_token" module.

With the patch, Addressfield is handling the tokens for both "commerce-customer-address" and "commerce_customer_address" but the field name (postal-code, etc.) always has dashes....

We could probably expand the patch to support both "-" and "_" for addressfield field names, but I'm not sure that's the way to go.

jsacksick’s picture

@vibrasphere: Could you test now with the attached patch? It should now both support postal-code and postal_code.
I don't really understand all those inconsistencies... but well... Since many people are probably printing both, let's support both.

matas.k’s picture

Yes, it's working now.

xaa’s picture

Yeah, the reason for that is that you're using administrative_area and not administrative-area (if you want the state code you can use administrative-area-raw).
Same with the zip, you're using "postal_code" instead of "postal-code".

thank you for the explanation.

We could probably expand the patch to support both "-" and "_" for addressfield field names, but I'm not sure that's the way to go.

I think this will ensure that existing project won't break.

ps Here I don't use entity_token neither addressfield_tokens

tyler-durden’s picture

I'm having the same issue with this latest update, however this patch doesn't seem to solve the issue.

I am using Entity Tokens also, and have cleared the cache multiple times.

jsacksick’s picture

@tyler-durden: Could you describe exactly what's not working for you? Which tokens are you trying to print etc? That'd allow us to replicate the issue.

tyler-durden’s picture

I reapplied the patch and it seems to be working now. I must have done something wrong with the initial patch update.

xaa’s picture

@Tyler are you sure you have only reapply the patch?

The patch was working for me but not anymore.

I try to use the following tokens on Metatag user og description:
a[node:field_addresse:country]
b[node:field-addresse:country]
c[node:field_addresse:country-code]
d[node:field-addresse:country-code]

no results.
same issue with locality.

waverate’s picture

Patch #18 applied cleanly against 7.x-1.3 and re-enabled the tokens.

Thank you @jsacksick.

  • bojanz committed ab9e317 on 7.x-1.x authored by jsacksick
    Issue #3013566 by jsacksick, czigor: New version doesn't print tokens...
bojanz’s picture

Status: Needs review » Fixed

Committed #18. Thanks, everyone!

egarbeil’s picture

Changing the underscores to hyphens throughout fixed the problem for us - with token module installed. 7.66, all modules current.

Status: Fixed » Closed (fixed)

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

heyyo’s picture

I still have another issue with this. The tokens are correctly displayed within devel token in any node, but printed empty in the node's metatags.
It seems to be an language error related. I have the module locale enabled, with only english language.

to fix my issue I replaced in function addressfield_tokens

  if (isset($options['language'])) {
    $language_code = $options['language']->language;
  }

with

  if (isset($options['language']) && $options['language']->language != 'en') {
    $language_code = $options['language']->language;
  }
anybody’s picture

This is definitely not fixed!
With 7.x-1.2 tokens are working correctly in metatags, in 7.x-1.x-dev and latest stable release 7.x-1.3 they are broken. I'm for example using:

  • [node:field-address:thoroughfare]
  • [node:field_address:thoroughfare]

in a single value field which outputs the correct value testing with 7.x-1.2 and outputting NOTHING in metatags with latest 1.3 and dev.
There are several follow-up issues, I'll reply there if one of the patches fixed the problem for me. But this should definitely be fixed with a new stable release.

Follow-Ups / related see:

Thank you!

anybody’s picture

Reverting back to 7.x-1.2 now, because I couldn't find a solution... The other issues seem unrelated in my case.

vaccinemedia’s picture

@anybody I can confirm that downgrading from 1.3 to 1.2 fixed the issue regarding tokens being output in metatags. Specifically I'm adding job posting schema to a jobs section on a website and need each part of the address to be output as the components of the address in the schema json. I'm not using any custom code, I'm using the schema metatag module.

During testing on version 1.3 of this module I took a look at the token display of the job content type and noticed it was in the disabled region. I thought that this may have been the cause of the missing tokens (always check the token display!). I enabled it and saw SEVERAL options for outputting as either default, individual components separated by a delimiter, just the country name etc... I selected default. Still nothing. Selected the other options, nothing.

I then downgraded to 1.2 and found that using the hyphenated tokens e.g. [node:field-address:thoroughfare] worked for the components of the address:

"jobLocation": {
                "@type": "Place",
                "address": {
                    "@type": "PostalAddress",
                    "streetAddress": "Portland Street",
                    "addressLocality": "Manchester",
                    "addressRegion": "Greater Manchester",
                    "postalCode": "M1 6DF",
                    "addressCountry": "United Kingdom"
                }
            },

Unfortunately [node:field-address:country-code] does not work (it did for a brief second but not now). So:

1.2 has a single option for the token display and works and 1.3 has several options for the token display and doesn't work with metatag.

Manovra76’s picture

workaround for token

file addressfield.token.inc:
1) apply patch #18
2) add this code in line 132 in function addressfield_tokens

[...]
$sanitize = !empty($options['sanitize']);

+ if($type=='address-field') {
+ if(!isset($data['address-field'][$language_code])) {
+ $language_code = LANGUAGE_NONE;
+ }
+ }
$replacements = array();

lwalley’s picture

New issue created for the incorrect language code see #3082673: Language code incorrect in token replacement.

jeff.a’s picture

The code in #34 using 7.x-1.3 seems to have fixed my issue with metatags. Added it to line 116, not 132.

Patch #18 fails for me. Also, I haven't had issues with pathauto tokens, only metatag.

ciss’s picture

Unfortunately the changes in this issue effectively cripple token replacement via the entity module, without offering a proper replacement (pun intended), especially in multi-language environments. Imo it should be reevaluated to what extent addressfield is even responsible for replacing tokens.

A short refresher on how field tokens get replaced:

  • Field tokens that use dashes are implemented by the entity module. They use entity_metadata_wrapper() and the corresponding API to retrieve and display values.
  • Field tokens that use underscores are implemented by the token module. They are rendered via field_view_field() with the "token" view mode.

I'd argue that addressfield should at the very least leave any dash tokens alone, and only try to handle underscore tokens.