The CMS pages are now supported by the Static Pages menus

This commit is contained in:
alekseybobkov 2014-10-02 15:55:55 -07:00
parent 762088463c
commit ef9a147d31
2 changed files with 92 additions and 0 deletions

View File

@ -1,6 +1,7 @@
<?php namespace Cms;
use Lang;
use Event;
use Backend;
use BackendMenu;
use BackendAuth;
@ -8,6 +9,7 @@ use Backend\Classes\WidgetManager;
use October\Rain\Support\ModuleServiceProvider;
use System\Classes\SettingsManager;
use Cms\Classes\ComponentManager;
use Cms\Classes\Page as CmsPage;
class ServiceProvider extends ModuleServiceProvider
{
@ -134,6 +136,22 @@ class ServiceProvider extends ModuleServiceProvider
public function boot()
{
parent::boot('cms');
Event::listen('pages.menuitem.listTypes', function() {
return [
'cms-page'=>'CMS Page '
];
});
Event::listen('pages.menuitem.getTypeInfo', function($type) {
if ($type == 'cms-page')
return CmsPage::getMenuTypeInfo($type);
});
Event::listen('pages.menuitem.resolveItem', function($type, $item, $url, $theme) {
if ($type == 'cms-page')
return CmsPage::resolveMenuItem($item, $url, $theme);
});
}
}

View File

@ -90,6 +90,7 @@ class Page extends CmsCompoundObject
/**
* Helper that makes a URL for a page in the active theme.
* @param mixed $page Specifies the Cms Page file name.
* @return string
*/
public static function url($page, $params = [], $absolute = true)
@ -105,4 +106,77 @@ class Page extends CmsCompoundObject
return $controller->pageUrl($page, $params, true, $absolute);
}
/**
* Handler for the pages.menuitem.getTypeInfo event.
* Returns a menu item type information. The type information is returned as array
* with the following elements:
* - references - a list of the item type reference options. The options are returned in the
* ["key"] => "title" format for options that don't have sub-options, and in the format
* ["key"] => ["title"=>"Option title", "items"=>[...]] for options that have sub-options. Optional,
* required only if the menu item type requires references.
* - nesting - Boolean value indicating whether the item type supports nested items. Optional,
* false if omitted.
* - dynamicItems - Boolean value indicating whether the item type could generate new menu items.
* Optional, false if omitted.
* - cmsPages - a list of CMS pages (objects of the Cms\Classes\Page class), if the item type requires a CMS page reference to
* resolve the item URL.
* @param string $type Specifies the menu item type
* @return array Returns an array
*/
public static function getMenuTypeInfo($type)
{
$result = [];
if ($type == 'cms-page') {
$theme = Theme::getActiveTheme();
$pages = self::listInTheme($theme, true);
foreach ($pages as $page)
$references[$page->getBaseFileName()] = $page->title;
$result = [
'references' => $references,
'nesting' => false,
'dynamicItems' => false
];
}
return $result;
}
/**
* Handler for the pages.menuitem.resolveItem event.
* Returns information about a menu item. The result is an array
* with the following keys:
* - url - the menu item URL. Not required for menu item types that return all available records.
* The URL should be returned relative to the website root and include the subdirectory, if any.
* Use the URL::to() helper to generate the URLs.
* - isActive - determines whether the menu item is active. Not required for menu item types that
* return all available records.
* - items - an array of arrays with the same keys (url, isActive, items) + the title key.
* The items array should be added only if the $item's $nesting property value is TRUE.
* @param \RainLab\Pages\Classes\MenuItem $item Specifies the menu item.
* @param \Cms\Classes\Theme $theme Specifies the current theme.
* @param string $url Specifies the current page URL, normalized, in lower case
* The URL is specified relative to the website root, it includes the subdirectory name, if any.
* @return mixed Returns an array. Returns null if the item cannot be resolved.
*/
public static function resolveMenuItem($item, $url, $theme)
{
$result = null;
if ($item->type == 'cms-page') {
if (!$item->reference)
return;
$pageUrl = self::url($item->reference);
$result = [];
$result['url'] = $pageUrl;
$result['isActive'] = $pageUrl == $url;
}
return $result;
}
}