diff --git a/Classes/DataProcessing/HighlightProcessor.php b/Classes/DataProcessing/HighlightProcessor.php index b88292e..82733fb 100644 --- a/Classes/DataProcessing/HighlightProcessor.php +++ b/Classes/DataProcessing/HighlightProcessor.php @@ -46,13 +46,28 @@ public function process(ContentObjectRenderer $cObj, array $contentObjectConfigu $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'bodytext_formatted'); $highlight = GeneralUtility::makeInstance(Highlighter::class); + // The column is nullable, so a record can carry NULL rather than the empty + // string the TCA default writes. Normalise before deciding, because "??" + // only substitutes for NULL and would send it on to the highlighter. + $language = trim((string)($processedData['data']['code_language'] ?? '')); + + $highlighted = null; + if ($language !== '') { + try { + $highlighted = $highlight->highlight($language, $processedData['data'][$fieldName]); + } catch (\DomainException $e) { + // The stored language is not registered - it may have been dropped from + // the list, or the record may come from an installation that had it. + // Detect instead, so a stale value does not take the page down. + $highlighted = null; + } + } + // Let highlight.php decide which code language to use from all registered if "detect automatically" is selected. - if (!($processedData['data']['code_language'] ?? true)) { + if ($highlighted === null) { $languages = $highlight->listLanguages(); $highlight->setAutodetectLanguages($languages); $highlighted = $highlight->highlightAuto($processedData['data'][$fieldName]); - } else { - $highlighted = $highlight->highlight($processedData['data']['code_language'], $processedData['data'][$fieldName]); } $processedData[$targetVariableName]['code'] = $highlighted->value;