Skip to content

Commit c33edd7

Browse files
committed
Only output list of used blocks
1 parent 4c04072 commit c33edd7

File tree

7 files changed

+31
-17
lines changed

7 files changed

+31
-17
lines changed

src/Facades/TwillBlocks.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* @method static Collection<Block>getSettingsBlocks
1515
* @method static Collection<Block>getRepeaters
1616
* @method static registerManualBlock(string $blockClass, string $source = Block::SOURCE_APP)
17+
* @method static Collection<Block>generateListOfAllBlocks(bool $settingsOnly = false)
18+
* @method static Collection<Block>getListOfUsedBlocks()
1719
* @method static Collection<Block>generateListOfAvailableBlocks(?array $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = [], bool $defaultOrder = false)
1820
*/
1921
class TwillBlocks extends Facade

src/Http/Controllers/Admin/ModuleController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,6 @@ protected function form(?int $id, ?TwillModelContract $item = null): array
22622262
'translateTitle' => $this->titleIsTranslatable(),
22632263
'permalink' => $this->getIndexOption('permalink', $item),
22642264
'createWithoutModal' => ! $itemId && $this->getIndexOption('skipCreateModal'),
2265-
'allBlocks' => TwillBlocks::generateListOfAllBlocks()->keyBy('name'),
22662265
'form_fields' => $this->repository->getFormFields($item),
22672266
'baseUrl' => $baseUrl,
22682267
'localizedPermalinkBase' => $localizedPermalinkBase,

src/Http/Controllers/Admin/SettingController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public function index(string $section)
7878
'editableTitle' => false,
7979
'customTitle' => ucfirst($section) . ' settings',
8080
'section' => $section,
81-
'allBlocks' => TwillBlocks::generateListOfAllBlocks()->keyBy('name'),
8281
'form_fields' => $formFields,
8382
'formBuilder' => Form::make(),
8483
'saveUrl' => $this->urlGenerator->route(config('twill.admin_route_name_prefix') . 'settings.update', $section),

src/TwillBlocks.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ class TwillBlocks
4444
public static $manualBlocks = [];
4545

4646
/**
47-
* @return A17\Twill\Services\Blocks\BlockCollection
47+
* @return \A17\Twill\Services\Blocks\BlockCollection
4848
*/
4949
private ?BlockCollection $blockCollection = null;
5050

5151
private array $cropConfigs = [];
5252

