-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHasContentBlocksTrait.php
58 lines (48 loc) · 1.7 KB
/
HasContentBlocksTrait.php
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
<?php
namespace Statikbe\FilamentFlexibleContentBlocks\Models\Concerns;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasContentBlocks;
use Statikbe\FilamentFlexibleContentBlocks\View\Components\ContentBlocks;
/**
* @mixin HasContentBlocks
*
* @property array $content_blocks
*/
trait HasContentBlocksTrait
{
public function initializeHasContentBlocksTrait(): void
{
//set casts of attributes:
$this->mergeCasts([
'content_blocks' => 'array',
]);
$this->mergeFillable(['content_blocks']);
$this->registerContentBlocksCollectionsAndConversions();
}
public static function getFilamentContentBlocks(): array
{
$filamentBlocks = [];
foreach (static::registerContentBlocks() as $blockClass) {
$filamentBlocks[$blockClass::getName()] = $blockClass::make();
}
return $filamentBlocks;
}
public function getSearchableBlockContent(bool $stripHtml = true): string
{
$contentBlocksComponent = new ContentBlocks($this);
$searchableContent = collect($contentBlocksComponent->getSearchableContent());
if ($stripHtml) {
$searchableContent = $searchableContent->map(function ($item) {
return html_entity_decode(htmlspecialchars_decode(strip_tags($item)));
});
return $searchableContent->implode(" \n ");
} else {
return $searchableContent->implode(' <br> ');
}
}
protected function registerContentBlocksCollectionsAndConversions()
{
foreach (static::registerContentBlocks() as $blockClass) {
$blockClass::addMediaCollectionAndConversion($this);
}
}
}