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

Screenshot swhoing debug values

Command icon 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

carolpettirossi created an issue. See original summary.

carolpettirossi’s picture

Issue summary: View changes
StatusFileSize
new103.04 KB
chrisla’s picture

I 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 just return $this->get('eventinstance_id')->entity; which means that the eventinstance_id must not be getting set for the registrant.

chrisla’s picture

Double-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

chrisla’s picture

Inside 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.

pfrenssen’s picture

I 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:

  1. On a newly created Registrant entity the event instance might not yet have been set. This is possibly what is causing the problem reported by
  2. The field eventinstance_id is 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 NULL and fix all calling code to check the value.

pfrenssen’s picture

Version: 2.0.2 » 2.0.x-dev
Assigned: Unassigned » pfrenssen
Priority: Normal » Major

Picking this up. Promoting to major since it is blocking registrations.

chrisla’s picture

@pfrenssen is the bug caused by change in https://www.drupal.org/project/recurring_events/issues/3452632 do you think?

pfrenssen’s picture

@pfrenssen is the bug caused by change in https://www.drupal.org/project/recurring_events/issues/3452632 do you think?

No 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.

pfrenssen’s picture

Started an MR with a test that shows the bug (ref. https://git.drupalcode.org/issue/recurring_events-3462480/-/jobs/2277646)

1) Drupal\Tests\recurring_events_registration\Kernel\RegistrantTest::testGetCacheTagsToInvalidate with data set "newly created, unsaved registrant" (false, false, false)
Error: Call to a member function getCacheTagsToInvalidate() on null

Now let's fix it. I will do the following:

  1. Fix the bug in ::getCacheTagsToInvalidate().
  2. Update the documentation and method signature of ::getEventInstance() so that it is clear in the future that this potentially returns NULL.
  3. I will do the same for ::getEventSeries() which also potentially returns NULL.
  4. Go through the code and fix cases where the calling code should check if the return value is NULL before calling methods on it.
pfrenssen’s picture

Assigned: pfrenssen » Unassigned
Status: Active » Needs review

pfrenssen’s picture

Also added an MR for 3.0.x

pfrenssen’s picture

Assigned: Unassigned » pfrenssen
Status: Needs review » Needs work

I 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).

pfrenssen’s picture

Assigned: pfrenssen » Unassigned
Status: Needs work » Needs review

  • pfrenssen committed 3f0edec2 on 2.0.x
    Issue #3462480 by pfrenssen, chrisla, carolpettirossi: Error when...

  • pfrenssen committed 3f0edec2 on 3.0.x
    Issue #3462480 by pfrenssen, chrisla, carolpettirossi: Error when...
pfrenssen’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

chrisla’s picture

I 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?

ansorg’s picture

I 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

yesct’s picture

When is the next release planned so folks can benefit from this fix without a patch or using dev versions?