-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added automatic link creation from sitetree
- Loading branch information
Showing
4 changed files
with
77 additions
and
3 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,49 @@ | ||
<?php | ||
|
||
namespace gorriecoe\Menu\Extensions; | ||
|
||
use SilverStripe\Dev\Debug; | ||
use gorriecoe\Menu\Models\MenuSet; | ||
use gorriecoe\Menu\Models\MenuLink; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\ORM\DataExtension; | ||
|
||
/** | ||
* Provides the option to automatically create a menu link | ||
* after creating a page in the sitetree | ||
* | ||
* @package silverstripe | ||
* @subpackage silverstripe-menu | ||
*/ | ||
class SiteTreeAutoCreateExtension extends DataExtension | ||
{ | ||
/** | ||
* Event handler called after writing to the database. | ||
*/ | ||
public function onAfterWrite() | ||
{ | ||
parent::onAfterWrite(); | ||
$owner = $this->owner; | ||
|
||
$autoCreateList = $owner->config()->get('owns_menu') ? : []; | ||
|
||
foreach ($autoCreateList as $key => $slug) { | ||
if ($menuSet = MenuSet::get_by_slug($slug)) { | ||
$menuLink = DataObject::get_one(MenuLink::class, [ | ||
'MenuSetID' => $menuSet->ID, | ||
'SiteTreeID' => $owner->ID | ||
]); | ||
if ($menuLink) { | ||
$menuLink->setField('Title', $owner->Title); | ||
} else { | ||
$menuLink = MenuLink::create([ | ||
'Type' => 'SiteTree', | ||
'MenuSetID' => $menuSet->ID, | ||
'SiteTreeID' => $owner->ID | ||
]); | ||
}; | ||
$menuLink->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