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-2018, 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 |
|
$this->resetCraftMatrixServiceBlockTypesCache(); |
40
|
|
|
|
41
|
2 |
|
if (parent::saveRecord($record, $definition)) { |
42
|
1 |
|
if (array_key_exists('blockTypes', $definition)) { |
43
|
1 |
|
Craft::$app->controller->module->modelMapper->import( |
44
|
1 |
|
$definition['blockTypes'], |
45
|
1 |
|
$record->getBlockTypes(), |
46
|
1 |
|
['fieldId' => $record->id] |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Reset craft matrix service block types cache using reflection. |
58
|
|
|
*/ |
59
|
2 |
|
private function resetCraftMatrixServiceBlockTypesCache() |
60
|
|
|
{ |
61
|
2 |
|
$obj = Craft::$app->matrix; |
62
|
2 |
|
$refObject = new \ReflectionObject($obj); |
63
|
2 |
|
if ($refObject->hasProperty('_fetchedAllBlockTypesForFieldId')) { |
64
|
|
|
$refProperty1 = $refObject->getProperty('_fetchedAllBlockTypesForFieldId'); |
65
|
|
|
$refProperty1->setAccessible(true); |
66
|
|
|
$refProperty1->setValue($obj, false); |
67
|
|
|
} |
68
|
2 |
|
} |
69
|
|
|
} |
70
|
|
|
|