Skip to content

Commit 064d211

Browse files
committed
Add AbstractSelect::getFilterOptionsForDcGeneral
This method will always return the options list based on id instead of alias. Therefore we do not have problems anymore, when editing data in a language different from the language the ui is using. The problem was, when the select was referencing a translated MetaModel and the configured alias column was also translated, then is was tried to save an alias value in the language of the UI instead of the currently active dataset rendering an exception to be thrown as the select attribute does not accept the value (it does not know about).
1 parent 155aa4b commit 064d211

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

src/MetaModels/Attribute/Select/AbstractSelect.php

+9
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ protected function isFilterOptionRetrievingPossible($idList)
161161
return $this->isProperlyConfigured() && (($idList === null) || !empty($idList));
162162
}
163163

164+
/**
165+
* Obtain the filter options with always the id being contained instead of the alias.
166+
*
167+
* This is being called from BackendSubscriber to circumvent problems when dealing with translated aliases.
168+
*
169+
* @return array
170+
*/
171+
abstract public function getFilterOptionsForDcGeneral();
172+
164173
/**
165174
* {@inheritdoc}
166175
*/

src/MetaModels/Attribute/Select/MetaModelSelect.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ protected function getValuesById($valueIds)
148148
*/
149149
public function valueToWidget($varValue)
150150
{
151-
if (isset($varValue[$this->getAliasColumn()])) {
151+
if (isset($varValue[$this->getIdColumn()])) {
152152
// Hope the best that this is unique...
153-
return (string) $varValue[$this->getAliasColumn()];
153+
return (string) $varValue[$this->getIdColumn()];
154154
}
155155

156156
if (isset($varValue[self::SELECT_RAW]['id'])) {
@@ -168,7 +168,7 @@ public function valueToWidget($varValue)
168168
public function widgetToValue($varValue, $itemId)
169169
{
170170
$model = $this->getSelectMetaModel();
171-
$alias = $this->getAliasColumn();
171+
$alias = $this->getIdColumn();
172172
$attribute = $model->getAttribute($alias);
173173

174174
if ($attribute) {
@@ -220,6 +220,32 @@ public function widgetToValue($varValue, $itemId)
220220
return $value[$valueId];
221221
}
222222

223+
/**
224+
* {@inheritDoc}
225+
*
226+
* @SuppressWarnings(PHPMD.Superglobals)
227+
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
228+
*/
229+
public function getFilterOptionsForDcGeneral()
230+
{
231+
if (!$this->isFilterOptionRetrievingPossible(null)) {
232+
return array();
233+
}
234+
235+
$originalLanguage = $GLOBALS['TL_LANGUAGE'];
236+
$GLOBALS['TL_LANGUAGE'] = $this->getMetaModel()->getActiveLanguage();
237+
238+
$filter = $this->getSelectMetaModel()->getEmptyFilter();
239+
240+
$this->buildFilterRulesForFilterSetting($filter);
241+
242+
$objItems = $this->getSelectMetaModel()->findByFilter($filter, $this->getSortingColumn());
243+
244+
$GLOBALS['TL_LANGUAGE'] = $originalLanguage;
245+
246+
return $this->convertItemsToFilterOptions($objItems, $this->getValueColumn(), $this->getIdColumn());
247+
}
248+
223249
/**
224250
* Fetch filter options from foreign table taking the given flag into account.
225251
*

src/MetaModels/Attribute/Select/Select.php

+21-16
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,36 @@ public function getAttributeSettingNames()
9393
*/
9494
public function valueToWidget($varValue)
9595
{
96-
$strColNameAlias = $this->get('select_alias');
97-
if ($this->isTreePicker() || !$strColNameAlias) {
98-
$strColNameAlias = $this->getIdColumn();
99-
}
100-
101-
return $varValue[$strColNameAlias];
96+
return $varValue[$this->getIdColumn()];
10297
}
10398

10499
/**
105100
* {@inheritdoc}
106101
*/
107102
public function widgetToValue($varValue, $itemId)
108103
{
109-
$database = $this->getDatabase();
110-
$strColNameAlias = $this->getAliasColumn();
111-
$strColNameId = $this->getIdColumn();
112-
if ($this->isTreePicker()) {
113-
$strColNameAlias = $strColNameId;
114-
}
115-
// Lookup the id for this value.
116-
$objValue = $database
117-
->prepare(sprintf('SELECT %1$s.* FROM %1$s WHERE %2$s=?', $this->getSelectSource(), $strColNameAlias))
104+
// Lookup the value.
105+
$values = $this->getDatabase()
106+
->prepare(sprintf('SELECT %1$s.* FROM %1$s WHERE %2$s=?', $this->getSelectSource(), $this->getIdColumn()))
118107
->execute($varValue);
119108

120-
return $objValue->row();
109+
return $values->row();
110+
}
111+
112+
/**
113+
* {@inheritDoc}
114+
*
115+
* @SuppressWarnings(PHPMD.Superglobals)
116+
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
117+
*/
118+
public function getFilterOptionsForDcGeneral()
119+
{
120+
if (!$this->isFilterOptionRetrievingPossible(null)) {
121+
return array();
122+
}
123+
124+
$values = $this->getFilterOptionsForUsedOnly(false);
125+
return $this->convertOptionsList($values, $this->getIdColumn(), $this->getValueColumn());
121126
}
122127

123128
/**

src/MetaModels/DcGeneral/Events/MetaModels/Select/BackendSubscriber.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getPropertyOptions(GetPropertyOptionsEvent $event)
6161
}
6262

6363
try {
64-
$options = $attribute->getFilterOptions(null, false);
64+
$options = $attribute->getFilterOptionsForDcGeneral();
6565
} catch (\Exception $exception) {
6666
$options = array('Error: ' . $exception->getMessage());
6767
}

0 commit comments

Comments
 (0)