-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlugin.php
179 lines (167 loc) · 6.27 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Winter\Blocks;
use Backend\Classes\WidgetManager;
use Cms\Classes\AutoDatasource;
use Cms\Classes\Theme;
use Event;
use System\Classes\PluginBase;
use Winter\Blocks\Classes\BlockManager;
use Winter\Blocks\Classes\BlocksDatasource;
use Winter\Blocks\Classes\Block as BlockModel;
use Winter\Blocks\FormWidgets\Block;
/**
* Blocks Plugin Information File
*
* @TODO:
* - Review https://octobercms.com/plugin/reazzon-editor
*
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*/
public function pluginDetails(): array
{
return [
'name' => 'winter.blocks::lang.plugin.name',
'description' => 'winter.blocks::lang.plugin.description',
'author' => 'Winter CMS',
'icon' => 'icon-cubes',
];
}
/**
* Registers the custom Blocks provided by this plugin
*/
public function registerBlocks(): array
{
return [
'button' => '$/winter/blocks/blocks/button.block',
'button_group' => '$/winter/blocks/blocks/button_group.block',
'cards' => '$/winter/blocks/blocks/cards.block',
'code' => '$/winter/blocks/blocks/code.block',
'columns_two' => '$/winter/blocks/blocks/columns_two.block',
'divider' => '$/winter/blocks/blocks/divider.block',
'image' => '$/winter/blocks/blocks/image.block',
'plaintext' => '$/winter/blocks/blocks/plaintext.block',
'richtext' => '$/winter/blocks/blocks/richtext.block',
'title' => '$/winter/blocks/blocks/title.block',
'video' => '$/winter/blocks/blocks/video.block',
'vimeo' => '$/winter/blocks/blocks/vimeo.block',
'youtube' => '$/winter/blocks/blocks/youtube.block',
];
}
/**
* Registers the custom FormWidgets provided by this plugin
*/
public function registerFormWidgets(): array
{
return [
\Winter\Blocks\FormWidgets\Blocks::class => 'blocks'
];
}
/**
* Registers the custom twig markups provided by this plugin
*/
public function registerMarkupTags()
{
return [
'functions' => [
'renderBlock' => [
function (array $context, string|array $block, array $data = []) {
return BlockModel::render(
$block,
$data,
$context['this']['controller'] ?? null
);
},
'options' => ['needs_context' => true]
],
'renderBlocks' => [
function (array $context, array $blocks) {
return BlockModel::renderAll(
$blocks,
$context['this']['controller'] ?? null
);
},
'options' => ['needs_context' => true]
],
],
];
}
/**
* Boot method, called right before the request route.
*/
public function boot(): void
{
$this->extendThemeDatasource();
$this->extendControlLibraryBlocks();
}
/**
* Extend the theme's datasource to include the BlocksDatasource for loading blocks from
*/
protected function extendThemeDatasource(): void
{
// Register the block manager instance
BlockManager::instance();
Event::listen('cms.theme.registerHalcyonDatasource', function (Theme $theme, $resolver) {
$source = $theme->getDatasource();
if ($source instanceof AutoDatasource) {
/* @var AutoDatasource $source */
$source->appendDatasource('blocks', new BlocksDatasource());
return;
} else {
$resolver->addDatasource($theme->getDirName(), new AutoDatasource([
'theme' => $source,
'blocks' => new BlocksDatasource(),
], 'blocks-autodatasource'));
}
});
}
/**
* Extend the ControlLibrary provided by Winter.Builder to register blocks as Form Controls
*/
protected function extendControlLibraryBlocks(): void
{
// Register blocks as custom controls
Event::listen('pages.builder.registerControls', function (\Winter\Builder\Classes\ControlLibrary $controlLibrary) {
foreach (BlockManager::instance()->getConfigs('forms') as $key => $config) {
// Map custom fields into standard properties, while ignoring irrelevant properties
$properties = $controlLibrary->getStandardProperties([
'label', 'required', 'comment', 'placeholder', 'default', 'defaultFrom', 'stretch'
], array_combine(
array_map(
fn($field) => sprintf('data[%s]', $field),
array_keys($config['fields'] ?? [])
),
array_values(
array_map(
fn ($field) => array_merge($field, [
'title' => $field['label'] ?? '',
'tab' => 'Field Options'
]),
$config['fields'] ?? []
)
)
));
// Sort custom fields to the top
uksort($properties, fn ($a, $b) => str_contains($key, 'data[') ? 1 : $a <=> $b);
$controlLibrary->registerControl(
Block::TYPE_PREFIX . $key,
$config['name'],
$config['description'],
Block::GROUP_BLOCKS,
$config['icon'],
$properties,
null
);
}
}, PHP_INT_MIN);
// Register a Winter\Blocks\FormWidgets\Block FormWidget under each block's key
WidgetManager::instance()->registerFormWidgets(function ($manager) {
foreach (BlockManager::instance()->getConfigs() as $key => $config) {
$manager->registerFormWidget(Block::class, Block::TYPE_PREFIX . $key);
}
});
}
}