-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathAbstractPostBlockForm.php
More file actions
109 lines (90 loc) · 3.21 KB
/
AbstractPostBlockForm.php
File metadata and controls
109 lines (90 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace App\Sharp\Posts\Blocks;
use App\Models\PostBlock;
use App\Sharp\Entities\PostEntity;
use Code16\Sharp\Form\Eloquent\WithSharpFormEloquentUpdater;
use Code16\Sharp\Form\Fields\SharpFormField;
use Code16\Sharp\Form\Fields\SharpFormHtmlField;
use Code16\Sharp\Form\Fields\SharpFormTextareaField;
use Code16\Sharp\Form\Layout\FormLayout;
use Code16\Sharp\Form\Layout\FormLayoutColumn;
use Code16\Sharp\Form\SharpForm;
use Code16\Sharp\Utils\Fields\FieldsContainer;
abstract class AbstractPostBlockForm extends SharpForm
{
use WithSharpFormEloquentUpdater;
protected static string $postBlockType = 'none';
protected static string $postBlockHelpText = '';
public function buildFormFields(FieldsContainer $formFields): void
{
$formFields
->addField(
SharpFormHtmlField::make('type')
->setTemplate(<<<'blade'
Post block type: <strong>{{ $name }}</strong>
@if($help)
<div><small>{{ $help }}</small></div>
@endif
blade),
);
if ($field = $this->getContentField()) {
$formFields->addField($field);
}
$this->buildAdditionalFields($formFields);
}
public function buildFormLayout(FormLayout $formLayout): void
{
$formLayout
->addColumn(6, function (FormLayoutColumn $column) {
$column->withField('type')
->when($this->getContentField(), fn ($column) => $column->withField('content'));
$this->addAdditionalFieldsToLayout($column);
});
}
public function buildFormConfig(): void {}
public function find($id): array
{
return $this
->setCustomTransformer('type', function () {
return [
'name' => static::$postBlockType,
'help' => static::$postBlockHelpText,
];
})
->addCustomTransformers()
->transform(PostBlock::with('files')->findOrFail($id));
}
protected function addCustomTransformers(): self
{
return $this;
}
public function create(): array
{
// Provide data to fill the dummy html field on creation
return [
'type' => [
'name' => static::$postBlockType,
'help' => static::$postBlockHelpText,
],
];
}
public function update($id, array $data)
{
$postBlock = $id
? PostBlock::findOrFail($id)
: new PostBlock([
'type' => static::$postBlockType,
'post_id' => sharp()->context()->breadcrumb()->previousShowSegment(PostEntity::class)->instanceId(),
]);
$this->save($postBlock, $data);
return $postBlock->id;
}
protected function getContentField(): ?SharpFormField
{
return SharpFormTextareaField::make('content')
->setLabel('Content')
->setRowCount(6);
}
protected function buildAdditionalFields(FieldsContainer $formFields): void {}
protected function addAdditionalFieldsToLayout(FormLayoutColumn $column): void {}
}