I found a bug where ctools_access_get_loggedin_context doesn't fully load the current user when building the logged in user context.
This meant fields from the user weren't available.
The logged in user context is currently created like this:
/**
* Create a context for the logged in user.
*/
function ctools_access_get_loggedin_context() {
global $user;
$context = ctools_context_create('entity:user', $user);
$context->identifier = t('Logged in user');
$context->keyword = 'viewer';
$context->id = 0;
return $context;
}
However, the related function ctools_context_create_user never actually loads the user entity ( Because $conf is passed as FALSE and $data ($user object) is not numeric )
See:
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_user($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context(array('entity:user', 'entity', 'user'));
$context->plugin = 'user';
if ($empty) {
return $context;
}
if ($conf) {
if ($data['type'] == 'current') {
global $user;
$data = user_load($user->uid);
$data->logged_in_user = TRUE;
}
else {
$data = user_load($data['uid']);
}
}
// Load entity if the data provided is a numeric value. This kind of data is
// passed by some relationships.
if (is_numeric($data)) {
$data = user_load($data);
}
if (!empty($data)) {
$context->data = $data;
$context->title = isset($data->name) ? $data->name : t('Anonymous');
$context->argument = $data->uid;
return $context;
}
}
The attached patch simply makes the function ctools_access_get_loggedin_context user the $data['type'] == 'current' parameter, which is supported by ctools_context_create_user and I believe should be used by ctools_access_get_loggedin_context anyway.
The result is a properly fully loaded current logged in user context.
Please review and commit if possible, thanks.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | 2010124-ctools-anonymous-current-user-context.patch | 530 bytes | davidwhthomas |
| #2 | 2010124-ctools-logged-in-user-context-user-load-v2.patch | 593 bytes | davidwhthomas |
| ctools-logged-in-user-context-user-load.patch | 548 bytes | davidwhthomas |
Comments
Comment #1
Angry Dan commentedCouple of points:
Another approach could have been to change ctools_context_create_user(), e.g:
Other than that, patch looks good. One note: ctools_access_get_loggedin_context() won't need the
global $userany more.Comment #2
davidwhthomas commentedHere's another version of the patch that removes the now unused
global $user;I think passing the 'current' parameter is most suitable here, as that's already supported in
ctools_context_create_userComment #3
jhedstromI can confirm that the patch in #2 resolves an issue I was seeing with user entity fields not being available during variant selection.
Comment #4
japerryFixed and committed:
http://drupalcode.org/project/ctools.git/commit/d3a1c5bd8ef36db31f03a0ed...
Comment #6
Kojo Unsui commentedThis issue seems to be related : https://drupal.org/node/2212641#comment-8804059 :
on any panelized page (Commons distrib) ,
ctools_context_create_user()getsArray ([type] => current)as 2nd parameter, then sets$data->logged_in_user = TRUE;even for anonymous...Comment #7
Kojo Unsui commentedComment #8
davidwhthomas commented@Kojo
Yes, there does appear to be a related issue that affects getting the current user context for an anonymous user.
I've attached a patch that checks the `user_is_logged_in` and will only set `$data->logged_in_user = TRUE;` if the user is indeed authenticated.
Comment #9
Kojo Unsui commentedFine, applied successfully. I didn't know
user_is_logged_in(). ThanksComment #11
ACF commentedNot sure why this was closed. This bug is still there and this patch fixes it.
Comment #12
japerryFixed! thanks for the report.