RainLab\Blog\Models\Category::resolveMenuItem PHP Method

resolveMenuItem() public static method

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.
public static resolveMenuItem ( RainLab\Pages\Classes\MenuItem $item, string $url, Cms\Classes\Theme $theme ) : mixed
$item RainLab\Pages\Classes\MenuItem Specifies the menu item.
$url string 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.
$theme Cms\Classes\Theme Specifies the current theme.
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 == 'blog-category') {
            if (!$item->reference || !$item->cmsPage) {
                return;
            }
            $category = self::find($item->reference);
            if (!$category) {
                return;
            }
            $pageUrl = self::getCategoryPageUrl($item->cmsPage, $category, $theme);
            if (!$pageUrl) {
                return;
            }
            $pageUrl = URL::to($pageUrl);
            $result = [];
            $result['url'] = $pageUrl;
            $result['isActive'] = $pageUrl == $url;
            $result['mtime'] = $category->updated_at;
            if ($item->nesting) {
                $categories = $category->getNested();
                $iterator = function ($categories) use(&$iterator, &$item, &$theme, $url) {
                    $branch = [];
                    foreach ($categories as $category) {
                        $branchItem = [];
                        $branchItem['url'] = self::getCategoryPageUrl($item->cmsPage, $category, $theme);
                        $branchItem['isActive'] = $branchItem['url'] == $url;
                        $branchItem['title'] = $category->name;
                        $branchItem['mtime'] = $category->updated_at;
                        if ($category->children) {
                            $branchItem['items'] = $iterator($category->children);
                        }
                        $branch[] = $branchItem;
                    }
                    return $branch;
                };
                $result['items'] = $iterator($categories);
            }
        } elseif ($item->type == 'all-blog-categories') {
            $result = ['items' => []];
            $categories = self::orderBy('name')->get();
            foreach ($categories as $category) {
                $categoryItem = ['title' => $category->name, 'url' => self::getCategoryPageUrl($item->cmsPage, $category, $theme), 'mtime' => $category->updated_at];
                $categoryItem['isActive'] = $categoryItem['url'] == $url;
                $result['items'][] = $categoryItem;
            }
        }
        return $result;
    }

Usage Example

コード例 #1
0
ファイル: Plugin.php プロジェクト: janusnic/OctoberCMS
 public function boot()
 {
     Event::listen('pages.menuitem.listTypes', function () {
         return ['blog-category' => 'Blog category', 'all-blog-categories' => 'All blog categories'];
     });
     Event::listen('pages.menuitem.getTypeInfo', function ($type) {
         if ($type == 'blog-category' || $type == 'all-blog-categories') {
             return Category::getMenuTypeInfo($type);
         }
     });
     Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) {
         if ($type == 'blog-category' || $type == 'all-blog-categories') {
             return Category::resolveMenuItem($item, $url, $theme);
         }
     });
 }
All Usage Examples Of RainLab\Blog\Models\Category::resolveMenuItem