-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathGroup.php
More file actions
158 lines (135 loc) · 4.41 KB
/
Group.php
File metadata and controls
158 lines (135 loc) · 4.41 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
<?php
namespace Statamic\Fieldtypes;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Fields;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\Values;
use Statamic\GraphQL\Types\GroupType;
use Statamic\Support\Arr;
use Statamic\Support\Str;
class Group extends Fieldtype
{
protected $categories = ['structured'];
protected $defaultable = false;
protected function configFieldItems(): array
{
return [
[
'display' => __('Fields'),
'fields' => [
'fields' => [
'display' => __('Fields'),
'instructions' => __('statamic::fieldtypes.group.config.fields'),
'type' => 'fields',
'full_width_setting' => true,
],
],
],
[
'display' => __('Appearance & Behavior'),
'fields' => [
'fullscreen' => [
'display' => __('Allow Fullscreen Mode'),
'instructions' => __('statamic::fieldtypes.grid.config.fullscreen'),
'type' => 'toggle',
'default' => true,
],
'border' => [
'display' => __('Border'),
'instructions' => __('statamic::fieldtypes.grid.config.border'),
'type' => 'toggle',
'default' => true,
],
],
],
];
}
public function process($data)
{
$values = $this->fields()->addValues($data ?? [])->process()->values()->all();
return Arr::removeNullValues($values);
}
public function preProcess($data)
{
return $this->fields()->addValues($data ?? [])->preProcess()->values()->all();
}
public function fields()
{
return new Fields($this->config('fields'), $this->field()->parent(), $this->field());
}
public function rules(): array
{
return ['array'];
}
public function extraRules(): array
{
$rules = $this
->fields()
->addValues((array) $this->field->value())
->validator()
->withContext([
'prefix' => $this->field->handle().'.',
])
->rules();
return collect($rules)->mapWithKeys(function ($rules, $handle) {
return [$this->field->handle().'.'.$handle => $rules];
})->all();
}
public function extraValidationAttributes(): array
{
return collect($this->fields()->validator()->attributes())->mapWithKeys(function ($attribute, $handle) {
return [$this->field->handle().'.'.$handle => $attribute];
})->all();
}
public function preload()
{
return $this->fields()->addValues($this->field->value() ?? $this->defaultGroupData())->meta()->toArray();
}
protected function defaultGroupData()
{
return $this->fields()->all()->map(function ($field) {
return $field->fieldtype()->preProcess($field->defaultValue());
})->all();
}
public function augment($value)
{
return $this->performAugmentation($value, false);
}
public function shallowAugment($value)
{
return $this->performAugmentation($value, true);
}
private function performAugmentation($value, $shallow)
{
$method = $shallow ? 'shallowAugment' : 'augment';
return new Values($this->fields()->addValues($value ?? [])->{$method}()->values()->all());
}
public function preProcessValidatable($value)
{
return array_merge(
$value ?? [],
$this->fields()
->addValues($value ?? [])
->preProcessValidatables()
->values()
->all(),
);
}
public function toGqlType()
{
return GraphQL::type($this->gqlItemTypeName());
}
public function addGqlTypes()
{
GraphQL::addType(new GroupType($this, $this->gqlItemTypeName()));
$this->fields()->all()->each(function ($field) {
$field->fieldtype()->addGqlTypes();
});
}
private function gqlItemTypeName()
{
return 'Group_'.collect($this->field->handlePath())->map(function ($part) {
return Str::studly($part);
})->join('_');
}
}