Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom event behaviors are attached to occurrence clones #150

Open
mikejpeters opened this issue Jul 19, 2022 · 1 comment
Open

Custom event behaviors are attached to occurrence clones #150

mikejpeters opened this issue Jul 19, 2022 · 1 comment
Assignees
Labels
issue Something isn't working correctly

Comments

@mikejpeters
Copy link
Contributor

Describe the bug or issue you're experiencing

When using a custom behavior to add functionality to the Event class,
the behavior gets assigned to clones of the event during calls to $event->getOcurrence()

This means that if the behavior relies on the startDate or endDate properties of it's owner,
it no longer functions correctly because it's attached to a cloned occurrence where these properties have been modified.

Steps to reproduce

  1. Create a custom behavior and register it:

    use yii\base\Behavior;
    
    class EventBehavior extends Behavior
    {
        public function getStartDateInSiteTimezone(): ?Carbon
        {
            $date = $this->owner->getStartDate()->copy();
            $timezone = Craft::$app->config->general->timezone;
            return $date->shiftTimezone($timezone);
        }
    }
    Event::on(CalendarEvent::class, CalendarEvent::EVENT_DEFINE_BEHAVIORS, function (DefineBehaviorsEvent $e) {
        $e->behaviors[] = EventBehavior ::class;
    });
  2. Create an event with multiple occurrences

  3. In code, access the event, call getOccurrences(), and then try to use the behavior:

    $event->getOccurrences();
    echo $event->getStartDateInSiteTimezone();
  4. The result will be the start date of the last occurrence, because calling getOccurrences() has the side-effect of attaching the existing behavior to the occurrence instead of it's original owner

Expected behavior

Calling getOccurrences() should not interfere with custom behaviors.

Craft & Plugin Info (please complete the following information):

  • Craft Version: 3.7.44
  • Calendar Version: 3.3.12
  • Calendar Edition: Pro
  • Fresh Install or Upgrade:

Additional context

The code that re-attaches the behaviors can be found here:

foreach ($this->getBehaviors() as $key => $value) {
$clone->attachBehavior($key, $value);
}

@mikejpeters mikejpeters added the issue Something isn't working correctly label Jul 19, 2022
@jesuismaxime
Copy link

Any release date related to that issue? I think this cause a major major issue : resaving a calendar resave all occurences of all events of this calendar. Causing Craft to crash because it needs to save 1700 elements for a calendar containing 70 events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue Something isn't working correctly
Development

No branches or pull requests

3 participants