What is the way to use tokens in PHP code?
I need to show the user profile picture URL, but the following code doesn't work.
<?php echo "[current-user:picture:url]"; ?>
How do I display tokens in a block? I am using Drupal 7.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.Sign up to join this community
Unless you have a specific reason to use tokens for this purpose, you could just get the profile picture url directly from the user object:
global $user; // get the current user $user = user_load($user->uid); // load all fields etc for user object $picture_url = $user->picture->uri; echo $picture_url;
I totally agree with saadlulu above though - much better to put this code into a module.
Not sure if it suits your purposes, but a quick search of d.o also brings up an existing contributed module that will display the user picture for you:
You could try token_replace
token_replace($text, array $data = array(), array $options = array())
$text: A string potentially containing replaceable tokens.
$data: (optional) An array of keyed objects. For simple replacement scenarios 'node', 'user', and others are common keys, with an accompanying node or user object being the value. Some token types, like 'site', do not require any explicit information from $data and can be replaced even if it is empty.
$options: (optional) A keyed array of settings and flags to control the token replacement process. Supported options are: