-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathContentBlock.php
More file actions
231 lines (201 loc) Β· 5.14 KB
/
ContentBlock.php
File metadata and controls
231 lines (201 loc) Β· 5.14 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\elements;
use Craft;
use craft\base\Element;
use craft\base\NestedElementInterface;
use craft\base\NestedElementTrait;
use craft\db\Table;
use craft\elements\db\ContentBlockQuery;
use craft\fields\ContentBlock as ContentBlockField;
use craft\gql\interfaces\elements\ContentBlock as ContentBlockInterface;
use craft\models\FieldLayout;
use craft\records\ContentBlock as ContentBlockRecord;
use GraphQL\Type\Definition\Type;
use yii\base\InvalidConfigException;
/**
* Content block element.
*
* @method ContentBlockField getField()
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 5.8.0
*/
class ContentBlock extends Element implements NestedElementInterface
{
use NestedElementTrait;
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Content Block');
}
/**
* @inheritdoc
*/
public static function lowerDisplayName(): string
{
return Craft::t('app', 'content block');
}
/**
* @inheritdoc
*/
public static function pluralDisplayName(): string
{
return Craft::t('app', 'Content Blocks');
}
/**
* @inheritdoc
*/
public static function pluralLowerDisplayName(): string
{
return Craft::t('app', 'content blocks');
}
/**
* @inheritdoc
*/
public static function refHandle(): ?string
{
return 'block';
}
/**
* @inheritdoc
*/
public static function hasDrafts(): bool
{
return true;
}
/**
* @inheritdoc
*/
public static function trackChanges(): bool
{
return true;
}
/**
* @inheritdoc
*/
public static function isLocalized(): bool
{
return true;
}
/**
* @inheritdoc
* @return ContentBlockQuery The newly created [[ContentBlockQuery]] instance.
*/
public static function find(): ContentBlockQuery
{
return new ContentBlockQuery(static::class);
}
/**
* @inheritdoc
*/
protected static function defineFieldLayouts(?string $source): array
{
/** @var ContentBlockField[] $fields */
$fields = Craft::$app->getFields()->getFieldsByType(ContentBlockField::class);
return array_map(fn(ContentBlockField $field) => $field->getFieldLayout(), $fields);
}
/**
* Returns the GraphQL type name that content block elements should use, based on their Content Block field.
*/
public static function gqlTypeName(ContentBlockField $field): string
{
return sprintf('%s_ContentBlock', $field->layoutElement?->getOriginalHandle() ?? $field->handle);
}
/**
* @inheritdoc
*/
public static function baseGqlType(): Type
{
return ContentBlockInterface::getType();
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
return [
...parent::defineRules(),
[['fieldId', 'ownerId', 'primaryOwnerId', 'sortOrder'], 'number', 'integerOnly' => true],
];
}
/**
* @inheritdoc
*/
public function getSupportedSites(): array
{
return $this->getField()->getSupportedSitesForElement($this);
}
/**
* @inheritdoc
*/
protected function cacheTags(): array
{
return [
"field:$this->fieldId",
"owner:$this->ownerId",
];
}
/**
* @inheritdoc
*/
public function getUiLabel(): string
{
return '';
}
/**
* @inheritdoc
*/
protected function uiLabel(): ?string
{
return null;
}
/**
* @inheritdoc
*/
public function getFieldLayout(): ?FieldLayout
{
return $this->getField()->getFieldLayout();
}
/**
* @inheritdoc
*/
public function getGqlTypeName(): string
{
return self::gqlTypeName($this->getField());
}
// Events
// -------------------------------------------------------------------------
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function afterSave(bool $isNew): void
{
if (!$this->propagating) {
// Get the content block record
if (!$isNew) {
$record = ContentBlockRecord::findOne($this->id);
if (!$record) {
throw new InvalidConfigException("Invalid content block ID: $this->id");
}
} else {
$record = new ContentBlockRecord();
$record->id = (int)$this->id;
}
$record->fieldId = $this->fieldId;
$record->primaryOwnerId = $this->getPrimaryOwnerId();
// Capture the dirty attributes from the record
$dirtyAttributes = array_keys($record->getDirtyAttributes());
$record->save(false);
$this->setDirtyAttributes($dirtyAttributes);
$this->saveOwnership($isNew, Table::CONTENTBLOCKS);
}
parent::afterSave($isNew);
}
}