Problem/Motivation
I'm facing an error when I try to register to an event instance.
Error: Call to a member function getCacheTagsToInvalidate() on null in Drupal\recurring_events_registration\Entity\Registrant->getCacheTagsToInvalidate() (line 397 of modules/contrib/recurring_events/modules/recurring_events_registration/src/Entity/Registrant.php).
Drupal\Core\Entity\EntityBase->getCacheTags() (Line: 18)
Drupal\Core\Cache\CacheableMetadata->addCacheableDependency(Object) (Line: 96)
Drupal\Core\Render\BubbleableMetadata->addCacheableDependency(Object) (Line: 360)
Drupal\Core\Utility\Token->generate('registrant', Array, Array, Array, Object) (Line: 392)
Drupal\eca\Token\ContribToken->generate('registrant', Array, Array, Array, Object) (Line: 241)
Drupal\Core\Utility\Token->doReplace(1, '[registrant:email]', Array, Array, Object) (Line: 191)
Drupal\Core\Utility\Token->replace('[registrant:email]', Array, Array, NULL) (Line: 403)
Drupal\eca\Token\ContribToken->replace('[registrant:email]', Array) (Line: 25)
Drupal\recurring_events_registration\Plugin\Field\ComputedRegistrantTitleFieldItemList->computeValue() (Line: 34)
Drupal\recurring_events_registration\Plugin\Field\ComputedRegistrantTitleFieldItemList->ensureComputedValue() (Line: 141)
Drupal\recurring_events_registration\Plugin\Field\ComputedRegistrantTitleFieldItemList->count() (Line: 41)
Symfony\Component\Validator\Constraints\CountValidator->validate(Object, Object) (Line: 202)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateConstraints(Object, '0000000000000edd0000000000000000', Array) (Line: 154)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object) (Line: 164)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validateNode(Object, Array, 1) (Line: 106)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator->validate(Object, NULL, NULL) (Line: 93)
Drupal\Core\TypedData\Validation\RecursiveValidator->validate(Object) (Line: 132)
Drupal\Core\TypedData\TypedData->validate() (Line: 518)It seems that the code producing the error was introduced here: https://www.drupal.org/project/recurring_events/issues/3454012
When I debug the code, I see that the registration type and event instance are NULL

| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3462480-registrant-getCacheTagsToInvalidate-error-5.patch | 1.51 KB | chrisla |
Issue fork recurring_events-3462480
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
carolpettirossi commentedComment #3
chrisla commentedI am experiencing this error when registering for an event instance:
Error: Call to a member function getCacheTagsToInvalidate() on null in Drupal\recurring_events_registration\Entity\Registrant->getCacheTagsToInvalidate() (line 397 of modules/contrib/recurring_events/modules/recurring_events_registration/src/Entity/Registrant.php).This line refers to:
$this->getEventInstance()->getCacheTagsToInvalidate(),The event instance is not being found. Checking
getEventInstance()and see it is justreturn $this->get('eventinstance_id')->entity;which means that the eventinstance_id must not be getting set for the registrant.Comment #4
chrisla commentedDouble-checked that this error happens still with custom guest registration disabled (see https://www.drupal.org/project/recurring_events/issues/3257502#comment-1...) and it does still occur (previous test was wrong, so I am editing my comment here).
Is this error also caused by the registration creation service no longer sharing the event details by default -- https://www.drupal.org/project/recurring_events/issues/3452632
Comment #5
chrisla commentedInside Registrant::getCacheTagsToInvalidate(),
$this->getEventInstance()is always returning NULL for me.getCacheTagsToInvalidate() is called via invalidateTagsOnSave(), which happens in entity's postSave() method. During postSave() I can see
$this->getEventInstance()does return the expected value.At some point EntityBase::getCacheTags() is called, which also calls getCacheTagsToInvalidate(). If getCacheTags() is commented out, the error is no longer triggered. This seems to be the problem point.
If I provide the eventinstance and eventseries directly to either getCacheTags() or getCacheTagsToInvalidate(), then the error is not triggered. For now, this patch stops the error from occurring, but this seems like a weird, unnecessary fix and badly coded so this is just a stopgap solution for me to keep working.
Comment #6
pfrenssenI am seeing this error as well, in my case I somehow have an orphaned registrant that is referencing an event instance that no longer exists. I also see other errors in my logs relating to the same issue. Everywhere we are calling
::getEventInstance()we are assuming that we will always get an event instance object back, but this is not guaranteed:Registrantentity the event instance might not yet have been set. This is possibly what is causing the problem reported byeventinstance_idis not set as a required field, so it is possible to save registrants without referencing an event instance.I think the best solution is probably to change the documentation of the method to reflect the fact that it can return
NULLand fix all calling code to check the value.Comment #7
pfrenssenPicking this up. Promoting to major since it is blocking registrations.
Comment #8
chrisla commented@pfrenssen is the bug caused by change in https://www.drupal.org/project/recurring_events/issues/3452632 do you think?
Comment #9
pfrenssenNo it is caused by #3454012: Make sure event instances are invalidated when registrants change. The method
::getCacheTagsToInvalidate()was assuming that there is always an event instance referenced (as was implied by the method signature), but this is not the case. We should just check that it exists before calling methods on it.Comment #11
pfrenssenStarted an MR with a test that shows the bug (ref. https://git.drupalcode.org/issue/recurring_events-3462480/-/jobs/2277646)
Now let's fix it. I will do the following:
::getCacheTagsToInvalidate().::getEventInstance()so that it is clear in the future that this potentially returnsNULL.::getEventSeries()which also potentially returnsNULL.NULLbefore calling methods on it.Comment #12
pfrenssenComment #14
pfrenssenAlso added an MR for 3.0.x
Comment #15
pfrenssenI am testing in my project. I am seeing another error with an orphaned event registration:
Error: Call to a member function id() on null in recurring_events_registration_entity_operation() (line 440 of modules/contrib/recurring_events/modules/recurring_events_registration/recurring_events_registration.module).Comment #16
pfrenssenComment #19
pfrenssenComment #21
chrisla commentedI have tested against 2.0.2 version of module and found this solved this bug of upgrading.
I would suggest future fixes like this are tested and merged into both branches perhaps, rather than just release for the 3.0 version that won't be released for a while. Could this be merged into 2.0 branch as well so a patch is not required?
Comment #22
ansorg commentedI run 2.0.2 on Drupal 10 and still get this error when trying to register on a event instance.
What am I missing, regarding the fix?
Thanks
Comment #23
yesct commentedWhen is the next release planned so folks can benefit from this fix without a patch or using dev versions?