[TASK] Move some functions to traits for reuse

This commit is contained in:
Philipp Dieter
2021-11-05 13:14:54 +01:00
parent f206c8fb0e
commit f427ab7cd6
4 changed files with 182 additions and 64 deletions

View File

@@ -16,6 +16,7 @@ use \Opis\JsonSchema\{
Validator, ValidationResult, ValidationError, Schema
};
use Cjel\TemplatesAide\Utility\ArrayUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* ValidationTrait
@@ -96,4 +97,64 @@ trait ValidationTrait
return $validationResult;
}
/**
* function to add validation error manually in the controller
*/
protected function addValidationError(
$field, $keyword, $overwrite = false
) {
$this->isValid = false;
$this->responseStatus = [400 => 'validationError'];
if (!array_key_exists($field, $this->errors)
|| $overwrite == true
) {
$this->errors[$field] = [
'keyword' => $keyword,
];
$this->errorLabels[$field] = $this->getErrorLabel(
$field,
$keyword
);
}
}
/**
* gets error label based on field and keyword, uses predefined extensionkey
*/
protected function getErrorLabel($field, $keyword) {
$path = 'error.' . $field . '.' . $keyword;
$errorLabel = $this->getTranslation($path);
if ($errorLabel == null) {
return $path;
}
return $errorLabel;
}
/**
* shortcut to get translation
*
* @return void
*/
protected function getTranslation($key, $arguments = null)
{
$translation = LocalizationUtility::translate(
$key,
$this->getExtensionKey(),
$arguments
);
if ($translation) {
return $translation;
}
$translation = LocalizationUtility::translate(
$key,
'site_templates',
$arguments
);
if ($translation) {
return $translation;
}
return null;
}
}