Unable to modify a cloned node

Hello, I want to set up a clone of my Node objects and add additional properties for display.

$node = flow-query...
$cloneNode = (clone $node)->setProperty('new_field', $new_value);

However I get this error that I don’t know how to fix: Detected modified or new objects (Neos\ContentRepository\Domain\Model\NodeData, uuid:4a72e159-7cee-48f5-83f7-231f12f93f98) to be persisted which is not allowed for "safe requests".

Need help.

that doesnt seem right … what do you want to archive :wink: (see https://xyproblem.info/ ^^)

1 Like

Good evening, sorry for the delay. I manage events that have a start date and an end date.
From these two dates, I must at the level of my view display the event at the interval time(DatePeriod) that there is.

Example : startDate : 24.02.2022, endDate : 28.02.22
I must have on my view : eventName => 24.02.22, eventName => 25.02.2022, ... eventName => 28.02.2022

So I implemented an AbstractFusionObject in php to do this, and I tried to loop through the list of my events and make a clone of each event by adding the displayDate property.

Hence the error described above.

Here is the solution I implemented for the problem. Is it effective, I can’t say. Is it the best way, not sure too.

foreach ($eventNodes as $eventNode) {
            if ($eventNode->getProperty('isTimePeriod')) {
                $dates = iterator_to_array($this->extractIntervalDates($eventNode->getProperty('startDate'), $eventNode->getProperty('endDate')));
                $result = $this->duplicateEventForDates($eventNode, $dates, $result, self::CHECK_WEEKS);
            }  else {
                $result[] = [
                    'event' => $eventNode,
                    'displayDate' => $eventNode->getProperty('startDate')
                ];
            }
        }

duplicateEventForDates method :

private function duplicateEventForDates(NodeInterface $eventNode, array $dates, array $result, bool $mustCheckWeeks = false): array
    {
        $datesCollection = DateCollection::collect($dates);

        if ($mustCheckWeeks) {
            $datesCollection->filter(fn($date) => ($date > new DateTimeImmutable('now')) && (in_array($date->format('w'), $this->getEventWeeks($eventNode))));
        } else {
            $datesCollection->filter(fn($date) => $date > new DateTimeImmutable('now'));
        }

        $datesCollection
            ->sort(fn(DateTimeImmutable $firstDate, DateTimeImmutable $secondDate) => $firstDate->getTimestamp() - $secondDate->getTimestamp())
            ->forEachDate(function ($date) use (&$result, $eventNode) {
                array_push($result, [
                    'event' => $eventNode,
                    'displayDate' => $date
                ]);
            });
        return $result;
    }