Last active
July 20, 2023 20:50
-
-
Save kinglozzer/a2c27f2a0c3563d8a72f8dfb982fc7fa to your computer and use it in GitHub Desktop.
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
<?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; | |
} | |
} |
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
--- | |
Name: app-elements | |
--- | |
DNADesign\Elemental\Models\BaseElement: | |
extensions: | |
- App\Extensions\BaseElementExtension | |
SilverStripe\Core\Injector\Injector: | |
DNADesign\Elemental\Services\ReorderElements: | |
class: App\Services\ReorderElements |
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
<?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