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.
global $user is an abomination. Let us put it out of our misery.
Replace all references to global $user with $context['user']. Note that in most cases we'll need to call drupal_get_context() to get the object, and then access the user out of there. We also need a handler for the user object that wraps what the user init process does. Probably as a first cut it can just wrap global $user, but eventually the context handler should be what does the actual user initialization.
$grep -l -r "global.*user;" .
./lib/Drupal/Core/Cache/DatabaseBackend.php
./update.php
./includes/gettext.inc
./includes/theme.inc
./includes/form.inc
./includes/bootstrap.inc
./includes/file.inc
./includes/common.inc
./includes/install.core.inc
./includes/language.inc
./modules/toolbar/toolbar.module
./modules/system/system.api.php
./modules/system/system.module
./modules/system/tests/common.test
./modules/system/tests/modules/form_test/form_test.module
./modules/system/tests/file.test
./modules/php/php.module
./modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
./modules/field/field.module
./modules/node/node.pages.inc
./modules/node/node.module
./modules/poll/poll.module
./modules/filter/filter.module
./modules/overlay/overlay.module
./modules/comment/lib/Drupal/comment/CommentStorageController.php
./modules/comment/comment.module
./modules/forum/forum.module
./modules/statistics/statistics.module
./modules/shortcut/shortcut.admin.inc
./modules/shortcut/shortcut.module
./modules/user/user.pages.inc
./modules/user/user.module
./modules/language/language.negotiation.inc
./modules/contact/contact.module
./modules/contact/contact.pages.inc
./modules/file/file.module
$grep -r "global.*user;" . | wc -l
97
Comment | File | Size | Author |
---|---|---|---|
#4 | wscci-globaluser-0.patch | 1.35 KB | tamasd |
Comments
Comment #1
Crell CreditAttribution: Crell commentedNote that users as stdClass objects technically are not supported by context, but since we don't have the conversion of users to real classes yet we'll live with it for now so that we can make some progress.
Comment #3
tamasd CreditAttribution: tamasd commentedI am working on this issue, and we'll see how far I will get.
Comment #4
tamasd CreditAttribution: tamasd commentedFirst patch (far from being ready): adds ContextHandlerUser class and user_context_init().
Comment #5
tamasd CreditAttribution: tamasd commentedComment #6
catchSubscribing.
Comment #7
mr.moses CreditAttribution: mr.moses commented.
Comment #8
stevectorYoririou, any progress? In today's WSCCI meeting I said I'd work on this issue.
Pounard mentioned that this issue should work in tandem with #335411: Switch to Symfony2-based session handling
sdboyer volunteered to work refactoring the session handling.
Comment #9
pounardA quick and dirty design can be:
We inject a specific handler into root context (at bootstrap) for handling session.
This handler is lazy loaded when accessing the "user" context key.
It is able to detect if there's a session or not.
Then if context key is something like "user:authenticated" we only check session existence.
If context key is "user" we ask the session the user.
The session uses its backend (injected at bootstrap too) which loads the user and returns it.
Context -> UserContextHandler -> Session -> SessionBackendInterface
Comment #10
tamasd CreditAttribution: tamasd commentedI have a branch for this. The code mostly work, I was clicking around and I haven't found any bugs, but I haven't tested it properly with the test suite. If all tests pass then the code is ready to merge.
Comment #11
Crell CreditAttribution: Crell commentedI suspect the branch is stale now that the context system is using PSR-0 for autoloading, and we did some other refactoring. Steve, Sam, I'd suggest branching off of the existing branch and working from there.
Comment #12
Crell CreditAttribution: Crell commentedstevector, sdboyer, and progress here?
Comment #13
stevectorI'm getting errors when merging wscci into the 1260864 branch stemming from drupal_get_context().
Even in just the wscci branch I'm getting
when calling drupal_get_context().
Comment #14
pounardThe real class name is Drupal\Context\Context you should either:
Or
I personally prefer the first since the fully qualified class name will be resolved at compile time and not a runtime, so is more performant by definition.
Comment #15
Crell CreditAttribution: Crell commentedI've pushed a fix to change it to return \Drupal\Context\Context::getActiveContext(); Since it's a fully qualified name from the get-go it should not need to be re-resolved. That may change later if/when we add more namespace using to bootstrap.inc, but we still need to do a lot to rip that file apart before we can get there. I expect we will convert to the "use" version eventually.
Steve, back to you!
Comment #16
stevectorI have merged WSCCI into the branch that Yorirou started. So far so good. I don't think this should be merged back to the main wscci branch yet. Not all of the
global $user
calls are out yet and #335411: Switch to Symfony2-based session handling still needs move forward.Comment #17
cweaganssub.
Comment #18
webchickPer catch, and blessed by Larry, moving this and all other WSCCI issues to the Drupal core queue.
Comment #19
Niklas Fiekas CreditAttribution: Niklas Fiekas commentedNote from todays Windsprint: With one example conversion this would make a good novice task.
Comment #20
Crell CreditAttribution: Crell commentedActually this issue is dead, because this was for the stillborn "context API".
Comment #20.0
Crell CreditAttribution: Crell commentedAdd a list of files.