|
16 | 16 | use Code16\Sharp\Form\Fields\SharpFormCheckField; |
17 | 17 | use Code16\Sharp\Form\Fields\SharpFormDateField; |
18 | 18 | use Code16\Sharp\Form\Fields\SharpFormEditorField; |
| 19 | +use Code16\Sharp\Form\Fields\SharpFormHtmlField; |
19 | 20 | use Code16\Sharp\Form\Fields\SharpFormListField; |
20 | 21 | use Code16\Sharp\Form\Fields\SharpFormTagsField; |
21 | 22 | use Code16\Sharp\Form\Fields\SharpFormTextareaField; |
|
27 | 28 | use Code16\Sharp\Form\Layout\FormLayoutTab; |
28 | 29 | use Code16\Sharp\Form\SharpForm; |
29 | 30 | use Code16\Sharp\Utils\Fields\FieldsContainer; |
| 31 | +use Illuminate\Support\Facades\Blade; |
30 | 32 |
|
31 | 33 | class PostForm extends SharpForm |
32 | 34 | { |
@@ -104,6 +106,29 @@ public function buildFormFields(FieldsContainer $formFields): void |
104 | 106 | ->setLabel('Publication date') |
105 | 107 | ->setHasTime(), |
106 | 108 | ) |
| 109 | + ->addField( |
| 110 | + SharpFormHtmlField::make('publication_label') |
| 111 | + ->setLiveRefresh(linkedFields: ['author_id', 'published_at', 'attachments']) |
| 112 | + ->setTemplate(function (array $data) { |
| 113 | + if (! isset($data['published_at'])) { |
| 114 | + return ''; |
| 115 | + } |
| 116 | + |
| 117 | + return Blade::render(<<<'blade' |
| 118 | + This post will be published on {{ $published_at }} |
| 119 | + @if($author) |
| 120 | + by {{ $author->name }}. |
| 121 | + @endif |
| 122 | + <br> |
| 123 | + {{ count($linkAttachments) }} link attachments, {{ count($fileAttachments) }} file attachments. |
| 124 | + blade, [ |
| 125 | + 'published_at' => \Carbon\Carbon::parse($data['published_at'])->isoFormat('LLLL'), |
| 126 | + 'author' => \App\Models\User::find($data['author_id']), |
| 127 | + 'linkAttachments' => collect($data['attachments'])->where('is_link', true)->values(), |
| 128 | + 'fileAttachments' => collect($data['attachments'])->where('is_link', false)->values(), |
| 129 | + ]); |
| 130 | + }) |
| 131 | + ) |
107 | 132 | ->addField( |
108 | 133 | SharpFormListField::make('attachments') |
109 | 134 | ->setLabel('Attachments') |
@@ -131,7 +156,7 @@ public function buildFormFields(FieldsContainer $formFields): void |
131 | 156 | ->setStorageDisk('local') |
132 | 157 | ->setStorageBasePath('data/posts/{id}') |
133 | 158 | ->addConditionalDisplay('!is_link'), |
134 | | - ), |
| 159 | + ) |
135 | 160 | ) |
136 | 161 | ->when(sharp()->context()->isUpdate(), fn ($formFields) => $formFields->addField( |
137 | 162 | SharpFormAutocompleteRemoteField::make('author_id') |
@@ -168,10 +193,10 @@ public function buildFormLayout(FormLayout $formLayout): void |
168 | 193 | fn ($column) => $column->withField('author_id') |
169 | 194 | ) |
170 | 195 | ->withFields('published_at', 'categories') |
| 196 | + ->withField('publication_label') |
171 | 197 | ->withListField('attachments', function (FormLayoutColumn $item) { |
172 | 198 | $item->withFields(title: 8, is_link: 4) |
173 | | - ->withField('link_url') |
174 | | - ->withField('document'); |
| 199 | + ->withField('link_url'); |
175 | 200 | }); |
176 | 201 | }) |
177 | 202 | ->addColumn(6, function (FormLayoutColumn $column) { |
|
0 commit comments