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:

<?php
/**
* 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:

<?php
/**
* 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.

Files: 
CommentFileSizeAuthor
#2 2010124-ctools-logged-in-user-context-user-load-v2.patch593 bytesdavidwhthomas
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]
ctools-logged-in-user-context-user-load.patch548 bytesdavidwhthomas
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]

Comments

Status:Needs review» Needs work

Couple of points:

Another approach could have been to change ctools_context_create_user(), e.g:

<?php
if ($data['type'] == 'current' || $data === $GLOBALS['user']]) {
?>

Other than that, patch looks good. One note: ctools_access_get_loggedin_context() won't need the global $user any more.

Status:Needs work» Needs review
StatusFileSize
new593 bytes
PASSED: [[SimpleTest]]: [MySQL] 121 pass(es).
[ View ]

Here'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_user

Issue summary:View changes
Status:Needs review» Reviewed & tested by the community

I can confirm that the patch in #2 resolves an issue I was seeing with user entity fields not being available during variant selection.

Status:Reviewed & tested by the community» Fixed

Status:Fixed» Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.