53+
private Collection $usedBlocks;
54+
55+
public function __construct()
56+
{
57+
$this->usedBlocks = collect();
58+
}
5359
/**
5460
* Registers a blocks directory.
5561
*
@@ -347,6 +353,11 @@ public function getAllCropConfigs(): array
347353
return $this->cropConfigs;
348354
}
349355

356+
public function getListOfUsedBlocks(): Collection
357+
{
358+
return $this->usedBlocks;
359+
}
360+
350361
public function generateListOfAllBlocks(bool $settingsOnly = false): Collection
351362
{
352363
return once(function () use ($settingsOnly) {
@@ -380,7 +391,7 @@ function ($appBlock) use ($block) {
380391
return false;
381392
}
382393
}
383-
return isset($disabledBlocks[$block->name]) || isset($disabledBlocks[ltrim($block->componentClass, '\\')]);
394+
return !(isset($disabledBlocks[$block->name]) || isset($disabledBlocks[ltrim($block->componentClass, '\\')]));
384395
})->sortBy(function (Block $b) use ($customOrder) {
385396
// Sort blocks by custom order then by group and then by name
386397
return ($customOrder[$b->name] ?? $customOrder[ltrim($b->componentClass, '\\')] ?? PHP_INT_MAX) . '-' . $b->group . '-' . $b->name;
@@ -395,7 +406,7 @@ public function generateListOfAvailableBlocks(
395406
array|callable $excludeBlocks = [],
396407
bool $defaultOrder = false
397408
): Collection {
398-
$globalExcludeBlocks = TwillBlocks::getGloballyExcludedBlocks();
409+
$globalExcludeBlocks = $this->getGloballyExcludedBlocks();
399410

400411
$matchBlock = function ($matcher, $block, $someFn = null) {
401412
if (is_callable($matcher)) {
@@ -443,6 +454,8 @@ function (Block $block) use ($blocks, $groups, $excludeBlocks, $globalExcludeBlo
443454
$finalList = $finalList->sortBy(fn(Block $block) => $groups[$block->group] ?? PHP_INT_MAX, SORT_NUMERIC);
444455
}
445456
}
457+
458+
$this->usedBlocks = $this->usedBlocks->merge($finalList->keyBy(fn(Block $block) => $block->name));
446459
return $finalList;
447460
}
448461
}

views/layouts/form.blade.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
@php use A17\Twill\Facades\TwillBlocks; @endphp
12
@extends('twill::layouts.main')
23

34
@section('appTypeClass', 'body--form')
45

56
@push('extra_css')
67
@if(app()->isProduction())
7-
<link href="{{ twillAsset('main-form.css') }}" rel="preload" as="style" crossorigin/>
8+
<link href="{{ twillAsset('main-form.css') }}" rel="preload" as="style" crossorigin />
89
@endif
910

1011
@unless(config('twill.dev_mode', false))
11-
<link href="{{ twillAsset('main-form.css') }}" rel="stylesheet" crossorigin/>
12+
<link href="{{ twillAsset('main-form.css') }}" rel="stylesheet" crossorigin />
1213
@endunless
1314
@endpush
1415

1516
@push('extra_js_head')
1617
@if(app()->isProduction())
17-
<link href="{{ twillAsset('main-form.js') }}" rel="preload" as="script" crossorigin/>
18+
<link href="{{ twillAsset('main-form.js') }}" rel="preload" as="script" crossorigin />
1819
@endif
1920
@endpush
2021

@@ -136,7 +137,7 @@
136137
:items-in-selects-tables="$users"
137138
label-key="name"
138139
name-pattern="user_%id%_permission"
139-
:list-user="true"/>
140+
:list-user="true" />
140141
</x-twill::formFieldset>
141142
@endcan
142143
@endif
@@ -155,7 +156,8 @@
155156
</a17-modal>
156157
<a17-editor v-if="editor" ref="editor"
157158
bg-color="{{ config('twill.block_editor.background_color') ?? '#FFFFFF' }}"></a17-editor>
158-
<a17-previewer ref="preview" :breakpoints-config="{{ json_encode(config('twill.preview.breakpoints')) }}"></a17-previewer>
159+
<a17-previewer ref="preview"
160+
:breakpoints-config="{{ json_encode(config('twill.preview.breakpoints')) }}"></a17-previewer>
159161
<a17-dialog ref="warningContentEditor" modal-title="{{ twillTrans('twill::lang.form.dialogs.delete.title') }}"
160162
confirm-label="{{ twillTrans('twill::lang.form.dialogs.delete.confirm') }}">
161163
<p class="modal--tiny-title">
@@ -173,7 +175,7 @@
173175
restoreUrl: '{{ $restoreUrl ?? '' }}',
174176
availableBlocks: {},
175177
blocks: {},
176-
allAvailableBlocks: {!! json_encode($allBlocks ?? []) !!},
178+
allAvailableBlocks: {!! TwillBlocks::getListOfUsedBlocks() !!}
177179
blockPreviewUrl: '{{ $blockPreviewUrl ?? '' }}',
178180
repeaters: {!! json_encode(($form_fields['repeaters'] ?? []) + ($form_fields['blocksRepeaters'] ?? [])) !!},
179181
fields: [],
@@ -235,10 +237,10 @@
235237
const checked = mutation.payload.value
236238
if (!isNaN(groupId)) {
237239
const users = groupUserMapping[groupId]
238-
users.forEach(function (userId) {
240+
users.forEach(function(userId) {
239241
// If the user's permission is <= view, it will be updated
240-
const currentPermission = state['form']['fields'].find(function (e) {
241-
return e.name == `user_${userId}_permission`
242+
const currentPermission = state['form']['fields'].find(function(e) {
243+
return e.name === `user_${userId}_permission`
242244
}).value
243245
if (currentPermission === '' || currentPermission === 'view-item') {
244246
const field = {

views/layouts/main.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
window.STORE.browsers.selected = {}
146146
147147
@stack('vuexStore')
148+
148149
</script>
149150
<script src="{{ twillAsset('chunk-vendors.js') }}"></script>
150151
<script src="{{ twillAsset('chunk-common.js') }}"></script>

views/partials/form/utils/_blocks_templates.blade.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
@php
2-
$blocks = \A17\Twill\Facades\TwillBlocks::getBlockCollection()
3-
->collect()
2+
$blocks = \A17\Twill\Facades\TwillBlocks::getListOfUsedBlocks()
43
->reject(function ($block) {
54
return $block->compiled ?? false;
65
});
@@ -26,4 +25,3 @@
2625
window['{{ config('twill.js_namespace') }}'].STORE.form.availableRepeaters = {!! \A17\Twill\Facades\TwillBlocks::getAvailableRepeaters() ?? '{}' !!}
2726
2827
</script>
29-

0 commit comments

Comments
 (0)