Hi all!

I have a weird behaviour in the latest site I have updated to the latest version.
After updating to bootstrap 8.x-3.19, in this site, if I go to /user/register I get the following error

The website encountered an unexpected error. Please try again later.

In the logs, the error I get says the following:

Error: Call to a member function id() on null in Drupal\bootstrap\Bootstrap::isAdmin() (line 1247 of .../web/themes/contrib/bootstrap/src/Bootstrap.php) #0 .../web/themes/contrib/bootstrap/src/Bootstrap.php(1373): Drupal\bootstrap\Bootstrap::isAdmin() #1......

In the line of the error we can find

...
(1246)  $user = $user ?: User::load(\Drupal::currentUser()->id());
(1247)  $uid = (int) $user->id();
...

If we check the drupal documentation (https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...), if the user is anonymous (well, he is going to create a new account...), then the User::load returns null and the next line is the one that fails as the $user var is null.

Shouldn't be a check to see if $user is null and return false before checking the id() part?

Comments

gorkagr created an issue. See original summary.

gorkagr’s picture

StatusFileSize
new590 bytes

After testing a bit in the morning, i can say that checking if the $user exists or not and returning false fixes this small issue.
No idea why is this happening only in one site and not in the others (can be a combination of modules used most likely in the one failing)

Here is a simple patch that works for me and allows the users to register in my site.

markhalliwell’s picture

Version: 8.x-3.19 » 8.x-3.x-dev
Status: Active » Needs work
  1. +++ b/src/Bootstrap.php
    @@ -1243,7 +1243,10 @@ class Bootstrap {
    -    $user = $user ?: User::load(\Drupal::currentUser()->id());
    +    $user = $user ? $user : User::load(\Drupal::currentUser()->id());
    

    This change doesn't make sense.

    $user ? $user is redundant because that is what ?: is for: assigns the $user object if it exists, otherwise it loads the current user.

  2. +++ b/src/Bootstrap.php
    @@ -1243,7 +1243,10 @@ class Bootstrap {
    +    if (!$user) {
    +      return false;
    +    }
    

    Should be FALSE. Also, I'd add some blank lines around the if statement block here with a comment above it explaining when this condition can occur.

gorkagr’s picture

StatusFileSize
new607 bytes

hi!

Thnks for the reply.

I didnt know you could ommit the first part in the ?: (i was always adding it).

I have changed the false and added a comment before. Check if it is ok.

geffio’s picture

I was having this issue with one site I'm working on, and can confirm #4 this fixes it for me.

Thanks!

markhalliwell’s picture

Title: isAdmin() and error when creating accounts » Error: Call to a member function id() on null in Bootstrap::isAdmin()
Assigned: Unassigned » markhalliwell

Actually, I think we can go a different route altogether that will make more sense, logically.

markhalliwell’s picture

Assigned: markhalliwell » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.09 KB

Please test this patch instead. It avoids User::load() altogether and will instead, ultimately, use \Drupal\Core\Session\AnonymousUserSession for anonymous users. Thus, allowing us to always have some sort of object that will return an ID.

gorkagr’s picture

Just tested #7.
Works fine and no issues when requesting the page.

Not related, but i have just noticed some issues with the CSS (images) loaded via the jsdelivr.net

  • markcarver committed 54b7aa0 on 8.x-4.x
    Issue #3057674 by markcarver, gorkagr: Error: Call to a member function...

  • markcarver committed fa37b6d on 8.x-3.x
    Issue #3057674 by markcarver, gorkagr: Error: Call to a member function...
markhalliwell’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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