静态页面插件 - 列出所有页面 url 作为自定义页面字段
Static page plugin - List with all pages url as a custom page field
我正在使用静态页面插件。我需要一种方法从自定义字段中获取 octobercms 的所有页面。我不关心这将如何完成(官方页面 url 选择器 - 下拉列表 ...),我只想对用户友好,以便可以从客户端使用。
但它只适用于转发器。如果我将它与另一个字段 {variable name="page" label="Page" type="dropdown"}{/variable}
一起使用,我会收到错误消息:
get_class() expects parameter 1 to be object, array given
哇!这个问题真的让我绞尽脑汁,
Thanks for the Question, I really enjoyed it to solve.
好像我们在中继器之外使用普通变量 October 字段解析会将其视为名称 viewBag[page]
并且它不适用于 fieldParser
和 fieldOptions
getter 方法。 (它的技术性很小,所以我想跳过这个并跳到真正的解决方案)
any how at last I found little hack/kind of extension/functionality (extensive nature of October Cms) and use it
您需要任何插件,以便可以在 boot
方法中编写扩展代码。
public function boot() {
$alias = \Illuminate\Foundation\AliasLoader::getInstance()
->alias('MyPluginOptionAlias','October\Demo\plugin');
}
您可以在此处更正 alias-name
和 namespace
我们正在使用此 alias
(MyPluginOptionAlias)[= 使我们的插件对象全局可用33=]
并且在 plugin.php
中我们添加了两个 static
方法以便我们稍后可以访问它们,这只是 logic
获取 page-list
和 return它作为一个数组。
public static function getPageOptions() {
// different lists // or even you can merge them
$result = self::getTypeInfo('cms-page');
// $result = self::getTypeInfo('static-page');
// $result = self::getTypeInfo('blog-post');
return $result['references'];
}
public static function getTypeInfo($type)
{
$result = [];
$apiResult = \Event::fire('pages.menuitem.getTypeInfo', [$type]);
if (is_array($apiResult)) {
foreach ($apiResult as $typeInfo) {
if (!is_array($typeInfo)) {
continue;
}
foreach ($typeInfo as $name => $value) {
if ($name == 'cmsPages') {
$cmsPages = [];
foreach ($value as $page) {
$baseName = $page->getBaseFileName();
$pos = strrpos($baseName, '/');
$dir = $pos !== false ? substr($baseName, 0, $pos).' / ' : null;
$cmsPages[$baseName] = strlen($page->title)
? $dir.$page->title
: $baseName;
}
$value = $cmsPages;
}
$result[$name] = $value;
}
}
}
return $result;
}
Now the main thing
{variable name="page" label="Page" type="dropdown" options="MyPluginOptionAlias|getPageOptions" tab="link"}{/variable}
你这样定义选项 exactly
MyPluginOptionAlias|getPageOptions
这里的逻辑是,现在它将从我们的 MyPluginOptionAlias
实例的 method: getPageOptions
中获取选项列表
what ever array
you return from it will be listed as options for this drop-down.
如果有任何问题请评论,我们会改正。
我正在使用静态页面插件。我需要一种方法从自定义字段中获取 octobercms 的所有页面。我不关心这将如何完成(官方页面 url 选择器 - 下拉列表 ...),我只想对用户友好,以便可以从客户端使用。
{variable name="page" label="Page" type="dropdown"}{/variable}
一起使用,我会收到错误消息:
get_class() expects parameter 1 to be object, array given
哇!这个问题真的让我绞尽脑汁,
Thanks for the Question, I really enjoyed it to solve.
好像我们在中继器之外使用普通变量 October 字段解析会将其视为名称 viewBag[page]
并且它不适用于 fieldParser
和 fieldOptions
getter 方法。 (它的技术性很小,所以我想跳过这个并跳到真正的解决方案)
any how at last I found little hack/kind of extension/functionality (extensive nature of October Cms) and use it
您需要任何插件,以便可以在 boot
方法中编写扩展代码。
public function boot() {
$alias = \Illuminate\Foundation\AliasLoader::getInstance()
->alias('MyPluginOptionAlias','October\Demo\plugin');
}
您可以在此处更正 alias-name
和 namespace
我们正在使用此 alias
(MyPluginOptionAlias)[= 使我们的插件对象全局可用33=]
并且在 plugin.php
中我们添加了两个 static
方法以便我们稍后可以访问它们,这只是 logic
获取 page-list
和 return它作为一个数组。
public static function getPageOptions() {
// different lists // or even you can merge them
$result = self::getTypeInfo('cms-page');
// $result = self::getTypeInfo('static-page');
// $result = self::getTypeInfo('blog-post');
return $result['references'];
}
public static function getTypeInfo($type)
{
$result = [];
$apiResult = \Event::fire('pages.menuitem.getTypeInfo', [$type]);
if (is_array($apiResult)) {
foreach ($apiResult as $typeInfo) {
if (!is_array($typeInfo)) {
continue;
}
foreach ($typeInfo as $name => $value) {
if ($name == 'cmsPages') {
$cmsPages = [];
foreach ($value as $page) {
$baseName = $page->getBaseFileName();
$pos = strrpos($baseName, '/');
$dir = $pos !== false ? substr($baseName, 0, $pos).' / ' : null;
$cmsPages[$baseName] = strlen($page->title)
? $dir.$page->title
: $baseName;
}
$value = $cmsPages;
}
$result[$name] = $value;
}
}
}
return $result;
}
Now the main thing
{variable name="page" label="Page" type="dropdown" options="MyPluginOptionAlias|getPageOptions" tab="link"}{/variable}
你这样定义选项 exactly
MyPluginOptionAlias|getPageOptions
这里的逻辑是,现在它将从我们的 MyPluginOptionAlias
实例的 method: getPageOptions
what ever
array
you return from it will be listed as options for this drop-down.
如果有任何问题请评论,我们会改正。