我也想要想系統設定那樣的支援二級的側邊欄。我感覺是目前最好的處理方案,要麼就等官方出面支援了。
細節
外掛說明地址:Jcc.Sidenav
截圖
實現效果
使用
安裝
git clone https://github.com/octobercms-plugin/oc-plugin-sidenav.git plugins/jcc/sidenav
在 Plugin.php 中的操作
跟據自己的習慣,修改了使用方法
我想直接用 [RainLab.Builder] 生成的 plugin.yaml 檔案中的配置實現側邊欄
這裡寫一個空的 registerNavigation() 方法,防止他按原來的方式多顯示一個
不知道為什麼我這裡如果直接用 PluginBase 中的 getConfigurationFromYaml() 方法會報錯,就把這個方法那出來,並且要 use Symfony\Component\Yaml\Yaml; 直接 use Yaml; 也會報錯。
<?php namespace Plus\Wechat;
use System\Classes\PluginBase;
use BackendMenu;
use Backend;
use ReflectionClass;
use SystemException;
use Symfony\Component\Yaml\Yaml;
class Plugin extends PluginBase
{
public function registerNavigation()
{
}
public function register()
{
$configuration = $this->getConfigurationFromYaml();
if (array_key_exists('navigation', $configuration)) {
$navigation = $configuration['navigation'];
if (is_array($navigation)) {
array_walk_recursive($navigation, function (&$item, $key) {
if ($key === 'url') {
$item = Backend::url($item);
}
});
}
foreach ($navigation as $nak=>$nav){
BackendMenu::registerCallback(function ($manager) use($nak,$nav){
$manager->registerMenuItems('October.System', [$nak=>$nav]);
});
BackendMenu::registerContextSidenavPartial(
'October.System',
$nak,
'~/plugins/jcc/sidenav/partials/_system_sidebar.htm'
);
}
}
}
public function registerSideNavSettings()
{
$sideNav=[];
$configuration = $this->getConfigurationFromYaml();
if (array_key_exists('navigation', $configuration)) {
$navigation = $configuration['navigation'];
if (is_array($navigation)) {
array_walk_recursive($navigation, function (&$item, $key) {
if ($key === 'url') {
$item = Backend::url($item);
}
});
}
foreach ($navigation as $nak=>$nav){
$sideMenu=$nav['sideMenu']??[];
foreach ($sideMenu as $sidek=>&$sidrv){
$sidrv['context']=$sidrv['context']??$nak;
$sidrv['description']=$sidrv['description']??'';
}
$sideNav = array_merge($sideNav, $sideMenu);
}
}
return $sideNav;
}
protected function getConfigurationFromYaml($exceptionMessage = null)
{
if ($this->loadedYamlConfiguration !== false) {
return $this->loadedYamlConfiguration;
}
$reflection = new ReflectionClass(get_class($this));
$yamlFilePath = dirname($reflection->getFileName()).'/plugin.yaml';
if (!file_exists($yamlFilePath)) {
if ($exceptionMessage) {
throw new SystemException($exceptionMessage);
}
$this->loadedYamlConfiguration = [];
}
else {
$this->loadedYamlConfiguration = Yaml::parse(file_get_contents($yamlFilePath));
if (!is_array($this->loadedYamlConfiguration)) {
throw new SystemException(sprintf('Invalid format of the plugin configuration file: %s. The file should define an array.', $yamlFilePath));
}
}
return $this->loadedYamlConfiguration;
}
}
在 plugin.yaml 檔案中的操作
只需在二級選單多新增一個 category 引數來分組
navigation:
main-menu-item-wechat:
label: 微信管理
url: plus/wechat/materialtext
icon: icon-wechat
sideMenu:
side-menu-item-material_text:
label: 文字素材
url: plus/wechat/materialtext
icon: icon-file-text
category: 素材管理
side-menu-item-material_news:
label: 圖文素材
url: /
icon: icon-newspaper-o
category: 素材管理
在控制器中的操作
public function __construct()
{
parent::__construct();
\BackendMenu::setContext('October.System', 'main-menu-item-wechat');
\Jcc\Sidenav\Classes\SideNavManager::setContext('Plus.Wechat', 'side-menu-item-material_text');
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結