Some lines of amazon.module's amazon_token_list() do nothing. For example, $values is never read or returned:

$values['amazon_item']['isbn'] = t('The 10-digit ISBN (International Standard Book Number)');

I assume $values should be $tokens. Looks like a copy/paste error from amazon_token_values.

Comments

rfay’s picture

Are you using tokens in D7 Amazon? I've never figured out how to use them, so have considered removing the support. However, since there is an open issue that would let you select which field tokens come from in D7 it might end up being useful.

Please explain how you're using tokens, and maybe we can find a path forward with them.

Edit: See #691078: Field tokens

jdleonard’s picture

I'm not actually using them, but I was looking over the code and noticed the discrepancy.

rfay’s picture

Patches are welcome! And a useful use for tokens with Amazon is *way* welcome.

pancho’s picture

Version: 7.x-1.0-beta4 » 7.x-1.x-dev
Assigned: Unassigned » pancho

I'm gonna take a look at this.the next days.

rfay’s picture

With tokens, we need correct behavior *and* instructions on the documentation page about how one might use them. I think the field tokens patch is getting closer, and it may require that. #691078: Field tokens.

pancho’s picture

Title: Not all tokens returned in amazon_token_list() » Port token support to D7
Status: Active » Needs work
StatusFileSize
new2.95 KB

Token support currently doesn't work at all, cause the API has completely changed in D7.
Making it work is unfortunately non-trivial.

Supporting amazon_field (aka asin.module) with tokens requires #691078: Field tokens to be committed at least to contrib token module, so in any case we'd still have the dependency on that. Don't know about amazon_filter, however the problem seems to be that 1:n relations can't be currently mapped, so it's reasonable to put this on hold, too.

While making token support work for amazon_store is indeed possible, it comes for free with using the Entity API, which is anyway planned. So first of all, it makes sense to remove the non-working token code from 7.x-1, then we proceed to #1047642: Use Entity API in amazon_media, and then we come back here.

Removed non-working token support on commit: http://drupal.org/cvs?commit=500940

Taxoman’s picture

Subscribing

michelle’s picture

@Pancho: It's been 1.5 years... Is this still in the works or did it get dropped?

smitty’s picture

As far as I can see the token module provides already a token-support for every asin-field which is added to a document type. And the best is: This seems to work!

BUT: This token uses one of the amazon asin-formatters, which all return html-code. And because in the metadata-module a strip_tags is used in the end, the return value seems to be empty there.

So all we need is one or more additional formatters (one of them defined as default_token_formatter as described in http://drupal.org/node/1252608) which delivers the asin as plain text, the title of the asin field or the url of the amazon cover (e.g. for the og:image metatag) and then we can choose which value we want to use as described in http://drupal.org/node/1299662.

Because I am not experienced with all the formatter stuff, I made a (really) quick and dirty solution for my project and inserted two tokens which deliver the plain text of the asin and the image url for one field_asin I put into my documenttyp. The code is not optimised at all and may not be correct in terms of the token module; but for me it works.

Perhaps someone having the same problems finds this helpful:

/**
 * Implements hook_token_info().
 */
function amazon_token_info() {
  $info['tokens']['node']['asin'] = array(
    'name' => t('Amazon asin'),
    'description' => t("Amazon asin."),
    'type' => 'amazon',
  );
  $info['tokens']['node']['asin-image'] = array(
    'name' => t('Amazon asin image'),
    'description' => t('url to the image of the asin.'),
    'type' => 'amazon',
  );
  return $info;
}

function amazon_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  if ($type == 'entity' && !empty($data['entity']) && $data['entity_type'] == 'node') {
    $node = $data['entity'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'asin':
          if (!empty($node->field_aisn)) {
            $replacements[$original] = $node->field_asin['und'][0]['asin'];
          }
          break;
        case 'asin-image':
          if (!empty($node->field_asin)) {
            $asin = $node->field_aisn['und'][0]['asin'];
            $amazon_item = amazon_item_lookup($asin);
            $replacements[$original] = $amazon_item[$asin]['imagesets']['largeimage']['url'];
          }
          break;
      }
    }
    return $replacements;
  }
}

Improvements welcome!

marcoka’s picture

i use the token support and it is a LOT useful. like using it for captions

<span class="single-image-caption">[amazon B0013BXFLG inline]</span>

or to link a whole image to the amazon url just by providing ASIN [amazon B0013BXFLG detailpageurl]

only problem is that the tokens are with spaces. a lot of filters and wysiwyg will add a instead of spaces:

[amazon B0013BXFLG detailpageurl] becomes [amazon%20B0013BXFLG%20detailpageurl]

willvincent’s picture

e-anima, that's the amazon_filter, not tokens.

Tokens are for accessing specific amazon data fields.

willvincent’s picture

StatusFileSize
new3.24 KB

This patch adds a new formatter type for the asin field for ASIN as plain text. This is also set to be the default token value.

I'll be working through adding other tokens as well, but this will work as the default field token that's already handled by token module.

So, if for example, you had a content type with a field with the machine name: field_amazon_item, the token [node:field_amazon_item] will output just the plain text asin value stored in that field.

This change has been commit to the 7.x-1.x branch

willvincent’s picture

Assigned: pancho » Unassigned
kihap’s picture

Hello, willvincent & pancho. Any luck getting additional tokens working for D7?

  • Pancho committed 3abe787 on 7.x-2.x
    #1014036 by Pancho: First take on token support: Remove non-working code...