Skip to content

Commit 1d948f4

Browse files
committed
[BUGFIX] Let Country object retrieval handle NULL value
This fixes a PHP type error when saving a NULL value as country object. Can be reproduced by saving an EXT:styleguide entry of type "elements - select" with default values (only fill in required fields). Resolves: #107094 Releases: main Change-Id: I861f94399ec7d2a6ca329a65290991e81798b2bd Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/90059 Tested-by: Oliver Hader <oliver.hader@typo3.org> Reviewed-by: Oliver Hader <oliver.hader@typo3.org> Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de> Reviewed-by: Sebastian Iffland <sebastian.iffland@fullhaus.de> Reviewed-by: Garvin Hicking <garvin@hick.ing> Tested-by: Oliver Klee <typo3-coding@oliverklee.de> Reviewed-by: Simon Schaufelberger <simonschaufi+typo3@gmail.com> Tested-by: Simon Schaufelberger <simonschaufi+typo3@gmail.com> Tested-by: core-ci <typo3@b13.com> Tested-by: Garvin Hicking <garvin@hick.ing>
1 parent 1435ffd commit 1d948f4

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

typo3/sysext/backend/Classes/Utility/BackendUtility.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ public static function getProcessedValue(
16931693
}
16941694
break;
16951695
case 'country':
1696-
$country = GeneralUtility::makeInstance(CountryProvider::class)->getByIsoCode($value);
1696+
$country = GeneralUtility::makeInstance(CountryProvider::class)->getByIsoCode($value ?? '');
16971697
if ($country) {
16981698
$l = $lang->sL($country->getLocalizedNameLabel());
16991699
}

typo3/sysext/core/Classes/Country/CountryProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,11 @@ public function getAll(): array
17431743
return $this->countries;
17441744
}
17451745

1746+
/**
1747+
* Searches for a matching country with fallback; uses Alpha2 ISO-Code by
1748+
* default, and if that does not match, compare the input $isoCode
1749+
* as an Alpha3 ISO-Code.
1750+
*/
17461751
public function getByIsoCode(string $isoCode): ?Country
17471752
{
17481753
$isoCode = strtoupper($isoCode);
@@ -1773,6 +1778,7 @@ public function getByAlpha3IsoCode(string $isoCode): ?Country
17731778
}
17741779
return null;
17751780
}
1781+
17761782
public function getByEnglishName(string $name): ?Country
17771783
{
17781784
foreach ($this->countries as $country) {

typo3/sysext/core/Tests/Unit/Country/CountryProviderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public function findByIsoCodeReturnsValidObject(): void
4747
self::assertEquals('French Republic', $countryIsoCode2->getOfficialName());
4848
}
4949

50+
#[Test]
51+
public function findByUnspecifiedIsoCodeReturnsNull(): void
52+
{
53+
$subject = new CountryProvider(new NoopEventDispatcher());
54+
$emptyIsocode = $subject->getByIsoCode('');
55+
self::assertNull($emptyIsocode);
56+
}
57+
5058
#[Test]
5159
public function findByThreeLetterIsoCodeReturnsValidObject(): void
5260
{

typo3/sysext/form/Classes/ViewHelpers/RenderFormValueViewHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function processElementValue(
9999
$properties = $element->getProperties();
100100
$options = $properties['options'] ?? null;
101101
if ($element->getType() === 'CountrySelect') {
102-
$country = GeneralUtility::makeInstance(CountryProvider::class)->getByIsoCode($value);
102+
$country = GeneralUtility::makeInstance(CountryProvider::class)->getByIsoCode($value ?? '');
103103
if ($country !== null) {
104104
return $country->getName();
105105
}

0 commit comments

Comments
 (0)