-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
AddDimensions.php
99 lines (87 loc) · 3.07 KB
/
AddDimensions.php
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
<?php
namespace Neos\ContentRepository\Migration\Transformations;
/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\ContentRepository\Domain\Model\NodeData;
use Neos\ContentRepository\Domain\Model\NodeDimension;
use Neos\ContentRepository\Domain\Repository\ContentDimensionRepository;
/**
* Add dimensions on a node. This adds to the existing dimensions, if you need to
* overwrite existing dimensions, @see SetDimensions
*/
class AddDimensions extends AbstractTransformation
{
/**
* @Flow\Inject
* @var ContentDimensionRepository
*/
protected $contentDimensionRepository;
/**
* If you omit a configured dimension this transformation will add the default value for that dimension.
*
* @var array
*/
protected $dimensionValues = [];
/**
* Adds the default dimension values for all dimensions that were not given.
*
* @var boolean
*/
protected $addDefaultDimensionValues = true;
/**
* @param array $dimensionValues
*/
public function setDimensionValues($dimensionValues)
{
$this->dimensionValues = $dimensionValues;
}
/**
* @param boolean $addDefaultDimensionValues
*/
public function setAddDefaultDimensionValues($addDefaultDimensionValues)
{
$this->addDefaultDimensionValues = $addDefaultDimensionValues;
}
/**
* Add dimensions to the node.
*
* @param NodeData $node
* @return void
*/
public function execute(NodeData $node)
{
$dimensionValuesToBeAdded = $node->getDimensionValues();
foreach ($this->dimensionValues as $dimensionName => $dimensionValues) {
if (!isset($dimensionValuesToBeAdded[$dimensionName])) {
if (is_array($dimensionValues)) {
$dimensionValuesToBeAdded[$dimensionName] = $dimensionValues;
} else {
$dimensionValuesToBeAdded[$dimensionName] = [$dimensionValues];
}
}
}
if ($this->addDefaultDimensionValues === true) {
$configuredDimensions = $this->contentDimensionRepository->findAll();
foreach ($configuredDimensions as $configuredDimension) {
if (!isset($dimensionValuesToBeAdded[$configuredDimension->getIdentifier()])) {
$dimensionValuesToBeAdded[$configuredDimension->getIdentifier()] = [$configuredDimension->getDefault()];
}
}
}
$dimensionsToBeSet = [];
foreach ($dimensionValuesToBeAdded as $dimensionName => $dimensionValues) {
foreach ($dimensionValues as $dimensionValue) {
$dimensionsToBeSet[] = new NodeDimension($node, $dimensionName, $dimensionValue);
}
}
$node->setDimensions($dimensionsToBeSet);
}
}