The module description says 'basic token support', but no tokens show up in the list of tokens available to Automatic Node Title.

I'd like to be able to simply input an ASIN number and have the generated node use the Title from amazon. How can you use tokens?

Is this possible?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

arghman’s picture

Me too! I'd love to use the various amazon metadata that is available, but can't figure out how.

Anonymous’s picture

A fast solution is something like the following (assuming that field_asin contains the ASIN, it is a single required value and you want the list of authors followed by the title) entered into the "automatic title generation" field.

  $asin    = _amazon_clean_type($node->field_asin[0]['value']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $author = "";
  for ( $i=0; $i<count($amazon['author']); $i++ ) {
    if ( $i > 0 ) { $ownTitle .= ", "; }
    $author .= $amazon['author'][$i];
  }
  if ( strlen($amazon) > 40 && count($amazon['author']) > 1 ) {
    $author = $amazon['author'][0] . "...";
  }
  $ownTitle .= "$author - ".$amazon['title'];
  print $ownTitle;
gmak’s picture

tirsales,

I'm interested in your code, but where do I put it?

Thanks

Anonymous’s picture

This code uses Auto Nodetitle to generate the nodetitle automatically. Auto nodetitle provides a field "Automatic title generation" on each "edit content-type settings"-page (e.g. admin/content/node-type/amazontype).
Please remember to check "evaluate PHP"

saepl’s picture

I just tried the auto nodetitle trick and had no success. The title only has a dash ('-') in it.

Anonymous’s picture

Could you paste your code and ASIN? Even better: A screenshot of your page.

saepl’s picture

FileSize
89.26 KB

Attached is a screen shot and here is the code I am using. I changed it a bit to add the word "Book Review: " at the beginning of the title.

  $asin    = _amazon_clean_type($node->field_asin[0]['value']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $author = "";
  for ( $i=0; $i<count($amazon['author']); $i++ ) {
    if ( $i > 0 ) { $ownTitle .= ", "; }
    $author .= $amazon['author'][$i];
  }
  if ( strlen($amazon) > 40 && count($amazon['author']) > 1 ) {
    $author = $amazon['author'][0] . "...";
  }
  $ownTitle .= "Book Review: ".$author." - ".$amazon['title'];
  print $ownTitle;
Anonymous’s picture

I do apologize for not stating clearly what I meant - I meant a screenshot of the content-type settings page (the page where you add your autotitle). Is your field named "asin" internally and did you apply the patch needed to parse ASINs with a leading 0 (looks alike seeing the cover, but still want to ask).

saepl’s picture

FileSize
74.15 KB
57.87 KB
59.47 KB

Sorry. Here are three more screen shots they should have what you need (I hope!)

Anonymous’s picture

Use field_booktitle instead of field_asin:

  $asin    = _amazon_clean_type($node->field_booktitle[0]['value']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $author = "";
  for ( $i=0; $i<count($amazon['author']); $i++ ) {
    if ( $i > 0 ) { $ownTitle .= ", "; }
    $author .= $amazon['author'][$i];
  }
  if ( strlen($amazon) > 40 && count($amazon['author']) > 1 ) {
    $author = $amazon['author'][0] . "...";
  }
  $ownTitle .= "Book Review: ".$author." - ".$amazon['title'];
  print $ownTitle;
saepl’s picture

Hmm...very disappointing but it doesn't work :( I even tried deleting my original field and created a new one called "field_asin" but I get the same thing, a title with just a dash in it.

Anonymous’s picture

Well - what title are you now using for the field? and which title is presented in the autotitle-form?

--edit: And to use the above snippet, the field should be a textfield, no Amazon-field.

saepl’s picture

I created a new textfield called "asin" so I am using "field_asin" in the autotitle-form.

Anonymous’s picture

Some corrections (assuming the field is called (field_)name and is a SIMPLE textfield, not an amazon textfield):

  $asin    = _amazon_clean_type($node->field_name[0]['value']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $author = "";
  for ( $i=0; $i<count($amazon['author']); $i++ ) {
    if ( $i > 0 ) { $author .= ", "; }
    $author .= $amazon['author'][$i];
  }
  if ( strlen($author) > 40 && count($amazon['author']) > 1 ) {
    $author = $amazon['author'][0] . "...";
  }
  $ownTitle = "Book Review: ".$author." - ".$amazon['title'];
  print $ownTitle;

assuming its an AMAZON textfield (Type: Amazon, element: Textfield)

  $asin    = _amazon_clean_type($node->field_name[0]['asin']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $author = "";
  for ( $i=0; $i<count($amazon['author']); $i++ ) {
    if ( $i > 0 ) { $author .= ", "; }
    $author .= $amazon['author'][$i];
  }
  if ( strlen($author) > 40 && count($amazon['author']) > 1 ) {
    $author = $amazon['author'][0] . "...";
  }
  $ownTitle = "Book Review: ".$author." - ".$amazon['title'];
  print $ownTitle;

Please regard: The difference lies in $node->field_name[0]['asin'] (AMAZON textfield) vs $node->field_name[0]['value] (normal textfield).

saepl’s picture

Thanks tirsales. It works now! I was using an amazon text field.

saepl’s picture

I am resurrecting this thread :)

I noticed that when I use this code in my auto title that it only generates the titles for books but not for movies or cds

  $asin    = _amazon_clean_type($node->field_asin[0]['asin']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $author = "";
  for ( $i=0; $i<count($amazon['author']); $i++ ) {
    if ( $i > 0 ) { $author .= ", "; }
    $author .= $amazon['author'][$i];
  }
  if ( strlen($author) > 40 && count($amazon['author']) > 1 ) {
    $author = $amazon['author'][0] . "...";
  }
  $ownTitle = "Review: ".$amazon['title'];
  print $ownTitle;
sean porter’s picture

Version: 6.x-1.0-beta5 » 6.x-1.0-beta7
FileSize
45.3 KB
37.7 KB

Trying to see what I'm doing wrong, I'd love to get auto nodetitle to work this way. I'm getting nothing to show up when I rebuild my nodetitles.

<?php
  $asin    = _amazon_clean_type($node->field_amazonitem[0]['asin']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $author = "";
 
  $ownTitle = $amazon['title'];
  print $ownTitle;
?>

I've attached screengrabs of my content-type settings

any help would be greatly appreciated!

saepl’s picture

Do you have evaluate php in patterns turned on? http://drupal.org/files/issues/Picture%204_75.png

sean porter’s picture

Yes, I do, sorry that wasn't included the picture.

Anonymous’s picture

@saepl:

Print the list of available keys - its very likely that the "author"-key is only available for *books, not for music or dvds.

watchdog('Amazon Test','

'.print_r($amazon,true).'

');

should print the complete $amazon-variable to the watchdog ( admin/reports/dblog ). Have a look at it and decide, which fields to use :)
Maybe you need something like

if ( array_key_exists('author',$amazon) ) {
// use $amazon['author'];
} else {
// use $amazon['producer'] or whatever
}

Anonymous’s picture

@sean porter:
You could change your code to the following:

  $asin    = _amazon_clean_type($node->field_amazonitem[0]['asin']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  watchdog('Amazon Test','<pre>'.print_r($amazon,true).'</pre>');
  print $amazon['title'];

This should log the content of $amazon, then you can verify whether you can find any item at all :)

saepl’s picture

When I add a book I get the $amazon variable if I choose a CD (I choose a beatles cd) I get empty - no data at all :(

Anonymous’s picture

Do you have the correct ASIN?

saepl’s picture

I assume I have the correct ASIN because the cd cover, artist, etc is put into my node

Anonymous’s picture

Well, print or watchdog $_amazon and $asin - it's quite possible that CDs use a different data structure.

saepl’s picture

Ok. I solved my problem.

When you enter an asin that relates to a cd or movie the asin value listed on amazon's website is all uppercase but then it gets converted to all lower case in $asin so you have to make $asin uppercase as per:

  $asin    = strtoupper(_amazon_clean_type($node->field_asin[0]['asin']));
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $ownTitle = "Review: ".$amazon['title'];
  print $ownTitle;
Anonymous’s picture

Thanks for the info :)

sean porter’s picture

@tirsales

awesome, this worked for me. I'm not sure if something was wrong with my code or if it was because I didn't have the php evaluation module enabled...

gmak’s picture

This:

<?php
  $asin    = strtoupper(_amazon_clean_type($node->field_asin[0]['asin']));
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $ownTitle = $amazon['title'];
  print $ownTitle;
?>

was working fine for me, but suddenly it has stopped working. My tests with the module (/admin/settings/amazon/test) work fine and retrieve all the relevant info.

Any idea why this would suddenly stop getting the title and populating the node title?

gmak’s picture

I've found something odd. There seem to be some ASIN numbers that don't work. I can't see why this would be, but with a few numbers a title doesn't get retrieved. If I plug the ASIN into the testing page it gets a title, but not with the PHP code for the auto node title.

Any suggestions?

Anonymous’s picture

Try searching if a previous issue exists (I remember a previous issue regarding non-working ASIN) - otherwise file a bug. And/or try to get some more debug output (e.g. printing $asin via print_r)

rfay’s picture

Status: Active » Fixed

Note that an ASIN is not a guarantee that the item will always be available from Amazon. It's quite normal that an ASIN disappears from Amazon, meaning that it's orphaned in your node on Drupal.

Marking this 'fixed', but I will add token support as a feature request in the D7 port list.

Status: Fixed » Closed (fixed)

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

Edward Andrews’s picture

Two questions related to this.

I'm using the code in post #14 - the version for the amazon field type. It works just as expected. EXCEPT there is at least one ASIN that doesn't work. It finds the information for the post, but the title doesn't work. I'm using amazon.co.uk and the particular ASIN is 193339742X

Using the amazon module test page with this ASIN works as expected, picture attached, but the title doesn't work at all.

Any thoughts?

Second question - is it possible to modify the code to use the 13 figure ISBN number as the input?

Thanks.

rfay’s picture

Status: Closed (fixed) » Active

@Edward Andrews: Please use the current version, if you're not already.

If you're having trouble doing a test on a specific ASIN, please (with the current version) do it and then follow the link to the error log and report the results.

The current version of Amazon module does ISBN-13. See the project page

antiorario’s picture

subscribing

hojoeast’s picture

I'm trying to utilize the snippet in comment 14. My field name is aisbn. The snippet I'm using is:

  $asin    = _amazon_clean_type($node->field_aisbn[0]['asin']);
  $_amazon = amazon_item_lookup($asin);
  $amazon  = $_amazon[$asin];
  $author = "";
  for ( $i=0; $i<count($amazon['author']); $i++ ) {
    if ( $i > 0 ) { $author .= ", "; }
    $author .= $amazon['author'][$i];
  }
  if ( strlen($author) > 40 && count($amazon['author']) > 1 ) {
    $author = $amazon['author'][0] . "...";
  }
  $ownTitle = "Book Review: ".$author." - ".$amazon['title'];
  print $ownTitle;

I've tried numerous variations, but, all I ever get is Book Review: and no title or author. I've tried several ISBN numbers. The content is displayed properly with the appropriate book. But, the node title always comes out as just Book Review:.

I've attached a snapshot of my field list and also the automatic title section. It must be something simple that I'm doing wrong, but, I just can't spot it. Any help would be greatly appreciated. Thx.

hojoeast’s picture

Perhaps someone might be able to point me in the right direction. I've done some further work with this snippet. I've determined that it works fine for ten digit ISBN numbers, with or without leading zeroes. However, it does not work for 10 or 13 digit ISBN numbers with embedded hyphens. Also, it does not work at all for 13 digit ISBN numbers. I'm using version 6.x-1.0-rc3 of the Amazon module. My content works fine. The only thing that is a problem is this issue with Auto Nodetitles. Thanks in advance.

rfay’s picture

Status: Active » Fixed

@hojoeast, the current version does fine with ISBN 13-digit ISBN numbers, and it accepts embedded dashes.

If you have an example or a specific problem, please open another issue. I'll do my best to resove it.

Thanks,
-Randy

Status: Fixed » Closed (fixed)

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

CarbonPig’s picture

subscribe

cwithout’s picture

For anyone else who doesn't want to turn on PHP filter, you can try the following in a custom module to get a token to use for autotitle.

/**
 * Implementation of hook_token_list()
 */
function [MODULE]_token_list($type = 'all') {
	if($type) == 'node' {
			$tokens['[MODULE]']['amazontitle'] = t('Amazon Title');
	}
	return $tokens;
}

/**
 * Implementation of hook_token_values()
 */
function [MODULE]_token_values($type, $object = NULL, $options = array()) {
	if($type ==  'node') {
			$asin = $object->field_isbn[0]['asin']; 
			//field_isbn is my content type's amazon field name. Change it if yours is different
			$itemArr = amazon_item_lookup($asin);
			$item = $itemArr[$asin];
			$values['amazontitle'] = $item['title'];
	}
	return $values;
}
rfay’s picture

Correct code for this use case (auto_nodetitle with an ASIN CCK field) is in #991994-3: Using Auto NodeTitle with an Amazon CCK field to generate title (and using ISBN-13). The code in this issue has some oddities (unneeded _amazon_clean_type(), etc.)

BeaPower’s picture

How can I add the ISBN to the title for books?

rfay’s picture

In the case of books, a 10-digit ASIN is the ISBN number....
This code will work assuming the ASIN field is named field_isbn. Note that the ISBN is not formatted.

    if (!empty($node->field_isbn[0]['asin'])) {
      $asin = $node->field_isbn[0]['asin'];    // Get the asin from the field
      $asin = amazon_convert_to_asin($asin);  // Turn ISBN-13, etc into ASIN. Needed only if ISBNs are allowed
      // dsm($asin, 'found asin');
      $items = amazon_item_lookup($asin);   // Get the full data.
      // dsm($items, '$items');
      if (count($items)) {
        print $items[$asin]['title'] . " (ISBN " . $items[$asin]['isbn'] . ')';
        return;
      }
    }
    print t("No Amazon item can be used to generate title, so this is the bogus title (node @nid)", array('@nid' => $node->nid));

You can access any of the values shown on admin/settings/amazon/test.

ambereyes’s picture

Status: Closed (fixed) » Needs review

#42 worked for me, thanks.

This token and others should be added to the module.

rfay’s picture

Title: token support for automatic node title » Add token support for automatic node title
Category: bug » feature
Status: Needs review » Fixed

I actually never have figured out how the (existing) token support in ASIN module is useful, since it's not clear which field in a multivalued field creates the token. #42 assumes in advance that it knows the name and number of the specific CCK field on the page to use. General code does not know that.

Setting this to "fixed", but if somebody wants to make a specific proposal or provide a patch, please open a new issue.

Status: Fixed » Closed (fixed)

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

cwithout’s picture

Anyone who needs this functionality, please go to #1043030: D6: Improved token support (especially for Automatic Node Titles) and test the patch. Post any issues you encounter with the patch there.

(Make sure you download the latest one. Don't use the first one posted. It had mistakes.)