Skip to content

BUG: PHP 8: TypeError in LanguageMenuViewHelper and AbstractMenuViewHelper due to null not handled #1975

Description

@enilee

Environment

  • VHS Version: 7.2.1
  • TYPO3 Version: 11.5.41
  • PHP Version: 8.1.33

Problem 1 – LanguageMenuViewHelper::getLanguageFlag() – null label causes TypeError

Error message

FluidTYPO3\Vhs\ViewHelpers\Page\LanguageMenuViewHelper::getLanguageFlag():
Argument #2 ($label) must be of type string, null given,
called in .../vhs/Classes/ViewHelpers/Page/LanguageMenuViewHelper.php on line 364

Root cause

In parseLanguageMenu(), the labelOverwrite argument is applied positionally to the language array. If the labelOverwrite list has fewer entries than languages configured in the TYPO3 site configuration, the remaining languages receive null as their label:

// Classes/ViewHelpers/Page/LanguageMenuViewHelper.php ~line 333
if (!empty($labelOverwrite)) {
    $i = 0;
    foreach ($languageMenu as $key => $value) {
        $languageMenu[$key]['label'] = $labelOverwrite[$i]; // null if $i >= count($labelOverwrite)
        $i++;
    }
}

This null label is then passed to getLanguageFlag() at line 364 which has a strict string type hint:

$languageMenu[$key]['flagSrc'] = $this->getLanguageFlag($value['flag'] ?? $value['iso'], $value['label']); // $value['label'] can be null

The hideNotTranslated filter at line 377 runs after getLanguageFlag() is called – so even hideNotTranslated="true" does not prevent the crash.

Fix

Cast $value['label'] to string with a null fallback:

// line 364
$languageMenu[$key]['flagSrc'] = $this->getLanguageFlag($value['flag'] ?? $value['iso'], (string)($value['label'] ?? ''));

// line 372–374
$languageMenu[$key]['flagCode'] = $this->getLanguageFlag(
    $value['flag'] ?? $value['iso'],
    (string)($value['label'] ?? '')
);

Problem 2 – AbstractMenuViewHelper::getItemTitle() – null title causes TypeError

Error message

FluidTYPO3\Vhs\ViewHelpers\Menu\AbstractMenuViewHelper::getItemTitle():
Return value must be of type string, null returned

Root cause

When a page in the TYPO3 page tree has no translation in the currently active language, the overlay mechanism returns a stub page record with uid => null and title => null. This stub is included in the menu pages array passed to parseMenu().

getItemTitle() is declared with return type string, but $page['title'] is null for these stub entries:

// Classes/ViewHelpers/Menu/AbstractMenuViewHelper.php ~line 475
protected function getItemTitle(array $page): string
{
    $titleFieldList = GeneralUtility::trimExplode(',', $this->arguments['titleFields']);
    foreach ($titleFieldList as $titleFieldName) {
        if (!empty($page[$titleFieldName])) {
            return $page[$titleFieldName];
        }
    }

    return $page['title']; // null if page has no translation → PHP 8 TypeError
}

Fix

Cast the return value to string with a null fallback:

return (string)($page['title'] ?? '');

Notes

Both issues are PHP 8 strict type incompatibilities that do not occur in PHP 7 (where null is silently coerced to string). They were introduced or became visible after VHS added return type declarations and argument type hints without ensuring all code paths return non-null values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions