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

Add default values in eval key #39

Merged
merged 3 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,32 @@ $GLOBALS['TL_DCA']['tl_theme']['fields']['anything'] = [
?>
```

From version 3.6.10:

Use `default` in `eval` to set multiple default values and rows.

```php
<?php
$GLOBALS['TL_DCA']['tl_theme']['fields']['templateSelection'] = [
'inputType' => 'multiColumnWizard',
'eval' => [
'default' => [
['ts_client_os' => 'option1', 'ts_client_browser' => 'FF'],
['ts_client_os' => 'option2', 'ts_client_browser' => 'Chrome'],
],
'columnFields' => [
'ts_client_os' => [
//...
],
'ts_client_browser' => [
//...
],
],
],
'sql' => 'blob NULL',
];
?>
```

The descriptions of the columns are displayed in the column header as a tooltip next to the icon `🛈` - the
character or text can be changed in the language file.
18 changes: 18 additions & 0 deletions src/Contao/Widgets/MultiColumnWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ class MultiColumnWizard extends Widget

private ?TranslatorInterface $translator = null;

/**
* @var array|mixed|string|string[]|null
*/
private mixed $defaultValue;

/**
* Initialize the object
*
Expand Down Expand Up @@ -257,6 +262,10 @@ public function __set($strKey, $varValue)
}
break;

case 'default':
$this->defaultValue = StringUtil::deserialize($varValue, true);
break;

case 'mandatory':
$this->arrConfiguration['mandatory'] = $varValue ? true : false;
break;
Expand Down Expand Up @@ -804,6 +813,10 @@ public function generate($overwriteRowCurrentRow = null, $onlyRows = false)
}

$intNumberOfRows = max(count($this->varValue), 1);
$useDefaultValue = (0 == count($this->varValue));
if ($useDefaultValue && !empty($this->defaultValue)) {
$intNumberOfRows = count($this->defaultValue);
}

// always show the minimum number of rows if set
if ($this->minCount && ($intNumberOfRows < $this->minCount)) {
Expand Down Expand Up @@ -839,6 +852,11 @@ public function generate($overwriteRowCurrentRow = null, $onlyRows = false)
$arrField = array_merge($arrField, $this->arrRowSpecificData[$i][$strKey]);
}

// Should we do this above the foreach and run a foreach twice ... nope.
if ($useDefaultValue && !empty($this->defaultValue[$i][$strKey] ?? null)) {
$this->varValue[$i][$strKey] = $this->defaultValue[$i][$strKey];
}

$objWidget = $this->initializeWidget(
$arrField,
$i,
Expand Down
Loading