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.
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;
}
Comment | File | Size | Author |
---|---|---|---|
#22 | 3073942-22.patch | 6.74 KB | jsacksick |
| |||
#18 | 3073942-18.patch | 4.13 KB | jsacksick |
| |||
#14 | 3073942-14.patch | 760 bytes | jsacksick |
| |||
#10 | 3073942-10.patch | 20.24 KB | jsacksick |
| |||
#10 | interdiff_9-10.txt | 621 bytes | jsacksick |
Comments
Comment #2
mglamanComment #3
jorgik CreditAttribution: jorgik at AnyforSoft, Drupal Ukraine Community commentedHi @mglaman, here is the path. Could you please check it.
Comment #4
jorgik CreditAttribution: jorgik at AnyforSoft, Drupal Ukraine Community commentedComment #5
jorgik CreditAttribution: jorgik at AnyforSoft, Drupal Ukraine Community 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 CreditAttribution: 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 CreditAttribution: jorgik at AnyforSoft, Drupal Ukraine Community 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 CreditAttribution: jsacksick at Centarro 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 CreditAttribution: jsacksick at Centarro commentedComment #12
jsacksick CreditAttribution: jsacksick at Centarro commentedThanks, Committed!
Comment #13
jsacksick CreditAttribution: jsacksick at Centarro 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 CreditAttribution: jsacksick at Centarro 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 CreditAttribution: jsacksick at Centarro commentedComment #16
jsacksick CreditAttribution: jsacksick at Centarro 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 CreditAttribution: jsacksick at Centarro commentedWrong update hook name for the payment hook.
Comment #18
jsacksick CreditAttribution: jsacksick at Centarro commentedUpdated the deprecation notice to mention Commerce instead of core.
Comment #19
jsacksick CreditAttribution: jsacksick at Centarro 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 CreditAttribution: jsacksick at Centarro 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 CreditAttribution: jsacksick at Centarro commentedHopefully the last iteration.
Comment #24
jsacksick CreditAttribution: jsacksick at Centarro commentedI was able to test this locally, so I went ahead and committed it.
Comment #26
jsacksick CreditAttribution: jsacksick at Centarro commented