generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAbstractContentBlock.php
More file actions
189 lines (159 loc) · 5.79 KB
/
AbstractContentBlock.php
File metadata and controls
189 lines (159 loc) · 5.79 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Statikbe\FilamentFlexibleContentBlocks\ContentBlocks;
use Closure;
use Filament\Forms\Components\Builder\Block;
use Filament\Support\Icons\Heroicon;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\View\Component;
use Spatie\MediaLibrary\HasMedia;
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Builder\ContentBlockWithPreview;
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\BlockIdField;
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleBlocksConfig;
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleContentBlocks;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasContentBlocks;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasMediaAttributes;
/**
* This is the base class from which all flexible content blocks should inherit.
*/
abstract class AbstractContentBlock extends Component
{
public final const CONVERSION_CROP = 'crop';
public final const CONVERSION_CONTAIN = 'contain';
protected static Block $block;
public Model&HasContentBlocks&HasMedia $record;
public ?array $blockData;
private string $blockId;
/**
* Create a new component instance.
*/
public function __construct(Model&HasContentBlocks&HasMedia $record, ?array $blockData)
{
$this->record = $record;
$this->blockData = $blockData;
// block id:
if (! isset($this->blockData[BlockIdField::FIELD]) || ! $this->blockData[BlockIdField::FIELD]) {
// initialise the ID for a new block, then never change it.
$this->blockData[BlockIdField::FIELD] = BlockIdField::generateBlockId();
}
$this->blockId = $this->blockData[BlockIdField::FIELD];
}
/**
* Get the name/type of this block
*/
abstract public static function getName(): string;
/**
* Get the heroicon of this block
*/
abstract public static function getIcon(): \BackedEnum|string;
/**
* Get translated label of this block
*/
abstract public static function getLabel(): string;
/**
* Get the translated label of this block in the context of the given state.
* You can use this to display the title or other state data in the block label.
*/
public static function getContextualLabel(?array $state): ?string
{
return null;
}
/**
* Get the translated label of the given field.
*/
abstract public static function getFieldLabel(string $field): string;
/**
* Make a new Filament Block instance for this flexible block.
*/
public static function make(): Block
{
return ContentBlockWithPreview::make(static::getName())
->label(function (?array $state) {
return static::getContextualLabel($state) ?? static::getLabel();
})
->icon(static::getIcon())
->schema(static::getFilamentBlockSchema())
->visible(static::visible())
->contentBlockClass(static::class);
}
/**
* Returns the final block schema with a block ID hidden field.
*/
protected static function getFilamentBlockSchema(): Closure
{
return function (\Filament\Schemas\Components\Component $component) {
return array_merge([
// keep track of block id:
BlockIdField::create(),
],
$component->evaluate(static::makeFilamentSchema()));
};
}
/**
* Returns the block's form schema consisting of an list of Filament form components or a closure that returns such a list.
*
* @return array<\Filament\Schemas\Components\Component>|Closure
*/
abstract protected static function makeFilamentSchema(): array|Closure;
/**
* Get the view / contents that represent the component.
*
* @return View|Closure|string
*/
abstract public function render();
public static function getPreviewView(): string
{
$themePrefix = FilamentFlexibleBlocksConfig::getViewThemePrefix();
return "filament-flexible-content-blocks::content-blocks.{$themePrefix}preview";
}
/**
* Registers media collection and conversions for the media fields of this block to the model.
*/
public static function addMediaCollectionAndConversion(HasMedia&HasMediaAttributes $record): void
{
// overwrite to add collection and conversion here if the block has images
}
/**
* Sets the visibility of the block in the builder. Call this to hide or show the block in the builder selector.
*
* @return bool|Closure $condition
*/
public static function visible(): bool|Closure
{
return true;
}
/**
* Sets the visibility of block styles on the block form. Call this to hide or show the block styles field in the block form.
*
* @return bool|Closure $condition
*/
public static function hasBlockStyles(): bool|Closure
{
return FilamentFlexibleBlocksConfig::isBlockStyleEnabled(static::class);
}
/**
* Return an array of strings with searchable content of the block fields.
*
* @return array<string>
*/
abstract public function getSearchableContent(): array;
/**
* @param array<string> $searchableContent
* @return array<string>
*/
protected function addSearchableContent(array &$searchableContent, ?string $textContent): array
{
if ($textContent && ! empty(trim($textContent))) {
$searchableContent[] = $textContent;
}
return $searchableContent;
}
public function replaceParameters(?string $content): ?string
{
return FilamentFlexibleContentBlocks::replaceParameters($content);
}
public function getBlockId(): string
{
return $this->blockId;
}
}