Homepage
Main navigation
Main content
Additional information
October Tricks
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
October CMS resources and help articles
Simple and to the point. Optimized by the community.
×
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Edit trick
Changes will be published after manual review
Title
Give your trick a describing title. Do
not
start with «How to...».
Your trick
Keep it short and concise! Markdown is supported.
To add all entries of a specific model to a `RainLab.Sitemap` or `RainLab.Pages` menu do the following: In your `Plugin.php` add the following three event listeners. Replace the `your-items` key with a more descriptive string. ```php public function boot() { \Event::listen('pages.menuitem.listTypes', function () { return [ 'your-items' => 'Name of the items', ]; }); \Event::listen('pages.menuitem.getTypeInfo', function ($type) { if ($type === 'your-items') { $theme = \Cms\Classes\Theme::getActiveTheme(); $pages = \Cms\Classes\Page::listInTheme($theme, true); return [ 'dynamicItems' => true, 'cmsPages' => $pages, ]; } }); \Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) { if ($type === 'your-items') { return YourModel::resolveMenuItem($item, $url, $theme); } }); } ``` Then add the `resolveMenuItem` method to your Model. The method has to return an array of all the items you want to add. The example code below fetches all models from the database and generates the link using a specific cms page. ```php public static function resolveMenuItem($item, $url, $theme) { $pageName = $item->cmsPage; $cmsPage = \Cms\Classes\Page::loadCached($theme, $pageName); $items = self ::orderBy('created_at', 'DESC') ->get() ->map(function (self $item) use ($cmsPage, $url, $pageName) { $pageUrl = $cmsPage->url($pageName, ['slug' => $item->slug]); return [ 'title' => $item->name, 'url' => $pageUrl, 'mtime' => $item->updated_at, 'isActive' => $pageUrl === $url, ]; }) ->toArray(); return [ 'items' => $items, ]; } ``` You will now find your new menu items type in the respective editors of `RainLab.Sitemap` and `RainLab.Pages`.
References
Add additional online resources to your trick
×
Name
URL
+ Add reference
Topics
If your trick fits more than one topic select multiple. Select at least one.
Backend
Plugin Development
CMS
Twig
Themes
Deployment
Tags
You can use existing tags or create new ones. Add at least one.
Submit for review
Cancel
We use cookies to measure the performance of this website. Do you want to accept these cookies?
Accept
Decline