-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(page-sections): Add page section class to fix polymorphic issues
- Loading branch information
Marco Crespi
committed
Jun 4, 2018
1 parent
2250174
commit 53d48de
Showing
7 changed files
with
238 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace FLXLabs\PageSections; | ||
|
||
use SilverStripe\CMS\Model\SiteTree; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Forms\Form; | ||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; | ||
use SilverStripe\Forms\GridField\GridFieldConfig; | ||
use SilverStripe\Forms\GridField\GridFieldButtonRow; | ||
use SilverStripe\Forms\GridField\GridFieldToolbarHeader; | ||
use SilverStripe\Forms\GridField\GridFieldDataColumns; | ||
use SilverStripe\Forms\GridField\GridFieldDetailForm; | ||
use SilverStripe\Forms\GridField\GridField; | ||
use SilverStripe\Versioned\Versioned; | ||
|
||
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; | ||
|
||
class PageSection extends DataObject { | ||
|
||
private static $table_name = "FLXLabs_PageSections_PageSection"; | ||
|
||
private static $db = [ | ||
"__Counter" => "Int", | ||
]; | ||
|
||
private static $has_one = [ | ||
"Page" => SiteTree::class, | ||
]; | ||
|
||
private static $owns = [ | ||
"Elements", | ||
]; | ||
|
||
private static $cascade_deletes = [ | ||
"Elements", | ||
]; | ||
|
||
private static $many_many = [ | ||
"Elements" => [ | ||
"through" => PageSectionPageElementRel::class, | ||
"from" => "PageSection", | ||
"to" => "Element" | ||
] | ||
]; | ||
|
||
public function onBeforeWrite() { | ||
parent::onBeforeWrite(); | ||
|
||
$elems = $this->Elements()->Sort("SortOrder")->Column("ID"); | ||
$count = count($elems); | ||
for ($i = 0; $i < $count; $i++) { | ||
$this->Elements()->Add($elems[$i], [ "SortOrder" => ($i + 1) * 2, "__NewOrder" => true ]); | ||
} | ||
} | ||
|
||
public function onAfterWrite() { | ||
parent::onAfterWrite(); | ||
|
||
if (Versioned::get_stage() == Versioned::DRAFT) { | ||
$this->Page()->__PageSectionCounter++; | ||
$this->Page()->write(); | ||
} | ||
} | ||
|
||
public function forTemplate() { | ||
return $this->Elements()->Count(); | ||
|
||
$actions = FieldList::create(); | ||
$fields = FieldList::create(); | ||
$form = Form::create(null, "Form", $fields, $actions); | ||
|
||
$config = GridFieldConfig::create() | ||
->addComponent(new GridFieldToolbarHeader()) | ||
->addComponent($dataColumns = new GridFieldDataColumns()) | ||
->addComponent(new GridFieldPageSectionsExtension($this->owner)); | ||
$dataColumns->setFieldCasting(["GridFieldPreview" => "HTMLText->RAW"]); | ||
|
||
$grid = GridField::create("Elements", "Elements", $this->Elements(), $config); | ||
$grid->setForm($form); | ||
$fields->add($grid); | ||
|
||
$form->setFields($fields); | ||
|
||
return $form->forTemplate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,49 @@ | ||
<?php | ||
|
||
namespace FLXLabs\PageSections; | ||
|
||
use SilverStripe\CMS\Model\SiteTree; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class PageSectionPageElementRel extends DataObject { | ||
|
||
private static $table_name = "FLXLabs_PageSections_PageSectionPageElementRel"; | ||
|
||
private static $db = [ | ||
"SortOrder" => "Int", | ||
]; | ||
|
||
private static $has_one = [ | ||
"PageSection" => "Page", | ||
"Element" => PageElement::class, | ||
]; | ||
|
||
|
||
public function onBeforeWrite() { | ||
parent::onBeforeWrite(); | ||
|
||
if (!$this->ID && !$this->SortOrder) { | ||
$this->SortOrder = 1337; | ||
} | ||
} | ||
} | ||
<?php | ||
|
||
namespace FLXLabs\PageSections; | ||
|
||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Versioned\Versioned; | ||
|
||
class PageSectionPageElementRel extends DataObject { | ||
|
||
private static $table_name = "FLXLabs_PageSections_PageSectionPageElementRel"; | ||
|
||
private static $db = array( | ||
"SortOrder" => "Int", | ||
); | ||
|
||
private static $has_one = array( | ||
"PageSection" => PageSection::class, | ||
"Element" => PageElement::class, | ||
); | ||
|
||
public function onBeforeWrite() { | ||
parent::onBeforeWrite(); | ||
|
||
if (!$this->ID) { | ||
if (!$this->SortOrder) { | ||
// Add new elements at the end (highest SortOrder) | ||
$this->SortOrder = ($this->PageSection()->Elements()->Count() + 1) * 2; | ||
} | ||
} | ||
} | ||
|
||
public function onAfterWrite() { | ||
parent::onAfterWrite(); | ||
|
||
if (!$this->__NewOrder && Versioned::get_stage() == Versioned::DRAFT) { | ||
$this->PageSection()->__Counter++; | ||
$this->PageSection()->write(); | ||
} | ||
} | ||
|
||
public function onAfterDelete() { | ||
parent::onAfterDelete(); | ||
|
||
if (Versioned::get_stage() == Versioned::DRAFT) { | ||
$this->PageSection()->__Counter++; | ||
$this->PageSection()->write(); | ||
} | ||
} | ||
} |
Oops, something went wrong.