0

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.

2
2

I dont recommend using PHP in a block for many reasons.

I would recommend you to use proper implementation of hook_block_view and hook_block_info to do you custom PHP codes in there.

1
  • 1
    Don't be offput by saadlu's suggestion - it is remarkably easy to make a module to return a block, and would be much easier in the long run (goo.gl/LdWeAM). Checkout the examples project if you want to see how it works (drupal.org/project/examples) Dec 15 '13 at 19:08
1

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:

https://drupal.org/project/user_picture_block

0

You could try token_replace

token_replace($text, array $data = array(), array $options = array())

Parameters

$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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.