Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Last active July 20, 2023 20:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kinglozzer/a2c27f2a0c3563d8a72f8dfb982fc7fa to your computer and use it in GitHub Desktop.
Save kinglozzer/a2c27f2a0c3563d8a72f8dfb982fc7fa 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