Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#9 | 1634280.patch | 2.82 KB | EclipseGc |
#4 | 1634280.patch | 2.7 KB | EclipseGc |
#2 | 1634280.patch | 3.44 KB | EclipseGc |
Comments
Comment #1
fagoComment #2
EclipseGc CreditAttribution: EclipseGc commentedrlmumford 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.
Comment #4
EclipseGc CreditAttribution: EclipseGc commentedOH! oops, uploaded some stuff I was playing with too.
this is the right patch now.
Comment #5
sunHm. 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?
Comment #6
sunComment #7
EclipseGc CreditAttribution: EclipseGc commentedyes, apply the patch and it'll install manually just fine.
Eclipse
Comment #8
sunWell, 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™.
Comment #9
EclipseGc CreditAttribution: EclipseGc commentedUpdated the docblock
Eclipse
Comment #10
sunThank you!
Comment #11
fagohm, not sure why that was there, but why does patch remove it? Is that relevant here?
Comment #12
EclipseGc CreditAttribution: EclipseGc commentedIt 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
Comment #13
fagoI see. It kind of makes the theme-system hard-depend on user module, what seems weird. So +1 for doing away with it.
Comment #14
webchickLooks 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...)
Comment #15
Dave ReidIt 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.
Comment #16
Dave ReidComment #17
catchGlobal 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.
Comment #18
benjifisherI am going to write a change notice (my first) for this issue.
Comment #19
benjifisherSummary
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,
in the D7 version of
DrupalWebTestCase::drupalCreateUser()
has been replaced withMost 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.Comment #20
benjifisherHere 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:
Drupal 8
The User class has a defined list of properties:
Comment #21
xjmComment #22
xjmOops, xpost :)
Comment #23
podarokhttp://drupal.org/node/1941676 created from #20
Comment #24
andypostLooks fine, should be updated after #1818570: Convert users to the new Entity Field API lands
Comment #25.0
(not verified) CreditAttribution: commented...minor typo + convert linked issue to the [#...] format