Skip to content

PHP error when events reference a non-existent Smart Date recurrence #5259

@bberndt-uaz

Description

@bberndt-uaz

Problem/Motivation

In certain circumstances, AZ Event nodes can reference Smart Date recurrence rules which have been deleted from the database. Visiting the node page results in a PHP error such as this:

Error: Call to a member function get() on null in az_event_get_recurrence_text() (line 323 of /usr/local/quickstart-install-profile/modules/custom/az_event/az_event.module).

Describe the bug

PHP errors can be caused by a lack of null checking of the Smart Date rule for an event in the az_event.module file.

One instance is on line 322:

/** @var \Drupal\smart_date_recur\Entity\SmartDateRule $smart_date_rule */
$smart_date_rule = \Drupal::entityTypeManager()->getStorage('smart_date_rule')->load($rule_index);
$start_date = (new \DateTime)->setTimestamp($smart_date_rule->get('start')[0]->getString());

A second instance is on line 216:

/** @var \Drupal\smart_date_recur\Entity\SmartDateRule $smart_date_rule */
$smart_date_rule = \Drupal::entityTypeManager()->getStorage('smart_date_rule')->load($event_rrules[0]);
$are_occurrences_modified = empty($smart_date_rule->getRuleOverrides()) ? FALSE : TRUE;

In both cases, the $smart_date_rule variable can be null, causing a white screen PHP error to be displayed.

To Reproduce

The field_az_event_date_rrule field of an AZ Event node can reference a non-existent Smart Date recurrence rule when an event node is cloned and then the recurrence is removed from the original node or the cloned node.

For example, if the cloned event node is edited to remove the recurrence, then upon saving that node, the reference to the recurrence rule is removed AND the recurrence rule itself is deleted from the smart_date_rule table in the database. The field_az_event_date_rrule field of the original node still references that deleted recurrence rule, resulting in errors. Editing the original node and saving it again resolves the issue, since it clears that field.

Note: This behavior can also result in changes to the dates of the original event if the cloned node recurrence is modified rather than deleted.

Steps to reproduce the behavior:

  1. Create an event with a recurrence.
  2. Clone the event and save it.
  3. Edit the cloned event and remove the recurrence (set Repeats to "never" and save the event).
  4. Clear cache.
  5. Visit the page for the original event and observe the white screen error.

Proposed resolution

  1. Add null checking when loading Smart Date rules to prevent the PHP errors.
  2. Follow up with a separate issue and/or updates to documentation to warn about potential issues when cloning Quickstart events that use recurrences.

Metadata

Metadata

Assignees

Labels

patch releaseIssues to be included in the next patch release

Type

Projects

Status

Done

Status

Done

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions