Skip to content

Commit 0d64e29

Browse files
committed
Alert when page linked to menu item is deleted: refactored to separate action classes: fix bugs + translations
1 parent df21326 commit 0d64e29

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

resources/lang/en/filament-flexible-content-block-pages.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
'table' => [
1717
'created_at_col' => 'Created at',
1818
],
19+
'notifications' => [
20+
'used_in_menu' => 'This page is used in the ":menu" menu, in item :menu_item. Please update the menu first and then try deleting this page again.',
21+
'used_in_menu_bulk' => 'Page ":page" is used in the ":menu" menu, in item :menu_item. Please update the menu first and then try deleting this page again.',
22+
],
23+
1924
],
2025
'redirects' => [
2126
'redirects_lbl' => 'redirect',

resources/lang/nl/filament-flexible-content-block-pages.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
'notifications' => [
2020
'used_in_menu' => 'Deze pagina wordt in het ":menu" menu gebruikt, in item :menu_item. Gelieve eerst het menu aan te passen en dan deze pagina opnieuw te verwijderen.',
21+
'used_in_menu_bulk' => 'Pagina ":page" wordt in het ":menu" menu gebruikt, in item :menu_item. Gelieve eerst het menu aan te passen en dan deze pagina opnieuw te verwijderen.',
2122
],
2223
],
2324
'redirects' => [

src/Actions/LinkedToMenuItemBulkDeleteAction.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setUp(): void
1919
{
2020
parent::setUp();
2121

22-
$this->action(function (Collection $records, Action $action) {
22+
$this->action(function (Collection $records, LinkedToMenuItemBulkDeleteAction $action) {
2323
$usedInMenu = false;
2424
foreach ($records as $record) {
2525
/** @var Model&HasMenuLabel $record */
@@ -31,16 +31,21 @@ public function setUp(): void
3131
$usedInMenu = true;
3232

3333
Notification::make()
34-
->title(flexiblePagesTrans('pages.notifications.used_in_menu', [
34+
->title(flexiblePagesTrans('pages.notifications.used_in_menu_bulk', [
35+
'page' => $record->getMenuLabel(),
3536
'menu' => $menuItem->menu->name,
3637
'menu_item' => $menuItem->getDisplayLabel(),
3738
]))
3839
->danger()
40+
->duration(12000)
3941
->send();
4042
}
4143
}
4244

43-
if (! $usedInMenu) {
45+
if ($usedInMenu) {
46+
$action->failure();
47+
}
48+
else {
4449
$this->process(static fn (Collection $records) => $records->each(fn (Model&HasMenuLabel $record) => $record->delete()));
4550
$action->success();
4651
}

src/Actions/LinkedToMenuItemDeleteAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function setUp(): void
2929
'menu_item' => $menuItem->getDisplayLabel(),
3030
]))
3131
->danger()
32+
->duration(12000)
3233
->send();
3334
} else {
3435
$record->delete();

src/Resources/PageResource.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Filament\Tables\Actions\EditAction;
1212
use Filament\Tables\Columns\TextColumn;
1313
use Filament\Tables\Table;
14+
use Illuminate\Database\Eloquent\Builder;
1415
use Statikbe\FilamentFlexibleContentBlockPages\Actions\LinkedToMenuItemBulkDeleteAction;
1516
use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages;
1617
use Statikbe\FilamentFlexibleContentBlockPages\Resources\PageResource\Pages\CreatePage;
@@ -167,7 +168,7 @@ protected static function getAdvancedTabFields(): array
167168
->searchable(['title', 'code', 'slug', 'intro']);
168169
}
169170

170-
if (! empty($gridFields)) {
171+
if (!empty($gridFields)) {
171172
$fields[] = Grid::make()->schema($gridFields);
172173
}
173174

@@ -197,8 +198,11 @@ public static function table(Table $table): Table
197198
LinkedToMenuItemBulkDeleteAction::make(),
198199
])
199200
->recordUrl(
200-
fn ($record): string => static::getUrl('edit', ['record' => $record])
201-
);
201+
fn($record): string => static::getUrl('edit', ['record' => $record])
202+
)
203+
->modifyQueryUsing(function (Builder $query) {
204+
$query->with(['menuItem']);
205+
});
202206
}
203207

204208
public static function getRelations(): array

0 commit comments

Comments
 (0)