Skip to content

Commit ff77869

Browse files
authored
feat: allow published at to be empty by default per content type (#39)
* feat: allow published at to be empty by default per content type * fix: styling * fix: styling --------- Co-authored-by: Baspa <[email protected]>
1 parent e575d32 commit ff77869

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('types', function (Blueprint $table) {
15+
$table->boolean('published_at_empty_on_create')->default(false)->after('parent_required');
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('types', function (Blueprint $table) {
25+
$table->dropColumn('published_at_empty_on_create');
26+
});
27+
}
28+
};

packages/core/src/Models/Type.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ protected function casts(): array
2525
return [
2626
'og_image_fields' => 'array',
2727
'parent_filters' => 'array',
28+
'published_at_empty_on_create' => 'boolean',
2829
'default_meta_tags_robots' => 'array',
2930
];
3031
}

packages/core/src/Resources/ContentResource.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,43 @@ function (Set $set, $component) {
565565
DateTimePicker::make('published_at')
566566
->columnSpanFull()
567567
->date()
568-
->default(now()->format('dd/mm/YYYY'))
568+
->default(function (Get $get, ?Content $record) {
569+
if ($record) {
570+
return $record->published_at;
571+
}
572+
573+
$type = self::$type;
574+
if (! $type) {
575+
$type = Type::firstWhere('slug', ($get('type_slug') ?? $record?->type_slug));
576+
}
577+
578+
if ($type && $type->published_at_empty_on_create) {
579+
return null;
580+
}
581+
582+
return now();
583+
})
569584
->displayFormat('M j, Y - H:i')
570-
->formatStateUsing(fn (?Content $record) => $record ? $record->published_at : now())
585+
->formatStateUsing(function (?Content $record, Get $get) {
586+
if ($record && $record->published_at) {
587+
return $record->published_at;
588+
}
589+
590+
if ($record) {
591+
return null;
592+
}
593+
594+
$type = self::$type;
595+
if (! $type) {
596+
$type = Type::firstWhere('slug', $get('type_slug'));
597+
}
598+
599+
if ($type && $type->published_at_empty_on_create) {
600+
return null;
601+
}
602+
603+
return now();
604+
})
571605
->label(__('Publication date'))
572606
->helperText('Set a date in past or future to schedule publication.')
573607
->native(false)

packages/core/src/Resources/TypeResource.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ public static function form(\Filament\Schemas\Schema $schema): \Filament\Schemas
157157
'desc' => 'Descending',
158158
]),
159159
])->columns(2),
160+
Fieldset::make(__('Publication'))
161+
->schema([
162+
Toggle::make('published_at_empty_on_create')
163+
->label(__('Published At Empty On Create'))
164+
->helperText(__('If enabled, published_at will be empty when creating new content. Publication date must be set manually.'))
165+
->inline(false)
166+
->columnSpanFull(),
167+
]),
160168
Fieldset::make(__('Meta Tags'))
161169
->schema([
162170
Select::make('default_meta_tags_robots')
@@ -173,6 +181,7 @@ public static function form(\Filament\Schemas\Schema $schema): \Filament\Schemas
173181
->helperText(__('If enabled, all content of this type must have a parent.'))
174182
->live()
175183
->inline(false),
184+
176185
Repeater::make('parent_filters')
177186
->label(__('Filters'))
178187
->live()

0 commit comments

Comments
 (0)