Adding a custom flex directory type from a plugin #3118
Answered
by
TheDancingCode
TheDancingCode
asked this question in
Development
-
I'd like to add a custom title: Products
description: Simple product directory.
type: flex-objects
form:
fields:
name:
type: text
label: Name
price:
type: text
label: Price
config:
data:
storage: user-data://flex-objects/products.json
admin:
router:
path: '/products'
list:
fields:
name:
link: edit
price:
menu:
list:
route: '/products'
title: Products
icon: fa-address-card-o
authorize: ['admin.users', 'admin.accounts', 'admin.super']
However, I can't seem to add it from a plugin. I know I have to use the <?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use Grav\Framework\Flex\FlexDirectory;
class ShopPlugin extends Plugin
{
public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => [
['onPluginsInitialized', 0],
]
];
}
public function onPluginsInitialized(): void
{
$this->enable([
'onFlexInit' => ['onFlexInit', 0]
]);
}
public function onFlexInit($event){
$directory = new FlexDirectory('products', 'plugins://shop/blueprints/flex-objects/products.yaml', ['enabled'=>true]);
$flex = $event['flex'];
$flex->addDirectory($directory);
}
} What should I do to make it work? |
Beta Was this translation helpful? Give feedback.
Answered by
TheDancingCode
Jan 5, 2021
Replies: 1 comment
-
The solution is to subscribe to the class ShopPlugin extends Plugin
{
public static function getSubscribedEvents(): array
{
return [
'onFlexInit' => ['onFlexInit', 0]
];
}
public function onFlexInit($event){
$directory = new FlexDirectory('products', 'plugins://shop/blueprints/flex-objects/products.yaml', ['enabled'=>true]);
$flex = $event['flex'];
$flex->addDirectory($directory);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
TheDancingCode
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The solution is to subscribe to the
onFlexInit
event earlier, undergetSubscribedEvents
: