-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
184 additions
and
51 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
Name: menu-manager#extensions | ||
Only: | ||
classexists: '\SilverStripe\Subsites\Model\Subsite' | ||
--- | ||
SilverStripe\Subsites\Model\Subsite: | ||
has_many: | ||
- MenuSets => 'Heyday\MenuManager\MenuSet' | ||
cascade_deletes: | ||
- MenuSets | ||
Heyday\MenuManager\MenuAdmin: | ||
extensions: | ||
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Heyday\MenuManager\Extensions; | ||
|
||
use Heyday\MenuManager\MenuSet; | ||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\Forms\HiddenField; | ||
use SilverStripe\ORM\DataExtension; | ||
use SilverStripe\Subsites\State\SubsiteState; | ||
|
||
if (!class_exists('\SilverStripe\Subsites\Model\Subsite') || !class_exists('\SilverStripe\Subsites\State\SubsiteState')) { | ||
return; | ||
} | ||
|
||
class MenuSubsiteExtension extends DataExtension | ||
{ | ||
private static $has_one = [ | ||
'Subsite' => 'SilverStripe\Subsites\Model\Subsite' | ||
]; | ||
|
||
public function updateCMSFields(FieldList $fields) | ||
{ | ||
$fields->replaceField('SubsiteID', new HiddenField('SubsiteID')); | ||
} | ||
|
||
public function onBeforeWrite() | ||
{ | ||
if (!$this->owner->SubsiteID) { | ||
$this->owner->SubsiteID = \SilverStripe\Subsites\State\SubsiteState::singleton()->getSubsiteId(); | ||
} | ||
} | ||
|
||
|
||
public function requireDefaultRecords() | ||
{ | ||
if ($this->owner->config()->get('create_menu_sets_per_subsite')) { | ||
$subsites = \SilverStripe\Subsites\Model\Subsite::get(); | ||
$names = $this->owner->getDefaultSetNames(); | ||
|
||
if ($names) { | ||
foreach ($subsites as $subsite) { | ||
$state = \SilverStripe\Subsites\State\SubsiteState::singleton(); | ||
|
||
$state->withState(function () use ($subsite, $names) { | ||
\SilverStripe\Subsites\State\SubsiteState::singleton()->setSubsiteId($subsite->ID); | ||
|
||
foreach ($names as $name) { | ||
$existingRecord = MenuSet::get() | ||
->filter([ | ||
'Name' => $name, | ||
'SubsiteID' => $subsite->ID | ||
]) | ||
->first(); | ||
|
||
if (!$existingRecord) { | ||
$set = MenuSet::create(); | ||
$set->Name = $name; | ||
$set->SubsiteID = $subsite->ID; | ||
$set->write(); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
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