How do I Render a Drupal User Profile using api calls? - drupal

I'm doing this to render a node and returning the output to an AJAX call and displaying a node inline on a page:
$node = node_load($nid);
$node_view = node_view($node);
echo drupal_render($node_view);
I need to be able to do the same things for a user profile...is it similar?

It's pretty similar, you can use the following functions:
$account = user_load($uid);
$account_view = user_view($account);
echo drupal_render($account_view);
EDIT
I changed the variable name to use $account instead of $user, just to eliminate the possibility of overwriting the global $user variable.

The Entity API module provide a generic function to render any entity: entity_view() that can also be used. For a user entity this should produce the same results than user_view().
$account = user_load($uid);
$account_view = entity_view('user', $account);
echo drupal_render($account_view);

Related

return result from function in symfony as global in twig templates

I have a function that returns the number of items in the cart. How it is possible to display this number in every page of the website? I need to show its value in a twig file that is being loaded in all pages of the site...
This is the function I d like to use:
public function getCartCount(){
$session = new Session();
$session_user = $session->get('temp_session_user');
$em = $this->doctrine->getManager();
$details = $em->getRepository('AppBundle:RechargeLogs')->findBy(array(
'sessionVal'=> $session_user,
'status'=> 0
));
There are a lot of solutions for this,
You can create a twig filter:
https://symfony.com/doc/current/templating/twig_extension.html
you can embed controller in twig:
http://symfony.com/doc/current/templating/embedding_controllers.html
you can store a variable in session and access it in twig:
https://symfony.com/doc/current/templating/app_variable.html
you can even create service and register it as a global variable in twig and use it:
https://symfony.com/doc/current/templating/global_variables.html#referencing-services

Drupal 7 global $user variable

Would appreciate it, if anyone can let me know how we can set the global $user variable, so that we don't have to keep re-declaring it in each function, to access its contents. How can we declare it so that all the functions in a module can use it?
The type of global you're looking for (available always, in every scope) is called a superglobal in PHP. You cannot declare new superglobals, but you can access all globals directly because they are part of the $GLOBAL superglobal. In other words, you can use $GLOBALS['user'] to access the $user global directly.
See also create superglobal variables in php? for more info and alternative methods.
You can't...that's how globals work in PHP. If you want to import the global variable into your local function then you have to use the global keyword, there's no way round it. This is a 'feature' of the PHP language, it has nothing to do with Drupal.
An alternative method might be to implement a helper function:
function get_current_user() {
global $user;
return $user;
}
And call it like this:
$user = &get_current_user();
In your function if you want to use the $user variable, you just required to use/instantiate 'global' keyword before $user it. So that you can access all the data for that current user of the website.
For example
function myGenericFunc(){
global $user;
$user_id = $user->uid;
}
Note that you cannot redeclare it.
I hope it helps.

Drupal 7 - Why does $acc->value()->name print out the username?

<?php
$involved_users = array();
//grab usernames from user reference field of a node
$project = entity_metadata_wrapper('node', $node);
// field_users is user reference field
foreach ($project->field_users as $acc) {
$involved_users[] = $acc->value()->name;
}
var_dump($involved_users);
?>
Hi Guys,
I got this bit of code from http://pixeljets.com/blog/writing-robust-code-uses-fields-drupal-7
I have been going through it but I can't seem to figure out how "$acc->value()->name;" work?
What is value()? Is it PHP method or a Drupal one. I can't find any documentation on this.
Thanks!
If you read the blog post you provided carefully, you'll see that the value() method is provided by entity_metadata_wrapper() from the Entity module.

Adding avatar to $user in Drupal 7

I've created a simple module for displaying a flash game in a custom block by overwriting game_block_view() and game_block_info() in the sites/default/modules/game.module and it works ok.
I need however to pass user avatar and also gender and city (I've added the 2 mandatory fields to the registration form) through the FlashVars-parameter to the flash game in my block.
So I'm trying to overload the hook_user_load, because I suppose that this is the method where you add properties to the $user object after it has been initiated from the database (this probably happens when the user logins or alters his/her profile data?):
function game_user_load($users) {
global $user;
$uid = $user->uid;
$result = db_query('select filename from {file_managed} where uid=:uid', array(':uid' => array($uid)));
$avatar = $result->fetchField();
$users[$uid]->avatar = $avatar;
drupal_set_message("<pre>$uid: $avatar</pre>\n");
print_r($users);
}
Unfortunately I see no output produced by the last 2 lines above in the web page
What am I doing wrong?
Thank you!
Alex
The global user object does not go through hook_user_load(), see http://api.drupal.org/api/drupal/includes--session.inc/function/_drupal_session_read/7. Don't ask me why, that's just the way it is :)
When using user_load(), any added fields will automatically be loaded, you don't need custom code for that. You just need to know how to access them, which is a bit complicated.
Something like this should work:
global $user;
// $account is now a fully loaded user object.
$account = user_load($user->uid);
// Your field name is probably 'field_avatar'.
if ($avatar = field_get_items('user', $account, 'field_avatar')) {
dpm($avatar); // only works with devel.module, strongly suggested!
}

Dynamic links in Drupal

This must be an easy one but I can't find documentation for it online.
I'm trying to use the l() function in Drupal to create a dynamic link. What's the syntax?
At the moment I have:
l('Destination',"path/$user->uid/category")
which points to:
path/%2Fcategory
first of all, if you're working within a function, you'll need to get access to the global user object.
Secondly, if the user is anonymous/not logged in, the $user->uid might not be set or be 0.
lastly to prevent errors, it is common to concatenate variables together with strings
global $user;
if ($user->uid)
{
l('Destination', 'path/'.$user->uid.'/category')
}
l() is correcting your URL to path/%2Fcategory because it's trying to make a workable link from the string path//category.
Your string is path//category because $user->uid has no value. It has no value because either you haven't pulled up a user object from global $user or user_load(), or your user is anonymous.
I would suggest putting checking the value of $user before calling l(), for example:
global $user; // or $user = user_load($foo);
if ($user) {
l('Destination', 'path/'.$user->uid.'/category');
} else {
l('Destination', 'path/you-are-not-logged-in');
}
Try concatenating the strings instead.
l('Destination',"path/".$user->uid."/category")
as for the documentation, here it is: http://api.drupal.org/api/function/l/4.7
l($text,
$path,
$attributes = array(),
$query = NULL,
$fragment = NULL,
$absolute = FALSE,
$html = FALSE)
The documentation for the l() function is located at:
http://api.drupal.org/api/function/l/6
Other stuff has been said yet by others :)

Resources