1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Models; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use craft\base\Model; |
7
|
|
|
use craft\db\Query; |
8
|
|
|
use craft\models\MatrixBlockType as MatrixBlockTypeModel; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Schematic Matrix Block Types Converter. |
12
|
|
|
* |
13
|
|
|
* Sync Craft Setups. |
14
|
|
|
* |
15
|
|
|
* @author Nerds & Company |
16
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* @see http://www.nerds.company |
20
|
|
|
*/ |
21
|
|
|
class MatrixBlockType extends Base |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
1 |
|
public function getRecordDefinition(Model $record): array |
27
|
|
|
{ |
28
|
1 |
|
$definition = parent::getRecordDefinition($record); |
29
|
|
|
|
30
|
1 |
|
unset($definition['attributes']['fieldId']); |
31
|
1 |
|
unset($definition['attributes']['hasFieldErrors']); |
32
|
|
|
|
33
|
1 |
|
$definition['fields'] = Craft::$app->controller->module->modelMapper->export($record->fieldLayout->getFields()); |
34
|
|
|
|
35
|
1 |
|
return $definition; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public function saveRecord(Model $record, array $definition): bool |
42
|
|
|
{ |
43
|
|
|
// Get existing fields by block type handle |
44
|
1 |
|
$existingBlockType = $this->getBlockTypeByHandle($definition['attributes']['handle']); |
45
|
|
|
$existingFields = $existingBlockType ? $existingBlockType->getFields() : []; |
|
|
|
|
46
|
|
|
|
47
|
|
|
// Set the content table for this matrix block |
48
|
|
|
$originalContentTable = Craft::$app->content->contentTable; |
49
|
|
|
$matrixField = Craft::$app->fields->getFieldById($record->fieldId); |
50
|
|
|
$contentTable = Craft::$app->matrix->getContentTableName($matrixField); |
51
|
|
|
Craft::$app->content->contentTable = $contentTable; |
52
|
|
|
|
53
|
|
|
// Get the matrix block fields from the definition |
54
|
|
|
$modelMapper = Craft::$app->controller->module->modelMapper; |
55
|
|
|
$fields = $modelMapper->import($definition['fields'], $existingFields, [], false); |
56
|
|
|
$record->setFields($fields); |
|
|
|
|
57
|
|
|
|
58
|
|
|
// Save the matrix block |
59
|
|
|
$result = Craft::$app->matrix->saveBlockType($record, false); |
60
|
|
|
|
61
|
|
|
// Restore the content table to what it was before |
62
|
|
|
Craft::$app->content->contentTable = $originalContentTable; |
63
|
|
|
|
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
1 |
|
public function deleteRecord(Model $record): bool |
71
|
|
|
{ |
72
|
1 |
|
return Craft::$app->matrix->deleteBlockType($record); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get a Matrix Block Type Model by its handle. |
77
|
|
|
* |
78
|
|
|
* @param string $handle |
79
|
|
|
* |
80
|
|
|
* @return MatrixBlockTypeModel |
81
|
|
|
*/ |
82
|
1 |
|
private function getBlockTypeByHandle(string $handle): MatrixBlockTypeModel |
83
|
|
|
{ |
84
|
1 |
|
$result = (new Query()) |
85
|
1 |
|
->select([ |
86
|
1 |
|
'id', |
87
|
|
|
]) |
88
|
1 |
|
->from(['{{%matrixblocktypes}}']) |
89
|
1 |
|
->where(['handle' => $handle]) |
90
|
1 |
|
->one(); |
91
|
|
|
|
92
|
|
|
return new MatrixBlockTypeModel($result); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.