I know that I can fetch the logged in user's name as
<?php
global $user;
print $user->name;
?>
However, I want to know how to show logged in user's full name (first and last name) and email id. Any help?
You could use print_r($user) to look at the $user object. But i dont think global $user includes profile fields, so you might have to load the user object again like so:
global $user;
$account = user_load($user->uid);
print_r($account);
And the email field is usually:
$user->mail;
If you are still using drupal 7. you can get the user profile like this:
global $user;
$account = user_load($user->uid);
$user_email = $account->field_profile_email[LANGUAGE_NONE][0]["email"];
$user_first_name = $account->field_first_names[LANGUAGE_NONE][0]["value"];
$user_last_name = $account->field_surname[LANGUAGE_NONE][0]["value"];
$user_full_name = "$user_first_name $user_last_name";
Related
$users = TableRegistry::get('Users');
if ($this->request->data) {
$query = $users->findByEmail($this->request->getData('email'));
In the code above, I have retrieved one row from my table where the user's email matches the requested email.
Next, I want to write the code below to check if the password of the selected user is same as the requested password.
PasswordOfSelectedRow == md5($this->request->getData('password')))
What should I put instead of PasswordOfSelectedRow?
The following line returns a query object from a dynamic finder
$query = $users->findByEmail($this->request->getData('email'));
From the docs:
Once you have a query object from a dynamic finder, you’ll need to call first() if you want the first result.
So you could write something like this to retrieve the user:
$user = $query->first();
And then to compare to the request data:
$user->password == md5($this->request->getData('password')))
Hi I'm new in cakephp and i want to save some array data in database
here is my code
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
$userdata=[
'firstname'=>$user['firstname'],
'lastname'=>$user['lastname'],
'nationcod'=>$user['nationcod'],
'usrename'=>$user['username'],
'password'=>$user['username']
];
$this->Users->save($userdata);
but when I test it this error occurred:
Call to a member function errors() on array.
I just wanna change user posted data and save it in the users table.
You have assigned $user with $this->Users->newEntity();. And afterwords you are using $user['firstname']. There won't be anything in it as it is an entity not array. From your question I guess that you want to keep user's username as their password. So all you have to do is this.
$user=$this->Users->newEntity();
if($this->request->is('post')){
$this->request->data['password']=$this->request->data['username'];
$user=$this->Users->patchEntity($user,$this->request->data);
$this->Users->save($user);
}
I'm trying to print a few fields in a block on each profile page.
The block needs to display the fields of the user being viewed, not the logged in user.
$account = user_load($node->uid); - doesn't work. user->uid doesn't either.
Globals user will return the logged in users info.
Not exactly sure how I'm supposed to load anything into a block. Any idea?
Assuming you're viewing a user profile with a URL path like this
drupal/user/USER_ID
You can do as follows :
// Option 1
$user = explode('/', current_path());
$user_id = end($user);
// Option 2
// $path = explode('/', current_path());
// $user_id = $path[count($path)-1];
$account = user_load($user_id, true);
Documentation current_path()
This way you get the last part of the URL and pass it to the user_load function. We set true as second parameter so it loads from the DB and not the cache.
Another way to do the same without breaking the URL into pieces is to use arg() to get the parameters
In this case the code would look like this
// Option 3
// drupal/user/USER_ID
// drupal = arg(0)
// user = arg(1)
// USER_ID = arg(2)
$account = user_load(arg(2), true);
In both cases check that $user_id is an integer, do not let it pass at least is an integer, then you can check the result of user_load.
Update
If you are using the user's name in the URL you can load a full user object by using user_load_by_name()
So the code would change a little bit. Try this :
$account = user_load_by_name(arg(2));
I used arg for simplicity, you can get the user's name from URL as you want.
Hope it helps.
I have a page where there are multiples forms generated with the FormHelper that aim at modifying the same entity. The problem is: validation errors will show up on both forms.
With cakephp 2, this problem was solved by extending Models (see : http://bakery.cakephp.org/articles/RabidFire/2010/06/26/multiple-forms-per-page-for-the-same-model ).
However I don't see how to do this with cakephp 3.
EDIT: i'm gonna describe more precisely what I'm trying to do.
I have two forms on the same page. The first one enables a user to change his email address, the other one to change his password.
Both forms are created with the Form helper and the same user entity.
In both forms, there is a field where the user should enter his current password (as a security measure). A validator will check if the password entered is correct before letting the email or the password to be changed.
Problem: let's say the user tries to change his email but typed a wrong password, the "wrong password" message will appear on both forms.
This is sort of an edge case that the FormHelper is not ready to handle graciously. But this is a solution, you will need 2 entities:
$user = $this->Users->get($id);
$user->unsetProperty('password');
$clonedUser = clone $user;
$this->set(compact('user', 'clonedUser'));
In your view, you build your forms in a way that you can detect which entity you should pass:
echo $this->Form->create($this->request->data('_form1') ? $user : $clonedUser);
... fields here
echo $this->Form->hidden('_form1', ['value' => 1]);
echo $this->Form->create($this->request->data('_form2') ? $user : $clonedUser);
... fields here
echo $this->Form->hidden('_form2', ['value' => 1]);
What the above code does is detecting which of the forms was previously submitted and render the form with either the empty cloned entity or the entity having the errors.
Stumbled upon this because I had the same requirement. I would go with José but move the logic to the Controller instead:
$callback = $this->Inquiries->newEntity();
$inquiry = $this->Inquiries->newEntity();
if ($this->request->is('post')) {
if ($this->request->data('_type') === 'callback') {
$callback = $this->Inquiries->patchEntity($callback, $this->request->data, ['validate' => 'callback']);
$entity = &$callback;
} elseif ($this->request->data('_type') === 'inquiry') {
$inquiry = $this->Inquiries->patchEntity($inquiry, $this->request->data);
$entity = &$inquiry;
}
if (!$entity->errors()) {
// do stuff here
}
}
$this->set(compact('callback', 'inquiry'));
Pass the type of the form:
echo $this->Form->input('_type', ['type' => 'hidden', 'value' => 'inquiry']);
I am trying to get the user profile loaded with entity_metadata_wrapper:
$user_profile = entity_metadata_wrapper('user', $uid);
dpm($user_profile);
but I get nothing back. The user fields load fine with user_load($uid); What am I doing wrong?
The information provided by Linas is provably incorrect. The use of entity_metadata_wrapper with an entity id is completely okay. The function uses uid internally only if you pass an object to it to begin with, otherwise it passes the argument directly to EntityDrupalWrapper.
Eventually the set method will be called, which states: "Overridden to support setting the entity by either the object or the id."
This basic code will demonstrate a working call to entity_metadata_wrapper with just a user id.
$wrapper = entity_metadata_wrapper('user', 1);
dpm($wrapper->value());
The output will be an array of all the data belonging to the admin user.
The problem you are having is unrelated to your arguments being passed in (assuming $uid is valid), and requires more information to troubleshoot.
entity_metadata_wrapper expects second parameter to be an object, in this case, user object. According to your variable naming, it seems that you are passing user id instead. You can use user_load to fetch user object by ID.
I have the same problem the code not work return empty (Object) EntityDrupalWrapper
global $user;
$user_profile = entity_metadata_wrapper('user', $user->uid);
dpm($user_profile);
But that code work well and return (Object) stdClass with all users fields like name password and other
global $user;
$user = user_load($user->uid);
$wrapper = entity_metadata_wrapper('user', $user);
dpm($wrapper->value());