public function restoreFromHistory(SectionInterface $sectionFromHistory): SectionInterface
    {
        /** @var SectionInterface $activeSection */
        $activeSection = $this->readByHandle($sectionFromHistory->getHandle()); // 1

        /** @var SectionInterface $newSectionHistory */
        $newSectionHistory = $this->copySectionDataToSectionHistoryEntity($activeSection); // 2
        $newSectionHistory->setVersioned(new \DateTime()); // 3
        $version = $this->getHighestVersion($newSectionHistory->getHandle());
        $newSectionHistory->setVersion(1 + $version->toInt());
        $this->sectionHistoryManager->create($newSectionHistory); // 3

        $updatedActiveSection = $this->copySectionHistoryDataToSectionEntity($sectionFromHistory, $activeSection); // 4

        $updatedActiveSection->removeFields(); // 5
        $this->entityManager->persist($updatedActiveSection);
        $this->entityManager->flush();

        $fields = $this->fieldManager->readByHandles($updatedActiveSection->getConfig()->getFields()); // 6

        foreach ($fields as $field) {
            $updatedActiveSection->addField($field);
        } // 7

        $this->entityManager->persist($updatedActiveSection);
        $this->entityManager->flush();

        return $updatedActiveSection;
    }
public function restoreFromHistory(SectionInterface $sectionFromHistory): SectionInterface
    {
        /** @var SectionInterface $activeSection */
        $activeSection = $this->readByHandle($sectionFromHistory->getHandle()); // 1

        /** @var SectionInterface $newSectionHistory */
        $newSectionHistory = $this->copySectionDataToSectionHistoryEntity($activeSection); // 2
        $newSectionHistory->setVersioned(new \DateTime()); // 3
        $version = $this->getHighestVersion($newSectionHistory->getHandle());
        $newSectionHistory->setVersion(1 + $version->toInt());
        $this->sectionHistoryManager->create($newSectionHistory); // 3

        $updatedActiveSection = $this->copySectionHistoryDataToSectionEntity($sectionFromHistory, $activeSection); // 4

        $updatedActiveSection->removeFields(); // 5
        $this->entityManager->persist($updatedActiveSection);
        $this->entityManager->flush();

        $fields = $this->fieldManager->readByHandles($updatedActiveSection->getConfig()->getFields()); // 6

        foreach ($fields as $field) {
            $updatedActiveSection->addField($field);
        } // 7

        $this->entityManager->persist($updatedActiveSection);
        $this->entityManager->flush();

        return $updatedActiveSection;
    }
public function restoreFromHistory ( SectionInterface $ sectionFromHistory ) : SectionInterface { /** @var SectionInterface $activeSection */ $ activeSection = $ this -> readByHandle ( $ sectionFromHistory -> getHandle ( ) ) ; // 1 /** @var SectionInterface $newSectionHistory */ $ newSectionHistory = $ this -> copySectionDataToSectionHistoryEntity ( $ activeSection ) ; // 2 $ newSectionHistory -> setVersioned ( new \ DateTime ( ) ) ; // 3 $ version = $ this -> getHighestVersion ( $ newSectionHistory -> getHandle ( ) ) ; $ newSectionHistory -> setVersion ( 1 + $ version -> toInt ( ) ) ; $ this -> sectionHistoryManager -> create ( $ newSectionHistory ) ; // 3 $ updatedActiveSection = $ this -> copySectionHistoryDataToSectionEntity ( $ sectionFromHistory , $ activeSection ) ; // 4 $ updatedActiveSection -> removeFields ( ) ; // 5 $ this -> entityManager -> persist ( $ updatedActiveSection ) ; $ this -> entityManager -> flush ( ) ; $ fields = $ this -> fieldManager -> readByHandles ( $ updatedActiveSection -> getConfig ( ) -> getFields ( ) ) ; // 6 foreach ( $ fields as $ field ) { $ updatedActiveSection -> addField ( $ field ) ; } // 7 $ this -> entityManager -> persist ( $ updatedActiveSection ) ; $ this -> entityManager -> flush ( ) ; return $ updatedActiveSection ; }
Restore an old version of a section by it's handle and version

1. Fetch the currently active section
2. Copy the section history data to the active section entity, including the version
3. Move the currently active section to history
4. Clean the field associations from the updated active section
5. Fetch the fields from the 'old' section config
6. Assign the fields to the section
7. Persist the active section

This section might be generated from an updated config yml. Meaning the current config might
not comply with the active section configuration anymore.

This will only update the config stored in the database. The generators will have to be called
to make the version change complete.

@param $sectionFromHistory
@return SectionInterface
Restore an old version of a section by it s handle and version