From 0966649ea2867683ed0832fb4fc1e14504a6c2bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Z=C3=A1le=C5=A1=C3=A1k?= Date: Tue, 16 Nov 2021 16:55:10 +0100 Subject: [PATCH] Translator switch Option to turn off automatic translation of select values --- README.md | 3 +++ .../AjaxSelect/DI/AjaxSelectExtension.php | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8a2fe31..9781785 100644 --- a/README.md +++ b/README.md @@ -50,9 +50,12 @@ This extension adds following methods to `Nette\Forms\Container` and thus to all [ AjaxSelectExtension::CONFIG_INVALID_VALUE_MODE => AjaxSelectExtension::INVALID_VALUE_MODE_*, AjaxSelectExtension::CONFIG_OR_BY_ID_FILTER => TRUE, + AjaxSelectExtension::CONFIG_TRANSLATOR => TRUE, ] ``` +`AjaxSelectExtension::CONFIG_TRANSLATOR`: Sets the automatic translation of select values on/off. Default is `TRUE`. + ### Dynamic Select This control allows passing unknown value to `$control->value` field. Doing so will invoke control's `$itemFactory` with only one parameter - the invalid value. diff --git a/src/Components/AjaxSelect/DI/AjaxSelectExtension.php b/src/Components/AjaxSelect/DI/AjaxSelectExtension.php index f2f424e..e625e63 100644 --- a/src/Components/AjaxSelect/DI/AjaxSelectExtension.php +++ b/src/Components/AjaxSelect/DI/AjaxSelectExtension.php @@ -18,6 +18,8 @@ class AjaxSelectExtension extends \Nette\DI\CompilerExtension { const CONFIG_OR_BY_ID_FILTER = 'orByIdFilter'; + const CONFIG_TRANSLATOR = 'translator'; + const ENTITY_FACTORY_TAG = 'ajax-select.entity-factory'; public function loadConfiguration() { @@ -25,6 +27,7 @@ public function loadConfiguration() { static::CONFIG_GET_ITEMS_SIGNAL_NAME => 'getAjaxItems', static::CONFIG_INVALID_VALUE_MODE => static::INVALID_VALUE_MODE_EXCEPTION, static::CONFIG_OR_BY_ID_FILTER => TRUE, + static::CONFIG_TRANSLATOR => TRUE, ]; $builder = $this->getContainerBuilder(); @@ -125,11 +128,16 @@ public static function register(\Nette\DI\Container $container, $globalConfig) { /** @var AjaxSelect\AjaxSelect|AjaxSelect\DynamicSelect|mixed $control */ $control = new $class($label); - $config = static::processConfigOptions($config, $globalConfig, [static::CONFIG_INVALID_VALUE_MODE, static::CONFIG_OR_BY_ID_FILTER]); + $config = static::processConfigOptions($config, $globalConfig, [static::CONFIG_INVALID_VALUE_MODE, static::CONFIG_OR_BY_ID_FILTER, static::CONFIG_TRANSLATOR]); // set invalid value mode $control->setInvalidValueMode($config[static::CONFIG_INVALID_VALUE_MODE]); + // set translator + if (!$config[static::CONFIG_TRANSLATOR]) { + $control->setTranslator(NULL); + } + // inject ajax entity /** @var AjaxSelect\Services\AjaxService $ajaxService */ $ajaxService = $serviceGetter(); @@ -148,7 +156,7 @@ public static function register(\Nette\DI\Container $container, $globalConfig) { // pro dymanic select return function (\Nette\Forms\Container $container, $name, $label = NULL, $items = NULL, $itemFactory = NULL, $config = []) use ($class, $serviceGetter, $globalConfig) { - $config = static::processConfigOptions($config, $globalConfig, [static::CONFIG_INVALID_VALUE_MODE, static::CONFIG_OR_BY_ID_FILTER]); + $config = static::processConfigOptions($config, $globalConfig, [static::CONFIG_INVALID_VALUE_MODE, static::CONFIG_OR_BY_ID_FILTER, static::CONFIG_TRANSLATOR]); // if $items are not array of values, we have received query object if ($items instanceof \ADT\BaseQuery\BaseQuery) { @@ -168,6 +176,11 @@ public static function register(\Nette\DI\Container $container, $globalConfig) { // set invalid value mode $control->setInvalidValueMode($config[static::CONFIG_INVALID_VALUE_MODE]); + // set translator + if (!$config[static::CONFIG_TRANSLATOR]) { + $control->setTranslator(NULL); + } + $control->setItemFactory($itemFactory); return $container[$name] = $control;