diff --git a/src/Facades/TwillBlocks.php b/src/Facades/TwillBlocks.php index 47a6eb3f3..23a63d558 100644 --- a/src/Facades/TwillBlocks.php +++ b/src/Facades/TwillBlocks.php @@ -14,6 +14,8 @@ * @method static CollectiongetSettingsBlocks * @method static CollectiongetRepeaters * @method static registerManualBlock(string $blockClass, string $source = Block::SOURCE_APP) + * @method static CollectiongenerateListOfAllBlocks(bool $settingsOnly = false) + * @method static CollectiongetListOfUsedBlocks() * @method static CollectiongenerateListOfAvailableBlocks(?array $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = [], bool $defaultOrder = false) */ class TwillBlocks extends Facade diff --git a/src/Http/Controllers/Admin/ModuleController.php b/src/Http/Controllers/Admin/ModuleController.php index 18d7f4122..dcf4b820b 100644 --- a/src/Http/Controllers/Admin/ModuleController.php +++ b/src/Http/Controllers/Admin/ModuleController.php @@ -2262,7 +2262,6 @@ protected function form(?int $id, ?TwillModelContract $item = null): array 'translateTitle' => $this->titleIsTranslatable(), 'permalink' => $this->getIndexOption('permalink', $item), 'createWithoutModal' => ! $itemId && $this->getIndexOption('skipCreateModal'), - 'allBlocks' => TwillBlocks::generateListOfAllBlocks()->keyBy('name'), 'form_fields' => $this->repository->getFormFields($item), 'baseUrl' => $baseUrl, 'localizedPermalinkBase' => $localizedPermalinkBase, diff --git a/src/Http/Controllers/Admin/SettingController.php b/src/Http/Controllers/Admin/SettingController.php index 2dc670667..e81319242 100644 --- a/src/Http/Controllers/Admin/SettingController.php +++ b/src/Http/Controllers/Admin/SettingController.php @@ -78,7 +78,6 @@ public function index(string $section) 'editableTitle' => false, 'customTitle' => ucfirst($section) . ' settings', 'section' => $section, - 'allBlocks' => TwillBlocks::generateListOfAllBlocks()->keyBy('name'), 'form_fields' => $formFields, 'formBuilder' => Form::make(), 'saveUrl' => $this->urlGenerator->route(config('twill.admin_route_name_prefix') . 'settings.update', $section), diff --git a/src/TwillBlocks.php b/src/TwillBlocks.php index 906b339b3..7e70d8fe2 100644 --- a/src/TwillBlocks.php +++ b/src/TwillBlocks.php @@ -44,12 +44,18 @@ class TwillBlocks public static $manualBlocks = []; /** - * @return A17\Twill\Services\Blocks\BlockCollection + * @return \A17\Twill\Services\Blocks\BlockCollection */ private ?BlockCollection $blockCollection = null; private array $cropConfigs = []; + private Collection $usedBlocks; + + public function __construct() + { + $this->usedBlocks = collect(); + } /** * Registers a blocks directory. * @@ -347,6 +353,11 @@ public function getAllCropConfigs(): array return $this->cropConfigs; } + public function getListOfUsedBlocks(): Collection + { + return $this->usedBlocks; + } + public function generateListOfAllBlocks(bool $settingsOnly = false): Collection { return once(function () use ($settingsOnly) { @@ -380,7 +391,7 @@ function ($appBlock) use ($block) { return false; } } - return isset($disabledBlocks[$block->name]) || isset($disabledBlocks[ltrim($block->componentClass, '\\')]); + return !(isset($disabledBlocks[$block->name]) || isset($disabledBlocks[ltrim($block->componentClass, '\\')])); })->sortBy(function (Block $b) use ($customOrder) { // Sort blocks by custom order then by group and then by name return ($customOrder[$b->name] ?? $customOrder[ltrim($b->componentClass, '\\')] ?? PHP_INT_MAX) . '-' . $b->group . '-' . $b->name; @@ -395,7 +406,7 @@ public function generateListOfAvailableBlocks( array|callable $excludeBlocks = [], bool $defaultOrder = false ): Collection { - $globalExcludeBlocks = TwillBlocks::getGloballyExcludedBlocks(); + $globalExcludeBlocks = $this->getGloballyExcludedBlocks(); $matchBlock = function ($matcher, $block, $someFn = null) { if (is_callable($matcher)) { @@ -443,6 +454,8 @@ function (Block $block) use ($blocks, $groups, $excludeBlocks, $globalExcludeBlo $finalList = $finalList->sortBy(fn(Block $block) => $groups[$block->group] ?? PHP_INT_MAX, SORT_NUMERIC); } } + + $this->usedBlocks = $this->usedBlocks->merge($finalList->keyBy(fn(Block $block) => $block->name)); return $finalList; } } diff --git a/views/layouts/form.blade.php b/views/layouts/form.blade.php index 72785faf5..c1e91ea60 100644 --- a/views/layouts/form.blade.php +++ b/views/layouts/form.blade.php @@ -1,20 +1,21 @@ +@php use A17\Twill\Facades\TwillBlocks; @endphp @extends('twill::layouts.main') @section('appTypeClass', 'body--form') @push('extra_css') @if(app()->isProduction()) - + @endif @unless(config('twill.dev_mode', false)) - + @endunless @endpush @push('extra_js_head') @if(app()->isProduction()) - + @endif @endpush @@ -136,7 +137,7 @@ :items-in-selects-tables="$users" label-key="name" name-pattern="user_%id%_permission" - :list-user="true"/> + :list-user="true" /> @endcan @endif @@ -155,7 +156,8 @@ - +