I'm in the habit of removing unnecessary fields from Drupal entities, and this usually includes the user picture field from the User entity. However doing that breaks Discourse SSO with the error:
NOTICE: PHP message: Error: Call to a member function isEmpty() on null in modules/contrib/discourse_sso/src/Controller/DiscourseSsoController.php on line 59
which refers to the code:
$picture = (!$account->user_picture->isEmpty())
? $account->get('user_picture')->entity->url()
: '';
I've made a quick patch to fix the issue by checking if $account->user_picture is set before checking if it is empty:
$picture = (isset($account->user_picture) && !$account->user_picture->isEmpty())
? $account->get('user_picture')->entity->url()
: '';
Comments
Comment #2
Pls commentedHey Scott,
Nice patch. Just tested and it works perfectly! Cheers!
Comment #3
Pls commentedComment #4
jurgenhaasWe have got the same issue and the patch does not solve the problem, we're getting this:
Reworked the patch and would be glad to get it reviewed.
Comment #7
jurgenhaasSorry, the condition needs to be negated.
Comment #10
jurgenhaasThere has been another issue in the latest code. Calling
url()on an entity is running into an early render scenario because generating urls needs to be aware of context (e.g. language) which may use caching. To avoid this we need to calltoUrl()-toString(TRUE)instead which by the same time also avoids the usage of long deprecated functions.Comment #11
ugolek commentedI think the better to check if $account has field user_picture before trying to get the field value. I have attached updated patch.
Comment #12
jurgenhaasComment #14
jurgenhaasSlightly changed the patch from @lebster and committed to latest dev release.