evoWeb / store_finder

Store finder extension for TYPO3
GNU General Public License v2.0
2 stars 9 forks source link

Error on search with settings.countryValueType different from uid #34

Closed studiokaloa closed 2 years ago

studiokaloa commented 2 years ago

Hi,

With plugin.tx_storefinder.settings.countryValueType = isoCodeA2 or plugin.tx_storefinder.settings.countryValueType = isoCodeA3

constraint["country"] for search is a STRING (DE, DEU) and not an INT so the property mapping fails when initializeAction() of controller MapController.php call $this->setTypeConverter();

I solved this by this modification before the call :

protected function initializeAction(){
    if (isset($this->settings['override']) && is_array($this->settings['override'])) {
        $override = $this->settings['override'];
        unset($this->settings['override']);
        $this->settings = array_merge($this->settings, $override);
    }

    $this->settings['allowedCountries'] = $this->settings['allowedCountries']?explode(',', $this->settings['allowedCountries']):[];
    $this->geocodeService->setSettings($this->settings);
    $this->locationRepository->setSettings($this->settings);

    /******* START MODIFICATION */
    if ($this->request->hasArgument('constraint')) {
        $constraint = $this->request->getArgument('constraint');
        if( !intval($constraint['country']) ) {
            /** @var CountryRepository $countryRepository */
            $countryRepository = GeneralUtility::getContainer()->get(CountryRepository::class);
            /** @var $country Country */
            if (strlen($constraint['country']) === 2) {
                $value = $countryRepository->findByIsoCodeA2([$constraint['country']])->getFirst();
            } elseif (strlen($constraint['country']) === 3) {
                $value = $countryRepository->findByIsoCodeA3($constraint['country']);
            }
            $constraint['country'] = $value->getUid();
            $this->request->setArgument('constraint', $constraint);
        }
    }
    /******* END MODIFICATION */

    $this->setTypeConverter();
}

And in the country field list of the search form in Resources/Private/Partials/Search.html to get the good option preselected :

<f:switch expression="{settings.countryValueType}">
    <f:case value="uid"><f:variable name="value" value="{constraint.country.uid}" /></f:case>
    <f:case value="isoCodeA2"><f:variable name="value" value="{constraint.country.isoCodeA2}" /></f:case>
    <f:case value="isoCodeA3"><f:variable name="value" value="{constraint.country.isoCodeA3}" /></f:case>
</f:switch>
<sf:form.selectCountries 
    property="country" 
    id="country" 
    class="form-control custom-select"
    optionValueField="{settings.countryValueType}" 
    allowedCountries="{settings.allowedCountries}"
value="{value}" />