1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Fields; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use craft\base\Model; |
7
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
8
|
|
|
use NerdsAndCompany\Schematic\Converters\Base\Field; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Schematic Matrix Converter. |
12
|
|
|
* |
13
|
|
|
* Sync Craft Setups. |
14
|
|
|
* |
15
|
|
|
* @author Nerds & Company |
16
|
|
|
* @copyright Copyright (c) 2015-2019, Nerds & Company |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* @see http://www.nerds.company |
20
|
|
|
*/ |
21
|
|
|
class Matrix extends Field |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
2 |
|
public function getRecordDefinition(Model $record): array |
27
|
|
|
{ |
28
|
2 |
|
$definition = parent::getRecordDefinition($record); |
29
|
2 |
|
$definition['blockTypes'] = Craft::$app->controller->module->modelMapper->export($record->getBlockTypes()); |
30
|
|
|
|
31
|
2 |
|
return $definition; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
2 |
|
public function saveRecord(Model $record, array $definition): bool |
38
|
|
|
{ |
39
|
2 |
|
if (parent::saveRecord($record, $definition)) { |
40
|
1 |
|
if (array_key_exists('blockTypes', $definition)) { |
41
|
1 |
|
$this->resetCraftMatrixServiceBlockTypesCache(); |
42
|
1 |
|
$this->resetCraftMatrixFieldBlockTypesCache($record); |
43
|
|
|
|
44
|
1 |
|
Craft::$app->controller->module->modelMapper->import( |
45
|
1 |
|
$definition['blockTypes'], |
46
|
1 |
|
$record->getBlockTypes(), |
47
|
1 |
|
['fieldId' => $record->id] |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Reset craft matrix service block types cache using reflection. |
59
|
|
|
*/ |
60
|
1 |
|
private function resetCraftMatrixServiceBlockTypesCache() |
61
|
|
|
{ |
62
|
1 |
|
$obj = Craft::$app->matrix; |
63
|
1 |
|
$refObject = new \ReflectionObject($obj); |
64
|
1 |
|
if ($refObject->hasProperty('_fetchedAllBlockTypesForFieldId')) { |
65
|
|
|
$refProperty1 = $refObject->getProperty('_fetchedAllBlockTypesForFieldId'); |
66
|
|
|
$refProperty1->setAccessible(true); |
67
|
|
|
$refProperty1->setValue($obj, false); |
68
|
|
|
} |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Reset craft matrix field block types cache using reflection. |
73
|
|
|
* |
74
|
|
|
* @param Model $record |
75
|
|
|
*/ |
76
|
1 |
|
private function resetCraftMatrixFieldBlockTypesCache(Model $record) |
77
|
|
|
{ |
78
|
1 |
|
$obj = $record; |
79
|
1 |
|
$refObject = new \ReflectionObject($obj); |
80
|
1 |
|
if ($refObject->hasProperty('_blockTypes')) { |
81
|
|
|
$refProperty1 = $refObject->getProperty('_blockTypes'); |
82
|
|
|
$refProperty1->setAccessible(true); |
83
|
|
|
$refProperty1->setValue($obj, null); |
84
|
|
|
} |
85
|
1 |
|
} |
86
|
|
|
} |
87
|
|
|
|