Skip to content

Instantly share code, notes, and snippets.

@adrexia
Forked from kinglozzer/BaseElementExtension.php
Created March 5, 2020 03:29
Show Gist options
  • Save adrexia/a68ce071506130674f47535f4c7d7545 to your computer and use it in GitHub Desktop.
Save adrexia/a68ce071506130674f47535f4c7d7545 to your computer and use it in GitHub Desktop.
<?php
namespace App\Extensions;
class BaseElementExtension extends Extension
{
/**
* Flag to indiciate if this element is a new element. Used because isChanged('ID') seems to be unreliable
*
* @var bool
*/
protected $isNewElement = false;
public function onBeforeWrite(): void
{
if (!$this->owner->ID) {
$this->setIsNewElement(true);
}
}
/**
* @param $bool
*/
public function setIsNewElement($bool): void
{
$this->isNewElement = true;
}
/**
* @return bool
*/
public function getIsNewElement(): bool
{
return $this->isNewElement;
}
}
---
Name: app-elements
---
DNADesign\Elemental\Models\BaseElement:
extensions:
- App\Extensions\BaseElementExtension
SilverStripe\Core\Injector\Injector:
DNADesign\Elemental\Services\ReorderElements:
class: App\Services\ReorderElements
<?php
namespace App\Services;
use DNADesign\Elemental\Services\ReorderElements as ElementalReorderElements;
use SilverStripe\ORM\DataObject;
class ReorderElements extends ElementalReorderElements
{
public function reorder($elementToBeAfterID = 0)
{
$element = $this->getElement();
// We want to prevent the default behaviour of re-ordering new elements
// to be at the start of the list
if ($element->getIsNewElement() && $elementToBeAfterID == 0) {
$element->write();
return null;
}
return parent::reorder($elementToBeAfterID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment