Skip to content

Commit

Permalink
Added automatic link creation from sitetree
Browse files Browse the repository at this point in the history
  • Loading branch information
gorriecoe committed Apr 8, 2020
1 parent e31da44 commit 853b3c9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,20 @@ gorriecoe\Menu\Models\MenuSet:

## Adding links to menus

Once you have created your menus you can add links.
Once you have created your menus you can add links in the admin area. The fields are inherited from [silverstripe link](https://github.com/gorriecoe/silverstripe-link).

## Automatically add links from sitetree to specific menus

If you need to automatically add links to a menu after the creation of a page, you can do so by adding the following extension to page and defining `owns_menu`.

```
Page:
extensions:
- gorriecoe\Menu\Extensions\SiteTreeAutoCreateExtension
owns_menu:
- main
- footer
```

## Usage in template

Expand Down
49 changes: 49 additions & 0 deletions src/extensions/SiteTreeAutoCreateExtension.php
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();
}
}
}
}
12 changes: 12 additions & 0 deletions src/models/MenuSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ public function CMSEditLink()
);
}

/**
* Return the first menuset matching the given slug.
*
* @return gorriecoe\Menu\Models\MenuSet|Null
*/
public static function get_by_slug($slug)
{
if ($slug) {
return self::get()->find('Slug', $slug);
}
}

/**
* Relationship accessor for Graphql
* @return ManyManyList MenuLink
Expand Down
4 changes: 2 additions & 2 deletions src/view/MenuManagerTemplateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static function MenuSet($slug)
if (!$slug) {
return;
}
if ($MenuSet = MenuSet::get()->find('Slug', $slug)) {
return $MenuSet->Links();
if ($menuSet = MenuSet::get_by_slug($slug)) {
return $menuSet->Links();
}
}
}

0 comments on commit 853b3c9

Please sign in to comment.