Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: set unique form element identifier after node copy #72

Merged
merged 4 commits into from
Jun 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions Classes/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
class Package extends BasePackage
{
private const NODE_TYPE_IDENTIFIER_MIXIN = 'Neos.Form.Builder:IdentifierMixin';

/**
* @param Bootstrap $bootstrap The current bootstrap
* @return void
Expand All @@ -20,24 +22,53 @@ public function boot(Bootstrap $bootstrap)
{
$dispatcher = $bootstrap->getSignalSlotDispatcher();

$dispatcher->connect(Node::class, 'nodePropertyChanged', function (NodeInterface $node, $propertyName, $_, $newValue) use ($bootstrap) {
if ($propertyName !== 'identifier' || empty($newValue) || !$node->getNodeType()->isOfType('Neos.Form.Builder:IdentifierMixin')) {
$dispatcher->connect(Node::class, 'nodePropertyChanged', function (NodeInterface $node, $propertyName, $_, $newValue) {
if ($propertyName !== 'identifier' || empty($newValue) || !$node->getNodeType()->isOfType(self::NODE_TYPE_IDENTIFIER_MIXIN)) {
return;
}

/** @noinspection PhpUndefinedMethodInspection */
$flowQuery = (new FlowQuery([$node]))->context(['invisibleContentShown' => true, 'removedContentShown' => true, 'inaccessibleContentShown' => true]);
$possibleIdentifier = $initialIdentifier = $newValue;
$i = 1;
/** @noinspection PhpUndefinedMethodInspection */
while ($flowQuery
->closest('[instanceof Neos.Form.Builder:NodeBasedForm]')
// [identifier=".."] matches the Form Element identifier, [_identiier!="..."] excludes the current node
->find(sprintf('[instanceof Neos.Form.Builder:IdentifierMixin][identifier="%s"][_identifier!="%s"]', $possibleIdentifier, $node->getIdentifier()))
->count() > 0) {
$possibleIdentifier = $initialIdentifier . '-' . $i++;
$this->setUniqueFormElementIdentifier($node, $newValue);
});

$dispatcher->connect(Node::class, 'nodeAdded', function (NodeInterface $node) {
try {
$identifier = $node->getProperty('identifier');

if (empty($identifier) || !$node->getNodeType()->isOfType(self::NODE_TYPE_IDENTIFIER_MIXIN)) {
return;
}
} catch (\Neos\ContentRepository\Exception\NodeException $e) {
return;
}
$node->setProperty('identifier', $possibleIdentifier);

$this->setUniqueFormElementIdentifier($node, $identifier);
});
}

/**
* @param NodeInterface $node
* @param string $identifier
* @throws \Neos\Eel\Exception
*/
private function setUniqueFormElementIdentifier(NodeInterface $node, string $identifier): void
{
/** @noinspection PhpUndefinedMethodInspection */
$flowQuery = (new FlowQuery([$node]))->context([
'invisibleContentShown' => true,
'removedContentShown' => true,
'inaccessibleContentShown' => true
]);
$possibleIdentifier = $identifier;
$i = 1;
/** @noinspection PhpUndefinedMethodInspection */
while ($flowQuery
->closest('[instanceof Neos.Form.Builder:NodeBasedForm]')
// [identifier=".."] matches the Form Element identifier, [_identiier!="..."] excludes the current node
->find(sprintf('[instanceof %s][identifier="%s"][_identifier!="%s"]',
self::NODE_TYPE_IDENTIFIER_MIXIN ,$possibleIdentifier, $node->getIdentifier()))
->count() > 0) {
$possibleIdentifier = $identifier . '-' . $i++;
}
$node->setProperty('identifier', $possibleIdentifier);
}
}