drupal_anonymous_user() should return an user entity, however we currently can't as it's used during session initialization when the entity system isn't ready set.

Once the global user gets lazy loaded, we can do that. See #361471: Global $user object should be a complete entity. So postponing for this issue.

CommentFileSizeAuthor
#9 1634280.patch2.82 KBEclipseGc
#4 1634280.patch2.7 KBEclipseGc
#2 1634280.patch3.44 KBEclipseGc
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fago’s picture

Status: Active » Postponed
EclipseGc’s picture

Status: Postponed » Needs review
FileSize
3.44 KB

rlmumford and I worked this out over in #350407: Anonymous should not appear in the users table at all This makes anonymous users instances of the User() entity instead of stdClass(). TypedData will probably need to special case 0 uid user entities, but I think this is a good first step and it should pass tests.

Status: Needs review » Needs work

The last submitted patch, 1634280.patch, failed testing.

EclipseGc’s picture

Status: Needs work » Needs review
FileSize
2.7 KB

OH! oops, uploaded some stuff I was playing with too.

this is the right patch now.

sun’s picture

+++ b/core/includes/theme.inc
@@ -2650,8 +2650,6 @@ function _template_preprocess_default_variables() {
-    // User module overrides these when it is loaded.
-    'user' => drupal_anonymous_user(),

Hm. I'm surprised that the patch came back green with this.

AFAIK, the installer is the scenario in which user.module is actually not enabled, which in turn means that templates do not get a 'user' variable anymore.

Did you manually test the installer by any chance?

sun’s picture

Title: drupal_anonymous_user() should return an user entity » drupal_anonymous_user() should return a User entity
Category: bug » task
Issue tags: +API clean-up
EclipseGc’s picture

yes, apply the patch and it'll install manually just fine.

Eclipse

sun’s picture

Status: Needs review » Reviewed & tested by the community

Well, in that case, this looks good to go. :)

I wonder whether the other two variables is_admin and is_logged_in could be dropped, too, but we can leave that for laterz.

Also, calling ->save() or ->delete() on this returned object will blow up, but apparently, I think that's Exactly Right™.

EclipseGc’s picture

FileSize
2.82 KB

Updated the docblock

Eclipse

sun’s picture

Thank you!

fago’s picture

@@ -2650,8 +2650,6 @@ function _template_preprocess_default_variables() {
- // User module overrides these when it is loaded.
- 'user' => drupal_anonymous_user(),

hm, not sure why that was there, but why does patch remove it? Is that relevant here?

EclipseGc’s picture

It removes it because we don't have access to the class this early, so we'll fatal. I and rlmumford both didn't see a reason for it, and apparently there's no testing that uses it, so we removed it.

Eclipse

fago’s picture

It removes it because we don't have access to the class this early, so we'll fatal.

I see. It kind of makes the theme-system hard-depend on user module, what seems weird. So +1 for doing away with it.

webchick’s picture

Title: drupal_anonymous_user() should return a User entity » Change notice: drupal_anonymous_user() should return a User entity
Priority: Normal » Critical
Status: Reviewed & tested by the community » Active
Issue tags: +Needs change record

Looks like good clean-up, and I'm frankly amazed that such a small patch is all that's needed here, and even further surprised that tests don't barf all over the place. :)

Well, let's give it a try. Committed and pushed to 8.x. Thanks!

Probably needs a change notice.

Btw, for those wondering what happened with $user variable for templates, that's put back in http://api.drupal.org/api/drupal/core%21modules%21system%21system.api.ph... (a fun function name second or third only to http://api.drupal.org/api/function/form_test_form_form_test_alter_form_a...)

Dave Reid’s picture

Issue tags: -Needs change record

It kind of makes the theme-system hard-depend on user module, what seems weird.

It doesn't make the theme system hard-dependent on user, it only makes it user variable available if user module is installed which seems very reasonable to me as user module owns user entities.

Dave Reid’s picture

Issue tags: +Needs change record
catch’s picture

Global user might yet disappear if we get #335411: Switch to Symfony2-based session handling done - that'd make $_SESSION['uid'] or something the anonymous user check, but this seems like good cleanup regardless and that's a long way from being done.

benjifisher’s picture

I am going to write a change notice (my first) for this issue.

benjifisher’s picture

Summary

Convert drupal_anonymous_user() to return an object of type User.

API Changes

  • drupal_anonymous_user() returns an object of type User.

Examples

Help!

It is hard to come up with examples. Some D7 uses have been eliminated. For example,

    $account = user_save(drupal_anonymous_user(), $edit);

in the D7 version of DrupalWebTestCase::drupalCreateUser() has been replaced with

    $account = entity_create('user', $edit);

Most places where this function is called either pass it off to another function or assign it to the global $user object. I could not find an example where properties of the returned object were accessed explicitly.

benjifisher’s picture

Status: Active » Needs review
Issue tags: +SprintWeekend2013

Here is a suggestion for the change notice.

Summary

Convert drupal_anonymous_user() to return an object of type User.

API Changes

  • drupal_anonymous_user() returns an object of type User.

Examples

When this function is used in core, the returned object is either passed off to another function or assigned to the global $user object. These examples are purely hypothetical.

Access properties of the returned object.

Drupal 7

Since the user object is of type stdClass, you can make up your own properties:

  $account = drupal_anonymous_user();
  $account->extra_local_info = 'Secret';
  $html = theme('user_username', $account);

Drupal 8

The User class has a defined list of properties:

  $account = drupal_anonymous_user();
  $defaults = array(
    'name' => '',
    'mail' => '',
    'access' => 0,
    'login' => 0,
    'status' => 1,
    'langcode' => LANGUAGE_NOT_SPECIFIED,
    'preferred_langcode' => LANGUAGE_NOT_SPECIFIED,
    'preferred_admin_langcode' => LANGUAGE_NOT_SPECIFIED,
    'init' => '',
  );
  foreach ($defaults as $property => $value) {
    if ($account->$property !== $value) {
      // Something went wrong.
      break;
    }
  }
xjm’s picture

Status: Needs review » Active
xjm’s picture

Status: Active » Needs review

Oops, xpost :)

podarok’s picture

andypost’s picture

Title: Change notice: drupal_anonymous_user() should return a User entity » drupal_anonymous_user() should return a User entity
Priority: Critical » Normal
Status: Needs review » Fixed
Issue tags: -Needs change record

Looks fine, should be updated after #1818570: Convert users to the new Entity Field API lands

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

Anonymous’s picture

Issue summary: View changes

...minor typo + convert linked issue to the [#...] format