-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathSettingsAsset.php
314 lines (281 loc) · 10.7 KB
/
SettingsAsset.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
namespace benf\neo\assets;
use benf\neo\elements\Block;
use benf\neo\events\SetConditionElementTypesEvent;
use benf\neo\Field;
use benf\neo\models\BlockType;
use benf\neo\models\BlockTypeGroup;
use benf\neo\Plugin as Neo;
use Craft;
use craft\commerce\elements\Order;
use craft\commerce\elements\Product;
use craft\commerce\elements\Subscription;
use craft\commerce\elements\Variant;
use craft\elements\Address;
use craft\elements\Asset;
use craft\elements\Category;
use craft\elements\Entry;
use craft\elements\Tag;
use craft\elements\User;
use craft\helpers\Cp;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\models\FieldLayout;
use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;
use yii\base\Event;
/**
* Class SettingsAsset
*
* @package benf\neo\assets
* @author Spicy Web <[email protected]>
* @author Benjamin Fleming
* @since 3.0.0
*/
class SettingsAsset extends AssetBundle
{
/**
* @event SetConditionElementTypesEvent The event that's triggered when setting the element types for setting
* conditions on when block types can be used
*
* ```php
* use benf\neo\assets\SettingsAsset;
* use benf\neo\events\SetConditionElementTypesEvent;
* use yii\base\Event;
*
* Event::on(
* SettingsAsset::class,
* SettingsAsset::EVENT_SET_CONDITION_ELEMENT_TYPES,
* function (SetConditionElementTypesEvent $event) {
* $event->elementTypes[] = \some\added\ElementType::class;
* }
* );
* ```
*
* @since 3.2.0
*/
public const EVENT_SET_CONDITION_ELEMENT_TYPES = 'setConditionElementTypes';
/**
* @var string[] Supported element types for setting conditions on when block types can be used
*/
private static array $_conditionElementTypes = [];
/**
* @inheritdoc
*/
public function init(): void
{
$this->sourcePath = '@benf/neo/resources';
$this->depends = [
CpAsset::class,
];
$this->css = ['neo-configurator.css'];
$this->js = [
'neo-configurator.js',
'neo-converter.js',
];
parent::init();
}
/**
* @inheritdoc
*/
public function registerAssetFiles($view): void
{
$view->registerTranslations('neo', [
'Actions',
'Copy',
'Paste',
'Clone',
'Delete',
'Reorder',
'Name',
'What this block type will be called in the CP.',
'Handle',
'How you’ll refer to this block type in the templates.',
'Description',
'Enabled',
'Whether this block type is allowed to be used.',
'Max Blocks',
'The maximum number of blocks of this type the field is allowed to have.',
'All',
'Child Blocks',
'Which block types do you want to allow as children?',
'Max Child Blocks',
'The maximum number of child blocks this block type is allowed to have.',
'Top Level',
'Will this block type be allowed at the top level?',
'Delete block type',
'This can be left blank if you just want an unlabeled separator.',
'Show',
'Hide',
'Use global setting (Show)',
'Use global setting (Hide)',
'Always Show Dropdown?',
'Whether to show the dropdown for this group if it only has one available block type.',
'Delete group',
'Couldn’t copy block type.',
'Couldn’t create new block type.',
]);
parent::registerAssetFiles($view);
}
/**
* Sets up the field layout designer for a given Neo field.
*
* @param Field $field The Neo field.
* @return string
*/
public static function createSettingsJs(Field $field): string
{
$event = new SetConditionElementTypesEvent([
'elementTypes' => self::_getSupportedConditionElementTypes(),
]);
Event::trigger(self::class, self::EVENT_SET_CONDITION_ELEMENT_TYPES, $event);
self::$_conditionElementTypes = $event->elementTypes;
$blockTypes = $field->getBlockTypes();
$blockTypeGroups = $field->getGroups();
[$blockTypeSettingsHtml, $blockTypeSettingsJs] = self::_renderBlockTypeSettings();
$fieldLayoutHtml = Neo::$plugin->blockTypes->renderFieldLayoutDesigner(new FieldLayout(['type' => Block::class]));
$jsSettings = [
'namespace' => Craft::$app->getView()->getNamespace(),
'blockTypes' => self::_getBlockTypesJsSettings($blockTypes),
'groups' => self::_getBlockTypeGroupsJsSettings($blockTypeGroups),
'blockTypeSettingsHtml' => $blockTypeSettingsHtml,
'blockTypeSettingsJs' => $blockTypeSettingsJs,
'fieldLayoutHtml' => $fieldLayoutHtml,
'defaultAlwaysShowGroupDropdowns' => Neo::$plugin->settings->defaultAlwaysShowGroupDropdowns,
];
$encodedJsSettings = Json::encode($jsSettings, JSON_UNESCAPED_UNICODE);
return "Neo.createConfigurator($encodedJsSettings)";
}
/**
* Returns the raw data from the given block types, in the format used by the settings generator JavaScript.
*
* @param BlockType[] $blockTypes
* @return array
*/
private static function _getBlockTypesJsSettings(array $blockTypes): array
{
$view = Craft::$app->getView();
$jsBlockTypes = [];
foreach ($blockTypes as $blockType) {
[$blockTypeSettingsHtml, $blockTypeSettingsJs] = self::_renderBlockTypeSettings($blockType);
$jsBlockTypes[] = [
'id' => $blockType->id,
'sortOrder' => $blockType->sortOrder,
'name' => $blockType->name,
'handle' => $blockType->handle,
'enabled' => $blockType->enabled,
'description' => $blockType->description,
'minBlocks' => $blockType->minBlocks,
'maxBlocks' => $blockType->maxBlocks,
'minSiblingBlocks' => $blockType->minSiblingBlocks,
'maxSiblingBlocks' => $blockType->maxSiblingBlocks,
'minChildBlocks' => $blockType->minChildBlocks,
'maxChildBlocks' => $blockType->maxChildBlocks,
'childBlocks' => is_string($blockType->childBlocks) ? Json::decodeIfJson($blockType->childBlocks) : $blockType->childBlocks,
'topLevel' => (bool)$blockType->topLevel,
'errors' => $blockType->getErrors(),
'settingsHtml' => $blockTypeSettingsHtml,
'settingsJs' => $blockTypeSettingsJs,
'fieldLayoutId' => $blockType->fieldLayoutId,
'fieldLayoutConfig' => $blockType->getFieldLayout()->getConfig(),
'groupId' => $blockType->groupId,
];
}
return $jsBlockTypes;
}
/**
* Returns the raw data from the given block type groups, in the format used by the settings generator JavaScript.
*
* @param BlockTypeGroup[] $blockTypeGroups The Neo block type groups.
* @return array
*/
private static function _getBlockTypeGroupsJsSettings(array $blockTypeGroups): array
{
$jsBlockTypeGroups = [];
foreach ($blockTypeGroups as $blockTypeGroup) {
$jsBlockTypeGroups[] = [
'id' => $blockTypeGroup->id,
'sortOrder' => $blockTypeGroup->sortOrder,
'name' => Craft::t('site', $blockTypeGroup->name),
'alwaysShowDropdown' => $blockTypeGroup->alwaysShowDropdown,
];
}
return $jsBlockTypeGroups;
}
/**
* @param BlockType|null $blockType
* @return array
*/
private static function _renderBlockTypeSettings(?BlockType $blockType = null): array
{
$view = Craft::$app->getView();
$blockTypeId = $blockType?->id ?? '__NEOBLOCKTYPE_ID__';
$oldNamespace = $view->getNamespace();
$newNamespace = $oldNamespace . '[blockTypes][' . $blockTypeId . ']';
$view->setNamespace($newNamespace);
$view->startJsBuffer();
$html = $view->namespaceInputs($view->renderTemplate('neo/block-type-settings', [
'blockType' => $blockType,
'conditions' => self::_getConditions($blockType),
]));
$js = $view->clearJsBuffer();
$view->setNamespace($oldNamespace);
return [$html, $js];
}
/**
* Gets the condition builder field HTML for a block type.
*
* @param BlockType|null $blockType
* @return string[]
*/
private static function _getConditions(?BlockType $blockType = null): array
{
$conditionsService = Craft::$app->getConditions();
$conditionHtml = [];
Neo::$isGeneratingConditionHtml = true;
foreach (self::$_conditionElementTypes as $elementType) {
$condition = !empty($blockType?->conditions) && isset($blockType->conditions[$elementType])
? $conditionsService->createCondition($blockType->conditions[$elementType])
: $elementType::createCondition();
$condition->mainTag = 'div';
$condition->id = 'conditions-' . StringHelper::toKebabCase($elementType);
$condition->name = "conditions[$elementType]";
$condition->forProjectConfig = true;
$conditionHtml[$elementType] = Cp::fieldHtml($condition->getBuilderHtml(), [
'label' => Craft::t('neo', '{type} Condition', [
'type' => StringHelper::mb_ucwords($elementType::displayName()),
]),
'instructions' => Craft::t('neo', 'Only allow this block type to be used on {type} if they match the following rules:', [
'type' => $elementType::pluralLowerDisplayName(),
]),
]);
}
Neo::$isGeneratingConditionHtml = false;
return $conditionHtml;
}
/**
* Get the element types supported by Neo for block type conditionals.
*
* @return string[]
*/
private static function _getSupportedConditionElementTypes(): array
{
// In-built Craft element types
$elementTypes = [
Entry::class,
Category::class,
Asset::class,
User::class,
Tag::class,
Address::class,
];
// Craft Commerce element types
if (Craft::$app->getPlugins()->isPluginInstalled('commerce')) {
$elementTypes[] = Product::class;
$elementTypes[] = Variant::class;
$elementTypes[] = Order::class;
$elementTypes[] = Subscription::class;
}
return $elementTypes;
}
}