Skip to content

Commit

Permalink
[BUGFIX] Change adding countries to backend field
Browse files Browse the repository at this point in the history
Due to some problems with instantiating domain models the country field needs to be changed to an input. This caused the itemProcFunc to not be called anymore.
Adding a FormDataProvider for the country selection fixes this.
  • Loading branch information
garbast committed Oct 3, 2024
1 parent 4f33f90 commit ea66208
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 44 deletions.
62 changes: 62 additions & 0 deletions Classes/Form/FormDataGroup/LocationCountryItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/*
* This file is developed by evoWeb.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Evoweb\StoreFinder\Form\FormDataGroup;

use TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Country\CountryProvider;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* A data provider group for casual database records
*/
class LocationCountryItems extends AbstractItemProvider implements FormDataProviderInterface
{
public function addData(array $result): array
{
$table = $result['tableName'];

if ($table === 'tx_storefinder_domain_model_location') {
$fieldName = '';

/** @var CountryProvider $countryProvider */
$countryProvider = null;
foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
if ($fieldName !== 'country') {
continue;
} elseif ($countryProvider === null) {
$countryProvider = GeneralUtility::makeInstance(CountryProvider::class);
}

foreach ($countryProvider->getAll() as $country) {
$result['processedTca']['columns']['country']['config']['items'][] = [
'label' => $country->getLocalizedNameLabel(),
'value' => $country->getAlpha2IsoCode(),
];
}
}

$result['processedTca']['columns']['country']['config']['items'] = $this->translateLabels(
$result,
$result['processedTca']['columns']['country']['config']['items'],
$table,
$fieldName
);
}

return $result;
}
}
35 changes: 0 additions & 35 deletions Classes/Hooks/TcaItemsProcessorFunctions.php

This file was deleted.

5 changes: 0 additions & 5 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ services:
Evoweb\StoreFinder\Domain\Repository\CountryRepository:
public: true

Evoweb\StoreFinder\Hooks\TcaItemsProcessorFunctions:
arguments:
$countryProvider: '@TYPO3\CMS\Core\Country\CountryProvider'
public: true

Evoweb\StoreFinder\Hooks\TceMainListener:
public: true

Expand Down
4 changes: 1 addition & 3 deletions Configuration/TCA/tx_storefinder_domain_model_location.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Evoweb\StoreFinder\Hooks\TcaItemsProcessorFunctions;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

$languageFile = 'LLL:EXT:store_finder/Resources/Private/Language/locallang_db.xlf:';
Expand Down Expand Up @@ -141,12 +140,11 @@
'type' => 'input',
'renderType' => 'selectSingle',
'items' => [
['label' => '', 'value' => ''],
['label' => $languageFile . 'tx_storefinder_domain_model_location.country.default', 'value' => ''],
],
'sortItems' => [
'label' => 'asc',
],
'itemsProcFunc' => TcaItemsProcessorFunctions::class . '->populateCountryItems',
'minitems' => 1,
'maxitems' => 1,
'behaviour' => [
Expand Down
5 changes: 4 additions & 1 deletion Resources/Private/Language/locallang_db.xlf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" original="messages" datatype="plaintext" product-name="store_finder" date="2024-08-02T16:41:21+02:00">
<header></header>
<header/>
<body>
<trans-unit id="div-address" resname="div-address">
<source>Address</source>
Expand Down Expand Up @@ -66,6 +66,9 @@
<trans-unit id="tx_storefinder_domain_model_location.country" resname="tx_storefinder_domain_model_location.country">
<source>Country</source>
</trans-unit>
<trans-unit id="tx_storefinder_domain_model_location.country.default" resname="tx_storefinder_domain_model_location.country.default">
<source>Please select a country</source>
</trans-unit>
<trans-unit id="tx_storefinder_domain_model_location.distance" resname="tx_storefinder_domain_model_location.distance">
<source>Calculated value by search query</source>
</trans-unit>
Expand Down
10 changes: 10 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Evoweb\StoreFinder\Controller\MapController;
use Evoweb\StoreFinder\Form\Element\ModifyLocationMap;
use Evoweb\StoreFinder\Form\FormDataGroup\LocationCountryItems;
use Evoweb\StoreFinder\Hooks\TceMainListener;
use TYPO3\CMS\Backend\Form\FormDataProvider\TcaSelectItems as TcaSelectItems;
use TYPO3\CMS\Extbase\Utility\ExtensionUtility;

call_user_func(function () {
Expand Down Expand Up @@ -32,6 +34,14 @@
'class' => ModifyLocationMap::class,
];

$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][
LocationCountryItems::class
] = [
'depends' => [
TcaSelectItems::class,
]
];

ExtensionUtility::configurePlugin(
'StoreFinder',
'Map',
Expand Down

0 comments on commit ea66208

Please sign in to comment.