Skip to content

Commit

Permalink
Handle fallback for languages better for feature and feature values
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Nov 17, 2023
1 parent 8ef11bf commit b72452a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/Adapter/Feature/Repository/FeatureValueRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function getFeatureValues(?int $limit = null, ?int $offset = null, ?array
}
$featureValueIds = array_keys($indexedFeatureValues);

$localizedFeatureValues = $this->getFeatureValueLocalizedValues($featureValueIds, $filters['id_lang'] ?? null);
$localizedFeatureValues = $this->getFeatureValueLocalizedValues($featureValueIds, $filters);
foreach ($localizedFeatureValues as $localizedFeatureValue) {
$indexedFeatureValues[$localizedFeatureValue['id_feature_value']]['localized_values'][$localizedFeatureValue['id_lang']] = $localizedFeatureValue['value'];
}
Expand Down Expand Up @@ -213,22 +213,24 @@ public function getFeatureValuesCount(?array $filters = []): int

/**
* @param array $featureValuesIds
* @param int|null $langId
* @param array $filters
*
* @return array
*/
private function getFeatureValueLocalizedValues(array $featureValuesIds, ?int $langId): array
private function getFeatureValueLocalizedValues(array $featureValuesIds, array $filters): array
{
$qb = $this->connection->createQueryBuilder();
$qb->from($this->dbPrefix . 'feature_value_lang', 'fvl')
->select('fvl.*')
->where('fvl.id_feature_value IN(:featureValueIds)')
->setParameter('featureValueIds', $featureValuesIds, Connection::PARAM_INT_ARRAY)
;
if (!empty($langId)) {

if (!empty($filters['id_lang'])) {
$languageIds = is_array($filters['id_lang']) ? $filters['id_lang'] : [$filters['id_lang']];
$qb
->andWhere('fvl.id_lang = :langId')
->setParameter('langId', $langId)
->andWhere('fvl.id_lang IN (:languageIds)')
->setParameter('languageIds', $languageIds, Connection::PARAM_INT_ARRAY)
;
}

Expand Down
10 changes: 8 additions & 2 deletions src/Adapter/Form/ChoiceProvider/FeatureValuesChoiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ public function getChoices(array $options)
if (isset($options['custom'])) {
$filters['custom'] = $options['custom'];
}
$filters['id_lang'] = $options['id_lang'] ?? $this->contextLanguageId;
$cacheKey = implode('-', $filters);
$filters['id_lang'] = [
$this->contextLanguageId,
$this->defaultLanguageId,
];
$cacheKey = md5(serialize($filters));

if (!empty($this->cacheFeatureValueChoices[$cacheKey])) {
return $this->cacheFeatureValueChoices[$cacheKey];
Expand All @@ -101,6 +104,9 @@ public function getChoices(array $options)
$this->cacheFeatureValueChoices[$cacheKey][$featureValueName] = (int) $feature['id_feature_value'];
}

// Order alphabetically
ksort($this->cacheFeatureValueChoices[$cacheKey]);

return $this->cacheFeatureValueChoices[$cacheKey];
}
}
7 changes: 6 additions & 1 deletion src/Adapter/Form/ChoiceProvider/FeaturesChoiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@ public function getChoices()
foreach ($features as $feature) {
if (!empty($feature['localized_names'][$this->contextLanguageId])) {
$featureName = $feature['localized_names'][$this->contextLanguageId];
} else {
} elseif (!empty($feature['localized_names'][$this->defaultLanguageId])) {
$featureName = $feature['localized_names'][$this->defaultLanguageId];
} else {
$featureName = reset($feature['localized_names']);
}
$this->cacheFeatureChoices[$featureName] = $feature['id_feature'];
}

// Order alphabetically
ksort($this->cacheFeatureChoices);

return $this->cacheFeatureChoices;
}
}

0 comments on commit b72452a

Please sign in to comment.