Skip to content

Commit

Permalink
EZP-32256: GraphQL field validation throws exception without 'eng-GB'…
Browse files Browse the repository at this point in the history
… language (#93)
  • Loading branch information
ITernovtsii authored Jan 11, 2021
1 parent 9e8022b commit 9531abd
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/GraphQL/Resolver/DomainContentMutationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,29 +270,21 @@ private function getLocationService()
private function renderFieldValidationErrors(RepositoryExceptions\ContentFieldValidationException $e, API\Values\ContentType\ContentType $contentType)
{
$errors = [];
foreach ($e->getFieldErrors() as $fieldDefId => $fieldErrors) {
foreach ($e->getFieldErrors() as $fieldDefId => $fieldErrorByLanguage) {
$fieldDefinition = $contentType->getFieldDefinitions()->filter(
static function (FieldDefinition $fieldDefinition) use ($fieldDefId) {
return $fieldDefinition->id === $fieldDefId;
}
)->first();

foreach ($fieldErrors['eng-GB'] as $fieldError) {
$translatableMessage = $fieldError->getTranslatableMessage();
if ($translatableMessage instanceof API\Values\Translation\Plural) {
if (count($fieldError['values']) === 1) {
$message = $translatableMessage->singular;
} else {
$message = $translatableMessage->plural;
}
} else {
$message = $translatableMessage->message;
}
$errors[] = sprintf("Field '%s' failed validation: %s",
$fieldDefinition->identifier,
$message
);
}
// use error from first available language
$fieldError = reset($fieldErrorByLanguage);

// depending on $fieldError instance, values injected in Plural::__toString or Message::__toString
$errors[] = sprintf("Field '%s' failed validation: %s",
$fieldDefinition->identifier,
(string)$fieldError->getTranslatableMessage()
);
}

return $errors;
Expand Down

0 comments on commit 9531abd

Please sign in to comment.