Domanda

If I go to admin/config/people/accounts/fields I can see a lot of fields being mapped for LDAP on my server.

For example - for first name there is a machine name "field_fname".

How can I access this on my webform using PHP ?

Like I want to display something like this on my webform through PHP - "Hello first_name last_name, how are you ?"

È stato utile?

Soluzione

So those fields belong to the user entity.

First you will need the uid of the user you want to display data for. If you want to use the currently logged in user then you can use this code:

global $user;

The $user object will now contain details about the currently logged in user.

You now need to call the user_load function to load the additional user fields. E.g:

$account = user_load($user->uid);

If you have the module devel installed at this point you can use:

dpm($account);

This will show you a list of all of the different properties and fields attached to the users account. This can be useful to find out other field names.

Then to answer you original question you could use the following code to output your message:

$first_name = $account->field_fname[LANGUAGE_NONE][0]['value'];
$last_name = $account->field_lname[LANGUAGE_NONE][0]['value'];
echo t("Hello :first_name :last_name, how are you?", array(':first_name' => $first_name, ':last_name' => $last_name));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top