processwire-issues icon indicating copy to clipboard operation
processwire-issues copied to clipboard

RepeaterMatrixPageArray: removeAll() not working if we set field content just after.

Open hiboudev opened this issue 2 years ago • 0 comments

Short description of the issue

In this hook, removeAll() is not removing until I comment out last line (set clone). Actually after page saved, repeater contains new items AND previous items. I can't find a way to remove all.

protected static function onSaveReady(HookEvent $event, EveningRacePage $page, mixed $eventObject): void
{
    if (!self::getChanges($page)->has('raceSessionsTemplate')) {
        return;
    }

    /** @var RaceSessionsTemplatePage $sessionsTemplate */
    $sessionsTemplate = $page->raceSessionsTemplate;

    $page->raceSessions->removeAll(); // removeAll() alone works...
//        $page->raceSessions = null;
    $page->raceSessions = clone $sessionsTemplate->raceSessions; // ... but it doesn't work when this line is there.
}

This is probably the same issue as in this report: https://github.com/processwire/processwire-issues/issues/1959

Expected behavior

Having only new items.

Actual behavior

Having both previous and new items.

Optional: Screenshots/Links that demonstrate the issue

  1. Field before page save: image
  2. Field I try to import to replace all items: image
  3. Result: image

Workaround

$page->raceSessions->removeAll();

foreach ($sessionsTemplate->raceSessions as $raceSession){
    $page->raceSessions->add($raceSession);
}

Setup/Environment

  • ProcessWire version: 3.0.228

hiboudev avatar Dec 13 '23 20:12 hiboudev