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

EZP-28226: User can't add more fields after removing several of them #187

Merged
merged 1 commit into from
Dec 13, 2017
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
30 changes: 29 additions & 1 deletion lib/Form/Processor/ContentTypeFormProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace EzSystems\RepositoryForms\Form\Processor;

use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
use eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList;
use EzSystems\RepositoryForms\Event\FormActionEvent;
Expand Down Expand Up @@ -102,7 +103,11 @@ public function processAddFieldDefinition(FormActionEvent $event)

$fieldDefCreateStruct = new FieldDefinitionCreateStruct([
'fieldTypeIdentifier' => $fieldTypeIdentifier,
'identifier' => sprintf('new_%s_%d', $fieldTypeIdentifier, count($contentTypeDraft->fieldDefinitions) + 1),
'identifier' => $this->resolveNewFieldDefinitionIdentifier(
$contentTypeDraft,
$maxFieldPos,
$fieldTypeIdentifier
),
'names' => [$event->getOption('languageCode') => 'New FieldDefinition'],
'position' => $maxFieldPos + 1,
]);
Expand Down Expand Up @@ -150,4 +155,27 @@ public function processRemoveContentTypeDraft(FormActionEvent $event)
);
}
}

/**
* Resolves unique field definition identifier.
*
* @param ContentTypeDraft $contentTypeDraft
* @param int $startIndex
* @param string $fieldTypeIdentifier
*
* @return string
*/
private function resolveNewFieldDefinitionIdentifier(
ContentTypeDraft $contentTypeDraft,
int $startIndex,
string $fieldTypeIdentifier
): string {
$fieldDefinitionIdentifiers = array_column($contentTypeDraft->getFieldDefinitions(), 'identifier');

do {
$fieldDefinitionIdentifier = sprintf('new_%s_%d', $fieldTypeIdentifier, ++$startIndex);
} while (in_array($fieldDefinitionIdentifier, $fieldDefinitionIdentifiers, true));

return $fieldDefinitionIdentifier;
}
}