Skip to content

Commit

Permalink
Merge PR #656 from @zonky2
Browse files Browse the repository at this point in the history
Fix tree picker for language support
  • Loading branch information
stefanheimes committed Nov 30, 2024
2 parents 81f593c + a2797fd commit 65f2b62
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
61 changes: 58 additions & 3 deletions src/Contao/View/Contao2BackendView/TreePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@
use ContaoCommunityAlliance\DcGeneral\Contao\Factory\SessionStorageFactory;
use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\ModelToLabelEvent;
use ContaoCommunityAlliance\DcGeneral\Controller\ControllerInterface;
use ContaoCommunityAlliance\DcGeneral\Controller\ModelCollector;
use ContaoCommunityAlliance\DcGeneral\Controller\RelationshipManager;
use ContaoCommunityAlliance\DcGeneral\Controller\TreeNodeStates;
use ContaoCommunityAlliance\DcGeneral\Data\CollectionInterface;
use ContaoCommunityAlliance\DcGeneral\Data\ConfigInterface;
use ContaoCommunityAlliance\DcGeneral\Data\DataProviderInterface;
use ContaoCommunityAlliance\DcGeneral\Data\DCGE;
use ContaoCommunityAlliance\DcGeneral\Data\LanguageInformationInterface;
use ContaoCommunityAlliance\DcGeneral\Data\ModelId;
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
use ContaoCommunityAlliance\DcGeneral\Data\MultiLanguageDataProviderInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\ContainerInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\BasicDefinitionInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\Definition\View\DefaultModelFormatterConfig;
Expand Down Expand Up @@ -219,6 +222,7 @@ protected function setUp(General $dataContainer = null)
$this->dataContainer = $dataContainer;

$environment = $this->dataContainer->getEnvironment();
assert($environment instanceof EnvironmentInterface);

if (!$this->sourceName) {
$definition = $environment->getDataDefinition();
Expand Down Expand Up @@ -281,6 +285,7 @@ public function updateAjax($ajaxAction, $dataContainer)

$this->setUp($dataContainer);
$environment = $this->dataContainer->getEnvironment();
assert($environment instanceof EnvironmentInterface);

$inputProvider = $environment->getInputProvider();
assert($inputProvider instanceof InputProviderInterface);
Expand Down Expand Up @@ -489,6 +494,49 @@ public function __get($key)
return parent::__get($key);
}

/**
* @param DataProviderInterface $dataDriver
* @param mixed $rootId
*
* @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function setLanguageInProvider(DataProviderInterface $dataDriver, mixed $rootId): void
{
if ($dataDriver instanceof MultiLanguageDataProviderInterface) {
$dataContainer = $this->dataContainer;
assert($dataContainer instanceof General);

$rootEnvironment = $dataContainer->getEnvironment();
assert($rootEnvironment instanceof EnvironmentInterface);

$providerName = $rootEnvironment->getDataDefinition()?->getName();
if (null === $providerName) {
return;
}

$controller = $rootEnvironment->getController();
assert($controller instanceof ControllerInterface);

$sessionStorage = $rootEnvironment->getSessionStorage();
assert($sessionStorage instanceof SessionStorageInterface);

$session = (array) $sessionStorage->get('dc_general');
$currentLanguage = ($session['ml_support'][$providerName] ?? $GLOBALS['TL_LANGUAGE']);
$languages = $controller->getSupportedLanguages($rootId);

if (!\array_key_exists($currentLanguage, $languages)) {
$fallbackLanguage = $dataDriver->getFallbackLanguage($rootId);
assert($fallbackLanguage instanceof LanguageInformationInterface);

$currentLanguage = $fallbackLanguage->getLocale();
}

$dataDriver->setCurrentLanguage($currentLanguage);
}
}

/**
* Skip the field if "change selection" is not checked.
*
Expand Down Expand Up @@ -560,6 +608,7 @@ public function renderItemsPlain()
$config->setSorting([$this->orderField => 'ASC']);
}

$this->setLanguageInProvider($dataDriver, $value);
foreach ($dataDriver->fetchAll($config) as $model) {
if (!($model instanceof ModelInterface)) {
continue;
Expand Down Expand Up @@ -912,7 +961,7 @@ public function generatePopup()
$translator->translate('treePicker', 'dc-general', ['%table%' => $this->sourceName])
)
->set('fieldType', $this->fieldType)
->set('resetSelected', $translator->translate('.resetSelected', 'dc-general'))
->set('resetSelected', $translator->translate('resetSelected', 'dc-general'))
->set('selectAll', $translator->translate('selectAll', 'dc-general'))
->set('values', StringUtil::deserialize($this->varValue, true))
->set('tableName', $this->sourceName);
Expand Down Expand Up @@ -1003,7 +1052,7 @@ public function getSearchSessionKey()
}

/**
* Check if an custom sorting field has been defined.
* Check if a custom sorting field has been defined.
*
* @return bool
*/
Expand Down Expand Up @@ -1053,7 +1102,9 @@ protected function treeWalkModel(ModelInterface $model, $level, $subTables = [])

