FIX: Broken widgets (sidebar)

This commit is contained in:
Tony Air 2024-01-04 14:14:18 +02:00
parent 986f9435dd
commit be334571f1
2 changed files with 55 additions and 4 deletions

47
src/Tasks/BrokenSidebarTask.php Executable file
View File

@ -0,0 +1,47 @@
<?php
namespace A2nt\CMSNiceties\Tasks;
use Page;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Widgets\Model\WidgetArea;
class BrokenWidgetsTask extends BuildTask
{
protected $title = 'Broken Widgets Task';
protected $description = 'Broken widgets reset';
protected $enabled = true;
public function run($request)
{
$pages = Page::get();
$wIDs = array_keys($pages->map('ID', 'SideBarID')->toArray());
// delete orphaned widgets areas
$items = WidgetArea::get()->exclude('ID', $wIDs);
foreach ($items as $i) {
$i->delete();
}
// reset empty widget areas
foreach ($pages as $p) {
$w = $p->Sidebar();
if (!$w->ID) {
$p->setField('SideBarID', '0');
$p->write();
continue;
}
$widgets = $w->Widgets();
if (!$widgets->count()) {
$w->delete();
$p->setField('SideBarID', '0');
$p->write();
}
}
die('Done!');
}
}

View File

@ -1,10 +1,10 @@
<?php
namespace A2nt\CMSNiceties\Widgets;
use DNADesign\Elemental\Forms\ElementalAreaField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
use SilverStripe\Widgets\Model\Widget;
use SilverStripe\Widgets\Model\WidgetArea;
@ -33,9 +33,10 @@ class WidgetPageExtension extends \SilverStripe\Widgets\Extensions\WidgetPageExt
$available[get_class($type)] = $type->getCMSTitle();
}
$w = $this->owner->Sidebar();
$tab->push(WidgetAreaField::create(
'SideBar',
$this->owner->Sidebar(),
$w,
$available
));
}
@ -44,11 +45,14 @@ class WidgetPageExtension extends \SilverStripe\Widgets\Extensions\WidgetPageExt
{
parent::onBeforeWrite();
if (!$this->owner->getField('SideBarID')) {
$obj = $this->owner;
$w = $obj->SideBar();
if (!$w->ID || !$obj->getField('SideBarID')) {
$area = WidgetArea::create();
$area->write();
$this->owner->setField('SideBarID', $area->ID);
$obj->setField('SideBarID', $area->ID);
}
}
}