Skip to content

Commit

Permalink
Translator switch
Browse files Browse the repository at this point in the history
Option to turn off automatic translation of select values
  • Loading branch information
Jan Zálešák committed Nov 16, 2021
1 parent b6a8d5e commit 0966649
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 15 additions & 2 deletions src/Components/AjaxSelect/DI/AjaxSelectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ 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() {
$this->config = $this->config + [
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();
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 0966649

Please sign in to comment.