Skip to content

Commit

Permalink
fix(versioned): Fix for change in ss behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco (Valandur) committed Aug 19, 2019
1 parent b99694a commit 0c33dfe
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/PageSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function get_extra_config($class = null, $extensionClass = null)
}

foreach ($sections as $section) {
$name = "PageSection".$section;
$name = "PageSection" . $section;
$has_one[$name] = PageSection::class;

$names[] = $name;
Expand Down Expand Up @@ -68,7 +68,7 @@ public function getAllowedPageElements($section = "Main")
$classes = array_values(ClassInfo::subclassesFor(PageElement::class));
$classes = array_diff($classes, [PageElement::class]);
$ret = [];
foreach($classes as $class) {
foreach ($classes as $class) {
if ($class::$canBeRoot) $ret[] = $class;
}
return $ret;
Expand All @@ -95,7 +95,7 @@ public function onAfterWrite()
$sections = $this->getPageSectionNames();

foreach ($sections as $sectionName) {
$name = "PageSection".$sectionName;
$name = "PageSection" . $sectionName;

if (!$this->owner->{$name . "ID"}) {
// Restore or create a page section if we don't have one yet
Expand All @@ -113,18 +113,19 @@ public function onBeforeArchive()
$sections = $this->getPageSectionNames();

foreach ($sections as $sectionName) {
$name = "PageSection".$sectionName;
$name = "PageSection" . $sectionName;
$this->owner->{$name . "ID"} = 0;
}
}

public function updateCMSFields(FieldList $fields) {
public function updateCMSFields(FieldList $fields)
{
$sections = $this->getPageSectionNames();

$fields->removeByName("__PageSectionCounter");

foreach ($sections as $sectionName) {
$name = "PageSection".$sectionName;
$name = "PageSection" . $sectionName;

$fields->removeByName($name);
$fields->removeByName($name . "ID");
Expand Down Expand Up @@ -189,15 +190,17 @@ public function __call($method, $arguments)
}
}

private function restoreOrCreate($sectionName) {
private function restoreOrCreate($sectionName)
{
if (mb_strpos($sectionName, "PageSection") !== false) {
throw new \Error("PageSection name should not contain 'PageSection' when restoring or creating!");
}

$name = "PageSection" . $sectionName;

// Try restoring the section from the history
$archived = Versioned::get_latest_version(PageSection::class, $this->owner->{$name . "ID"});
$id = $this->owner->{$name . "ID"};
$archived = $id ? Versioned::get_latest_version(PageSection::class, $id) : null;
if ($archived && $archived->ID) {
// Update the back references
$archived->ID = $this->owner->{$name . "ID"};
Expand Down

0 comments on commit 0c33dfe

Please sign in to comment.