There are chances that getOwner can return a null value if the referenced user has been deleted. The Comment entity does the following to ensure a user object is returned, even if it is just an anonymous user.
/**
* {@inheritdoc}
*/
public function getOwner() {
$user = $this->get('uid')->entity;
if (!$user || $user->isAnonymous()) {
$user = User::getAnonymousUser();
$user->name = $this->getAuthorName();
$user->homepage = $this->getHomepage();
}
return $user;
}
We can provide the same checks, if !$user return User::getAnonymousUser. Would make a lot of checks simpler.
See Order
/**
* {@inheritdoc}
*/
public function getCustomer() {
$customer = $this->get('uid')->entity;
// Handle deleted customers.
if (!$customer) {
$customer = User::getAnonymousUser();
}
return $customer;
}
Comments
Comment #2
mglamanComment #3
jorgik commentedHi @mglaman, here is the path. Could you please check it.
Comment #4
jorgik commentedComment #5
jorgik commentedAdded some changes in tests, it was made the same way as in OrderTest for non-existent/deleted user ID case.
Comment #6
mglamanNice! And you updated the tests, great. This looks good, I'll take some time to review and make sure we didn't miss any entities.
Comment #7
bojanz commentedWe have this in Product/Store:
Guessing it will no longer trigger. This was a fix for a crash (where saving a NULL as the uid wasn't accepted), our test coverage is either incomplete, or the crash is no longer a problem. Will need to doublecheck.
Comment #8
jorgik commentedI suppose that for User::getAnonymousUser(); a crash it's not a problem for now. Do I need to change something according to that in the patch?
Comment #9
jsacksick commentedOk, so the attached patch introduced an EntityOwnerTrait in Commerce that basically uses core's EntityOwnerTrait and simply override
getOwner()so we don't have to duplicate this logic everywhere.I also replaced:
by
and added tests coverage for that logic that we were missing.
Should we rename our EntityOwnerTrait to CommerceEntityOwnerTrait? Or just keep it as is?
EntityOwnerTrait has been introduced in Drupal core 8.7 so it's safe to use.
Comment #10
jsacksick commentedComment #12
jsacksick commentedThanks, Committed!
Comment #13
jsacksick commentedWarning: call_user_func() expects parameter 1 to be a valid callback, class 'Drupal\commerce_product\Entity\Product' does not have a method 'getCurrentUserId' in Drupal\Core\Field\FieldConfigBase->getDefaultValue() (line 397 of core/lib/Drupal/Core/Field/FieldConfigBase.php).
I opened a products view, and got this warning, so this means we probably need an update hook.
Comment #14
jsacksick commentedI've been trying to fix this via an update hook and couldn't really manage, (because base fields are overridden on my install...
So I'm wondering if our EntityOwnerTrait shouldn't provide a
getCurrentUserId()method for BC reason.And if we do, should we still do the following?
Comment #15
jsacksick commentedComment #16
jsacksick commentedSame patch, with update hooks for each entity type.
With that patch, I'm still experiencing issues because of the base fields overrides (that is without the
getCurrentUserId()method.Comment #17
jsacksick commentedWrong update hook name for the payment hook.
Comment #18
jsacksick commentedUpdated the deprecation notice to mention Commerce instead of core.
Comment #19
jsacksick commentedNot committing this yet until I figure out what to do with base field overrides.
Comment #20
mglamanThey're config, so we could just edit, correct? We should add a test module that installs a current base field override and then we can test the upgrade.
Comment #21
jsacksick commentedOk, the attached patch is taking care of the base field overrides which removes the need for
getCurrentUserId(). I manually tested it on a dev install and the warning was gone after running the update and after clearing caches.Comment #22
jsacksick commentedHopefully the last iteration.
Comment #24
jsacksick commentedI was able to test this locally, so I went ahead and committed it.
Comment #26
jsacksick commented