mirror of
https://github.com/a2nt/cms-niceties.git
synced 2024-10-22 11:05:46 +02:00
FIX: Broken widgets (sidebar)
This commit is contained in:
parent
986f9435dd
commit
be334571f1
47
src/Tasks/BrokenSidebarTask.php
Executable file
47
src/Tasks/BrokenSidebarTask.php
Executable 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!');
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace A2nt\CMSNiceties\Widgets;
|
namespace A2nt\CMSNiceties\Widgets;
|
||||||
|
|
||||||
use DNADesign\Elemental\Forms\ElementalAreaField;
|
use DNADesign\Elemental\Forms\ElementalAreaField;
|
||||||
use SilverStripe\Forms\FieldList;
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\Forms\LiteralField;
|
||||||
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
|
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
|
||||||
use SilverStripe\Widgets\Model\Widget;
|
use SilverStripe\Widgets\Model\Widget;
|
||||||
use SilverStripe\Widgets\Model\WidgetArea;
|
use SilverStripe\Widgets\Model\WidgetArea;
|
||||||
@ -33,9 +33,10 @@ class WidgetPageExtension extends \SilverStripe\Widgets\Extensions\WidgetPageExt
|
|||||||
$available[get_class($type)] = $type->getCMSTitle();
|
$available[get_class($type)] = $type->getCMSTitle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$w = $this->owner->Sidebar();
|
||||||
$tab->push(WidgetAreaField::create(
|
$tab->push(WidgetAreaField::create(
|
||||||
'SideBar',
|
'SideBar',
|
||||||
$this->owner->Sidebar(),
|
$w,
|
||||||
$available
|
$available
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -44,11 +45,14 @@ class WidgetPageExtension extends \SilverStripe\Widgets\Extensions\WidgetPageExt
|
|||||||
{
|
{
|
||||||
parent::onBeforeWrite();
|
parent::onBeforeWrite();
|
||||||
|
|
||||||
if (!$this->owner->getField('SideBarID')) {
|
$obj = $this->owner;
|
||||||
|
$w = $obj->SideBar();
|
||||||
|
|
||||||
|
if (!$w->ID || !$obj->getField('SideBarID')) {
|
||||||
$area = WidgetArea::create();
|
$area = WidgetArea::create();
|
||||||
$area->write();
|
$area->write();
|
||||||
|
|
||||||
$this->owner->setField('SideBarID', $area->ID);
|
$obj->setField('SideBarID', $area->ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user