2012-08-01 62 views
4

將令牌替換存儲在變量中的正確方法是什麼?還是應該打擾並直接打電話給他們?Drupal 7令牌替換

事情是這樣:

$author_uid = [node:author:uid]; 
$name = [node:title]; 
$picture = [node:field-image-upload:file]; 
$link = [node:url]; 

是給我一個錯誤:

PHP Parse error: syntax error, unexpected ':' 

我做錯什麼了嗎?

此外,在關於這一行:

$picture = [node:field-image-upload:file]; 

我真正想要得到的是URL鏈接到該圖像文件。我怎樣才能用令牌來做到這一點?

回答

8

如果你想存儲在變量令牌,你應該寫$author_uid = "[node:author:uid]";

注意令牌只是一個字符串
正如the documentation for token.inc指出,令牌系統是...

API functions for replacing placeholders in text with meaningful values.

如果你想要的網址鏈接到圖像文件,你可以這樣做:

$picture = token_replace('[node:field-image-upload:file]', array('node' => $node)); 

請注意,您需要已經有$node對象進入token replacement function

+0

完美。非常感謝你! – hanleyhansen 2012-08-01 18:37:23

+0

你救了我的一天:) – 2017-05-16 13:25:37