|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NovaFlexibleContent\Nova\Collections; |
|
4
|
|
|
|
|
5
|
|
|
use Laravel\Nova\Fields\Field; |
|
6
|
|
|
use Laravel\Nova\Fields\FieldCollection as NovaFieldCollection; |
|
7
|
|
|
use Laravel\Nova\Http\Requests\NovaRequest; |
|
8
|
|
|
use Laravel\Nova\Resource; |
|
9
|
|
|
use NovaFlexibleContent\Flexible; |
|
10
|
|
|
use NovaFlexibleContent\Http\FlexibleAttribute; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @extends \Laravel\Nova\Fields\FieldCollection<int, \Laravel\Nova\Fields\Field> |
|
14
|
|
|
*/ |
|
15
|
|
|
class FieldsCollection extends NovaFieldCollection |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @inheritDoc |
|
19
|
|
|
*/ |
|
20
|
4 |
|
public function findFieldByAttribute($attribute, $default = null) |
|
21
|
|
|
{ |
|
22
|
4 |
|
if (str_contains($attribute, FlexibleAttribute::GROUP_SEPARATOR)) { |
|
23
|
3 |
|
return $this->findFieldUsedInFlexibleByAttribute($attribute, $default); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
return parent::findFieldByAttribute($attribute, $default); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
3 |
|
public function findFieldUsedInFlexibleByAttribute($attribute, mixed $default = null) |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var NovaRequest $request */ |
|
32
|
3 |
|
$request = resolve(NovaRequest::class); |
|
33
|
|
|
|
|
34
|
|
|
/** @var Resource $resource */ |
|
35
|
3 |
|
$resource = $request->findResourceOrFail(); |
|
36
|
|
|
|
|
37
|
3 |
|
[$groupKey, $fieldKey] = explode(FlexibleAttribute::GROUP_SEPARATOR, $attribute, 2); |
|
38
|
|
|
|
|
39
|
3 |
|
foreach ($resource->updateFields($request) as $field) { |
|
40
|
3 |
|
if ($field instanceof Flexible) { |
|
41
|
3 |
|
if ($group = $field->findGroupRecursive($groupKey)) { |
|
42
|
2 |
|
$foundField = $group->fieldsCollection() |
|
43
|
2 |
|
->first(fn (Field $groupField) => $groupField->attribute == $fieldKey); |
|
44
|
|
|
|
|
45
|
2 |
|
if ($foundField) { |
|
46
|
2 |
|
return $foundField; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
return value($default); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|