Skip to content

Commit

Permalink
Added null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tlueder committed Mar 6, 2024
1 parent f55f75f commit 80b02f1
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions Classes/Middleware/MiddlewareActionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;

abstract class MiddlewareActionAbstract {
protected LanguageService $languageService = null;
protected ?LanguageService $languageService = null;

protected LanguageServiceFactory $languageServiceFactory;

Expand All @@ -33,9 +33,9 @@ abstract class MiddlewareActionAbstract {

protected ServerRequestInterface $request;

protected Site $site;
protected ?Site $site;

protected SiteLanguage $siteLanguage;
protected ?SiteLanguage $siteLanguage;

protected UriBuilder $uriBuilder;

Expand All @@ -52,31 +52,40 @@ public function __construct(ServerRequestInterface $request, $pathParams) {

$langId = $this->queryParams['L'] ?? false;
if (false != $langId) {
$this->siteLanguage = $this->site->getLanguageById(intval($langId));
$this->siteLanguage = $this->site?->getLanguageById(intval($langId));
} else {
$this->siteLanguage = $this->request->getAttribute('language');
}

$this->languageServiceFactory = GeneralUtility::makeInstance(LanguageServiceFactory::class);
$this->languageService = $this->languageServiceFactory->createFromSiteLanguage($this->siteLanguage);
$GLOBALS['LANG'] = $this->languageService;
$this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);

// Set LanguageAspect for exbase
$context = GeneralUtility::makeInstance(Context::class);
$context->setAspect('language', new LanguageAspect($this->siteLanguage->getLanguageId()));
$context->setAspect('language', new LanguageAspect($this->siteLanguage?->getLanguageId() ?? 0));

$this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
if (null !== $this->siteLanguage) {
$this->languageService = $this->languageServiceFactory->createFromSiteLanguage($this->siteLanguage);
$GLOBALS['LANG'] = $this->languageService;
}
}

protected function getAbsPath(?FileReference $file): string {
if (null == $file) {
return '';
}
if (null == $this->siteLanguage) {
return '';
}

return $this->siteLanguage->getBase()->getScheme().'://'.$this->siteLanguage->getBase()->getHost().'/'.$file->getOriginalResource()->getPublicUrl();
}

protected function getLocalizationFromKey(string $keyToTranslate, string $filePath): string {
if (null === $this->siteLanguage || null === $this->languageService) {
return $keyToTranslate;
}

$translatedString = $this->languageService->sL('LLL:'.$filePath.':'.$keyToTranslate);

if (!empty($translatedString)) {
Expand Down

0 comments on commit 80b02f1

Please sign in to comment.