Jump to content

What is the expected behavior of Page::getChanges() ? (cloning a matrix repeater)


da²
 Share

Recommended Posts

Hello,

I was working on a hook that updates a matrix repeater. I have another hook that should trigger when this repeater is changed, and I found it is not triggered.

So I thought that changes are not triggered by API changes (only user changes in admin), so that I should call the second hook myself.
But I did another test with "title" field, and found it is triggered by API change. ?‍♂️

I plan to report the issue on GitHub, but want to ask here before in case I'm missing something.

The following code updates the matrix repeater from another repeater, then I update title, then I check if repeater and title changes are tracked, and only title one is:

protected static function onSaveReady(HookEvent $event, EveningRacePage $page, mixed $eventObject): void
{
    /** @var RaceSessionsTemplatePage $sessionsTemplate */
    $sessionsTemplate = $page->raceSessionsTemplate;
    
    $page->raceSessions->removeAll();

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

    $page->title = $page->title . "1";
    $changes = $page->getChanges(true);

    wire()->log->message("title changed" . array_key_exists('title', $changes)); // 1
    wire()->log->message("raceSessions changed" . array_key_exists('raceSessions', $changes)); // 0
}

 

Link to comment
Share on other sites

  • da² changed the title to What is the expected behavior of Page::getChanges() ? (cloning a matrix repeater)

So it seems this is field assignation that triggers change detection, it works like that:

$pageSessions = $page->raceSessions;
$pageSessions->removeAll();

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

$page->raceSessions = $pageSessions; // Assignation triggers change detection

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...