/
MatrixBlockType.php
193 lines (167 loc) · 4.32 KB
/
MatrixBlockType.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
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
190
191
192
193
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\models;
use Craft;
use craft\base\FieldLayoutProviderInterface;
use craft\base\GqlInlineFragmentInterface;
use craft\base\Model;
use craft\behaviors\FieldLayoutBehavior;
use craft\elements\MatrixBlock;
use craft\fields\Matrix;
use yii\base\InvalidConfigException;
/**
* MatrixBlockType model class.
*
* @property bool $isNew Whether this is a new block type
* @mixin FieldLayoutBehavior
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class MatrixBlockType extends Model implements FieldLayoutProviderInterface, GqlInlineFragmentInterface
{
/**
* @var int|string|null ID The block ID. If unsaved, it will be in the format "newX".
*/
public string|int|null $id = null;
/**
* @var int|null Field ID
*/
public ?int $fieldId = null;
/**
* @var int|null Field layout ID
*/
public ?int $fieldLayoutId = null;
/**
* @var string|null Name
*/
public ?string $name = null;
/**
* @var string|null Handle
*/
public ?string $handle = null;
/**
* @var int|null Sort order
*/
public ?int $sortOrder = null;
/**
* @var bool
*/
public bool $hasFieldErrors = false;
/**
* @var string|null
*/
public ?string $uid = null;
/**
* @inheritdoc
*/
protected function defineBehaviors(): array
{
return [
'fieldLayout' => [
'class' => FieldLayoutBehavior::class,
'elementType' => MatrixBlock::class,
],
];
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['id', 'fieldId', 'sortOrder'], 'number', 'integerOnly' => true];
return $rules;
}
/**
* Use the block type handle as the string representation.
*
* @return string
*/
public function __toString(): string
{
return (string)$this->handle ?: static::class;
}
/**
* @inheritdoc
*/
public function getFieldLayout(): FieldLayout
{
/** @var FieldLayoutBehavior $behavior */
$behavior = $this->getBehavior('fieldLayout');
return $behavior->getFieldLayout();
}
/**
* Returns whether this is a new block type.
*
* @return bool
*/
public function getIsNew(): bool
{
return (!$this->id || str_starts_with($this->id, 'new'));
}
/**
* Returns the block type's field.
*
* @return Matrix
* @throws InvalidConfigException if [[fieldId]] is missing or invalid
* @since 3.3.0
*/
public function getField(): Matrix
{
if (!isset($this->fieldId)) {
throw new InvalidConfigException('Block type missing its field ID');
}
if (($field = Craft::$app->getFields()->getFieldById($this->fieldId)) === null) {
throw new InvalidConfigException('Invalid field ID: ' . $this->fieldId);
}
/** @var Matrix $field */
return $field;
}
/**
* @inheritdoc
* @since 3.3.0
*/
public function getFieldContext(): string
{
return 'matrixBlockType:' . $this->uid;
}
/**
* @inheritdoc
* @since 3.3.0
*/
public function getEagerLoadingPrefix(): string
{
return $this->handle;
}
/**
* Returns the block type’s config.
*
* @return array
* @since 3.5.0
*/
public function getConfig(): array
{
$field = $this->getField();
$config = [
'field' => $field->uid,
'name' => $this->name,
'handle' => $this->handle,
'sortOrder' => (int)$this->sortOrder,
'fields' => [],
];
$fieldLayout = $this->getFieldLayout();
$fieldLayoutConfig = $fieldLayout->getConfig();
if ($fieldLayoutConfig) {
$config['fieldLayouts'][$fieldLayout->uid] = $fieldLayoutConfig;
}
$fieldsService = Craft::$app->getFields();
foreach ($this->getCustomFields() as $field) {
$config['fields'][$field->uid] = $fieldsService->createFieldConfig($field);
}
return $config;
}
}