1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Models; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use craft\base\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Schematic Matrix Block Types Converter. |
10
|
|
|
* |
11
|
|
|
* Sync Craft Setups. |
12
|
|
|
* |
13
|
|
|
* @author Nerds & Company |
14
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
15
|
|
|
* @license MIT |
16
|
|
|
* |
17
|
|
|
* @see http://www.nerds.company |
18
|
|
|
*/ |
19
|
|
|
class MatrixBlockType extends Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
1 |
|
public function getRecordDefinition(Model $record): array |
25
|
|
|
{ |
26
|
1 |
|
$definition = parent::getRecordDefinition($record); |
27
|
|
|
|
28
|
1 |
|
unset($definition['attributes']['fieldId']); |
29
|
1 |
|
unset($definition['attributes']['hasFieldErrors']); |
30
|
|
|
|
31
|
1 |
|
$definition['fields'] = Craft::$app->controller->module->modelMapper->export($record->fieldLayout->getFields()); |
32
|
|
|
|
33
|
1 |
|
return $definition; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function saveRecord(Model $record, array $definition): bool |
40
|
|
|
{ |
41
|
|
|
// Set the content table for this matrix block |
42
|
1 |
|
$originalContentTable = Craft::$app->content->contentTable; |
43
|
|
|
$matrixField = Craft::$app->fields->getFieldById($record->fieldId); |
44
|
|
|
$contentTable = Craft::$app->matrix->getContentTableName($matrixField); |
45
|
|
|
Craft::$app->content->contentTable = $contentTable; |
46
|
|
|
|
47
|
|
|
// Get the matrix block fields from the definition |
48
|
|
|
$modelMapper = Craft::$app->controller->module->modelMapper; |
49
|
|
|
$fields = $modelMapper->import($definition['fields'], $record->getFields(), [], false); |
|
|
|
|
50
|
|
|
$record->setFields($fields); |
|
|
|
|
51
|
|
|
|
52
|
|
|
// Save the matrix block |
53
|
|
|
$result = Craft::$app->matrix->saveBlockType($record, false); |
54
|
|
|
|
55
|
|
|
// Restore the content table to what it was before |
56
|
|
|
Craft::$app->content->contentTable = $originalContentTable; |
57
|
|
|
|
58
|
|
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function deleteRecord(Model $record): bool |
65
|
|
|
{ |
66
|
1 |
|
return Craft::$app->matrix->deleteBlockType($record); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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.