-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
ContentElementWrappingService.php
134 lines (112 loc) · 3.89 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
<?php
/*
* 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.
*/
declare(strict_types=1);
namespace Neos\Neos\Service;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Flow\Session\SessionInterface;
use Neos\Fusion\Service\HtmlAugmenter as FusionHtmlAugmenter;
use Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\Neos\Ui\Domain\Service\UserLocaleService;
use Neos\Neos\Ui\Fusion\Helper\NodeInfoHelper;
/**
* 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 FusionHtmlAugmenter
*/
protected $htmlAugmenter;
/**
* @Flow\Inject
* @var SessionInterface
*/
protected $session;
/**
* @Flow\Inject
* @var UserLocaleService
*/
protected $userLocaleService;
/**
* @Flow\Inject
* @var NodeInfoHelper
*/
protected $nodeInfoHelper;
/**
* @Flow\Inject
* @var ContentRepositoryRegistry
*/
protected $contentRepositoryRegistry;
/**
* Wrap the $content identified by $node with the needed markup for the backend.
*
* @param array<string,string> $additionalAttributes
*/
public function wrapContentObject(
Node $node,
string $content,
string $fusionPath,
array $additionalAttributes = []
): ?string {
$contentRepository = $this->contentRepositoryRegistry->get(
$node->contentRepositoryId
);
// TODO: reenable permissions
//if ($this->nodeAuthorizationService->isGrantedToEditNode($node) === false) {
// return $content;
//}
$nodeAddress = NodeAddressFactory::create($contentRepository)->createFromNode($node);
$attributes = $additionalAttributes;
$attributes['data-__neos-fusion-path'] = $fusionPath;
$attributes['data-__neos-node-contextpath'] = $nodeAddress->serializeForUri();
$this->userLocaleService->switchToUILocale();
// TODO illegal dependency on ui
$serializedNode = json_encode($this->nodeInfoHelper->renderNodeWithPropertiesAndChildrenInformation($node));
$this->userLocaleService->switchToUILocale(true);
$wrappedContent = $this->htmlAugmenter->addAttributes($content, $attributes, 'div');
$nodeContextPath = $nodeAddress->serializeForUri();
/** @codingStandardsIgnoreStart */
$wrappedContent .= "<script data-neos-nodedata>(function(){(this['@Neos.Neos.Ui:Nodes'] = this['@Neos.Neos.Ui:Nodes'] || {})['{$nodeContextPath}'] = {$serializedNode}})()</script>";
/** @codingStandardsIgnoreEnd */
return $wrappedContent;
}
/**
* Add required CSS classes to the attributes.
*
* @param array<string,mixed> $attributes
* @param array<string,mixed> $initialClasses
* @return array<string,mixed>
*/
protected function addCssClasses(array $attributes, Node $node, array $initialClasses = []): array
{
$classNames = $initialClasses;
if (!$node->dimensionSpacePoint->equals($node->originDimensionSpacePoint)) {
$classNames[] = 'neos-contentelement-shine-through';
}
if ($classNames !== []) {
$attributes['class'] = implode(' ', $classNames);
}
return $attributes;
}
}