-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
ContentElementWrappingService.php
330 lines (288 loc) · 12.1 KB
/
ContentElementWrappingService.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
namespace Neos\Neos\Service;
/*
* This file is part of the Neos.Neos 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\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Neos\Domain\Service\ContentContext;
use Neos\Neos\Service\Mapping\NodePropertyConverterService;
use Neos\ContentRepository\Domain\Model\Node;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Service\AuthorizationService;
use Neos\Fusion\Service\HtmlAugmenter as FusionHtmlAugmenter;
/**
* The content element wrapping service adds the necessary markup around
* a content element such that it can be edited using the Content Module
* of the Neos Backend.
*
* @Flow\Scope("singleton")
*/
class ContentElementWrappingService
{
/**
* @Flow\Inject
* @var PrivilegeManagerInterface
*/
protected $privilegeManager;
/**
* @Flow\Inject
* @var AuthorizationService
*/
protected $nodeAuthorizationService;
/**
* @Flow\Inject
* @var FusionHtmlAugmenter
*/
protected $htmlAugmenter;
/**
* @Flow\Inject
* @var NodePropertyConverterService
*/
protected $nodePropertyConverterService;
/**
* Wrap the $content identified by $node with the needed markup for the backend.
*
* @param NodeInterface $node
* @param string $content
* @param string $fusionPath
* @param array $additionalAttributes additional attributes in the form ['<attribute-name>' => '<attibute-value>', ...] to be rendered in the element wrapping
* @return string
*/
public function wrapContentObject(NodeInterface $node, $content, $fusionPath, array $additionalAttributes = [])
{
if ($this->needsMetadata($node, false) === false) {
return $content;
}
$attributes = $additionalAttributes;
$attributes['data-node-__typoscript-path'] = $fusionPath; // @deprecated
$attributes['data-node-__fusion-path'] = $fusionPath;
$attributes['tabindex'] = 0;
$attributes = $this->addGenericEditingMetadata($attributes, $node);
$attributes = $this->addNodePropertyAttributes($attributes, $node);
$attributes = $this->addCssClasses($attributes, $node, $this->collectEditingClassNames($node));
return $this->htmlAugmenter->addAttributes($content, $attributes, 'div', ['typeof']);
}
/**
* @param NodeInterface $node
* @param string $content
* @param string $fusionPath
* @param array $additionalAttributes additional attributes in the form ['<attribute-name>' => '<attibute-value>', ...] to be rendered in the element wrapping
* @return string
*/
public function wrapCurrentDocumentMetadata(NodeInterface $node, $content, $fusionPath, array $additionalAttributes = [])
{
if ($this->needsMetadata($node, true) === false) {
return $content;
}
$attributes = $additionalAttributes;
$attributes['data-node-__typoscript-path'] = $fusionPath; // @deprecated
$attributes['data-node-__fusion-path'] = $fusionPath;
$attributes = $this->addGenericEditingMetadata($attributes, $node);
$attributes = $this->addNodePropertyAttributes($attributes, $node);
$attributes = $this->addDocumentMetadata($attributes, $node);
$attributes = $this->addCssClasses($attributes, $node, []);
return $this->htmlAugmenter->addAttributes($content, $attributes, 'div', ['typeof']);
}
/**
* Adds node properties to the given $attributes collection and returns the extended array
*
* @param array $attributes
* @param NodeInterface $node
* @return array the merged attributes
*/
protected function addNodePropertyAttributes(array $attributes, NodeInterface $node)
{
foreach (array_keys($node->getNodeType()->getProperties()) as $propertyName) {
if ($propertyName[0] === '_' && $propertyName[1] === '_') {
// skip fully-private properties
continue;
}
$attributes = array_merge($attributes, $this->renderNodePropertyAttribute($node, $propertyName));
}
return $attributes;
}
/**
* Renders data attributes needed for the given node property.
*
* @param NodeInterface $node
* @param string $propertyName
* @return array
*/
protected function renderNodePropertyAttribute(NodeInterface $node, $propertyName)
{
$attributes = [];
/** @var $contentContext ContentContext */
$contentContext = $node->getContext();
// skip the node name of the site node - TODO: Why do we need this?
if ($propertyName === '_name' && $node === $contentContext->getCurrentSiteNode()) {
return $attributes;
}
$dataType = $node->getNodeType()->getPropertyType($propertyName);
$dasherizedPropertyName = $this->dasherize($propertyName);
$propertyValue = $this->nodePropertyConverterService->getProperty($node, $propertyName);
$propertyValue = $propertyValue === null ? '' : $propertyValue;
$propertyValue = !is_string($propertyValue) ? json_encode($propertyValue) : $propertyValue;
if ($dataType !== 'string') {
$attributes['data-nodedatatype-' . $dasherizedPropertyName] = 'xsd:' . $dataType;
}
$attributes['data-node-' . $dasherizedPropertyName] = $propertyValue;
return $attributes;
}
/**
* Add required CSS classes to the attributes.
*
* @param array $attributes
* @param NodeInterface $node
* @param array $initialClasses
* @return array
*/
protected function addCssClasses(array $attributes, NodeInterface $node, array $initialClasses = [])
{
$classNames = $initialClasses;
// FIXME: The `dimensionsAreMatchingTargetDimensionValues` method should become part of the NodeInterface if it is used here .
if ($node instanceof Node && !$node->dimensionsAreMatchingTargetDimensionValues()) {
$classNames[] = 'neos-contentelement-shine-through';
}
if ($classNames !== []) {
$attributes['class'] = implode(' ', $classNames);
}
return $attributes;
}
/**
* Collects metadata for the Neos backend specifically for document nodes.
*
* @param array $attributes
* @param NodeInterface $node
* @return array
*/
protected function addDocumentMetadata(array $attributes, NodeInterface $node)
{
/** @var ContentContext $contentContext */
$contentContext = $node->getContext();
$attributes['data-neos-site-name'] = $contentContext->getCurrentSite()->getName();
$attributes['data-neos-site-node-context-path'] = $contentContext->getCurrentSiteNode()->getContextPath();
// Add the workspace of the content repository context to the attributes
$attributes['data-neos-context-workspace-name'] = $contentContext->getWorkspaceName();
$attributes['data-neos-context-dimensions'] = json_encode($contentContext->getDimensions());
if (!$this->nodeAuthorizationService->isGrantedToEditNode($node)) {
$attributes['data-node-__read-only'] = 'true';
$attributes['data-nodedatatype-__read-only'] = 'boolean';
}
return $attributes;
}
/**
* Collects metadata attributes used to allow editing of the node in the Neos backend.
*
* @param array $attributes
* @param NodeInterface $node
* @return array
*/
protected function addGenericEditingMetadata(array $attributes, NodeInterface $node)
{
$attributes['typeof'] = 'typo3:' . $node->getNodeType()->getName();
$attributes['about'] = $node->getContextPath();
$attributes['data-node-_identifier'] = $node->getIdentifier();
$attributes['data-node-__workspace-name'] = $node->getWorkspace()->getName();
$attributes['data-node-__label'] = $node->getLabel();
if ($node->getNodeType()->isOfType('Neos.Neos:ContentCollection')) {
$attributes['rel'] = 'typo3:content-collection';
}
// these properties are needed together with the current NodeType to evaluate Node Type Constraints
// TODO: this can probably be greatly cleaned up once we do not use CreateJS or VIE anymore.
if ($node->getParent()) {
$attributes['data-node-__parent-node-type'] = $node->getParent()->getNodeType()->getName();
}
if ($node->isAutoCreated()) {
$attributes['data-node-_name'] = $node->getName();
$attributes['data-node-_is-autocreated'] = 'true';
}
if ($node->getParent() && $node->getParent()->isAutoCreated()) {
$attributes['data-node-_parent-is-autocreated'] = 'true';
// we shall only add these properties if the parent is actually auto-created; as the Node-Type-Switcher in the UI relies on that.
$attributes['data-node-__parent-node-name'] = $node->getParent()->getName();
$attributes['data-node-__grandparent-node-type'] = $node->getParent()->getParent()->getNodeType()->getName();
}
return $attributes;
}
/**
* Collects CSS class names used for styling editable elements in the Neos backend.
*
* @param NodeInterface $node
* @return array
*/
protected function collectEditingClassNames(NodeInterface $node)
{
$classNames = [];
if ($node->getNodeType()->isOfType('Neos.Neos:ContentCollection')) {
// This is needed since the backend relies on this class (should not be necessary)
$classNames[] = 'neos-contentcollection';
} else {
$classNames[] = 'neos-contentelement';
}
if ($node->isRemoved()) {
$classNames[] = 'neos-contentelement-removed';
}
if ($node->isHidden()) {
$classNames[] = 'neos-contentelement-hidden';
}
if ($this->isInlineEditable($node) === false) {
$classNames[] = 'neos-not-inline-editable';
}
return $classNames;
}
/**
* Determine if the Node or one of it's properties is inline editable.
$parsedType = TypeHandling::parseType($dataType);
*
* @param NodeInterface $node
* @return boolean
*/
protected function isInlineEditable(NodeInterface $node)
{
$uiConfiguration = $node->getNodeType()->hasConfiguration('ui') ? $node->getNodeType()->getConfiguration('ui') : [];
return (
(isset($uiConfiguration['inlineEditable']) && $uiConfiguration['inlineEditable'] === true) ||
$this->hasInlineEditableProperties($node)
);
}
/**
* Checks if the given Node has any properties configured as 'inlineEditable'
*
* @param NodeInterface $node
* @return boolean
*/
protected function hasInlineEditableProperties(NodeInterface $node)
{
return array_reduce(array_values($node->getNodeType()->getProperties()), function ($hasInlineEditableProperties, $propertyConfiguration) {
return ($hasInlineEditableProperties || (isset($propertyConfiguration['ui']['inlineEditable']) && $propertyConfiguration['ui']['inlineEditable'] === true));
}, false);
}
/**
* @param NodeInterface $node
* @param boolean $renderCurrentDocumentMetadata
* @return boolean
*/
protected function needsMetadata(NodeInterface $node, $renderCurrentDocumentMetadata)
{
/** @var $contentContext ContentContext */
$contentContext = $node->getContext();
return ($contentContext->isInBackend() === true && ($renderCurrentDocumentMetadata === true || $this->nodeAuthorizationService->isGrantedToEditNode($node) === true));
}
/**
* Converts camelCased strings to lower cased and non-camel-cased strings
*
* @param string $value
* @return string
*/
protected function dasherize($value)
{
return strtolower(trim(preg_replace('/[A-Z]/', '-$0', $value), '-'));
}
}