$this->determineModelState($model, ($level - 1));

$rootId = $model->getId();
$childCollections = [];

foreach ($subTables as $subTable) {
// Evaluate the child filter for this item.
$childFilter = $relationships->getChildCondition($model->getProviderName(), $subTable);
Expand All @@ -1063,9 +1114,10 @@ protected function treeWalkModel(ModelInterface $model, $level, $subTables = [])
continue;
}

// Create a new Config and fetch the children from the child provider.
// Create a new config and fetch the children from the child provider.
$dataProvider = $environment->getDataProvider($subTable);
assert($dataProvider instanceof DataProviderInterface);
$this->setLanguageInProvider($dataProvider, $rootId);

$childConfig = $dataProvider->getEmptyConfig();
$childConfig->setFilter($childFilter->getFilter($model));
Expand Down Expand Up @@ -1147,6 +1199,8 @@ public function getTreeCollectionRecursive($rootId, $level = 0, $providerName =
$dataDriver = $environment->getDataProvider($providerName);
assert($dataDriver instanceof DataProviderInterface);

$this->setLanguageInProvider($dataDriver, $rootId);

$tableTreeData = $dataDriver->getEmptyCollection();

$registry = $environment->getBaseConfigRegistry();
Expand Down Expand Up @@ -1337,6 +1391,7 @@ protected function getFormatter(ModelInterface $model, $treeMode)
$formatter->setPropertyNames($label['fields']);
$formatter->setFormat($label['format']);
$formatter->setMaxLength($label['maxCharacters']);

return $formatter;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Resources/contao/templates/widget_treepicker_entry.html5
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ $model = $this->objModel; ?>
</div>
<div class="tl_right">
<?php /*if($this->minLevel && !$this->itemTable): */ ?>
<?php if ($this->fieldType == 'radio'): ?>
<?php if ($this->fieldType === 'radio'): ?>
<input type="radio"
name="<?= $this->name ?>"
id="ctrl_<?= $this->id ?>_<?= $model->getId() ?>"
class="tl_tree_radio"
value="<?= StringUtil::specialchars($model->getProperty($this->idProperty)) ?>"
<?= $this->active ?>
onfocus="Backend.getScrollOffset();" />
<?php elseif ($this->fieldType == 'checkbox'): ?>
<?php elseif ($this->fieldType === 'checkbox'): ?>
<input
type="checkbox"
name="<?= $this->name ?>[]"
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/contao/templates/widget_treepicker_popup.html5
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $GLOBALS['TL_CSS'][] = 'bundles/ccadcgeneral/css/generalTreeView.css';
<li class="tl_folder">
<div class="tl_left">&nbsp;</div>
<div class="tl_right">
<?php if ($this->fieldType == 'radio'): ?>
<?php if ($this->fieldType === 'radio'): ?>
<label
for="ctrl_<?= $this->id ?>_0"
class="tl_change_selected"><?= $this->resetSelected ?></label>
Expand All @@ -31,7 +31,7 @@ $GLOBALS['TL_CSS'][] = 'bundles/ccadcgeneral/css/generalTreeView.css';
class="tl_tree_radio"
value=""
onfocus="Backend.getScrollOffset();" />
<?php elseif ($this->fieldType == 'checkbox'): ?>
<?php elseif ($this->fieldType === 'checkbox'): ?>
<label
for="check_all_<?= $this->id ?>_0"
class="tl_change_selected"><?= $this->selectAll ?></label>
Expand Down

0 comments on commit 65f2b62

Please sign in to comment.