Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: check if page is available in desired language. #15

Open
malio10AC opened this issue Mar 23, 2020 · 3 comments
Open

Added: check if page is available in desired language. #15

malio10AC opened this issue Mar 23, 2020 · 3 comments

Comments

@malio10AC
Copy link

malio10AC commented Mar 23, 2020

Hi. I have added a check to the file Classes/Middleware/RedirectionMiddleware.php

/**
 * @param int $iPageId
 * @param int $iIdLang
 * @return bool
 */
protected function checkPageAvailableInLanguage($iPageId, $iIdLang)
{
    if ($iIdLang > 0) {
        $table = 'pages';
        $queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
            ->getQueryBuilderForTable($table);
        $data = $queryBuilder
            ->select('uid')
            ->from($table)
            ->where(
                $queryBuilder->expr()->eq('l10n_parent', $iPageId)
            )
            ->andWhere(
                $queryBuilder->expr()->eq('sys_language_uid', $iIdLang)
            )
            ->andWhere(
                $queryBuilder->expr()->eq('deleted', 0)
            )
            ->andWhere(
                $queryBuilder->expr()->eq('hidden', 0)
            )
            ->setMaxResults(1)
            ->execute()
            ->fetchAll();
        if (count($data) == 0) {
            return false;
        }
    }
    return true;
}

and these lines in the methods getRedirectResponseByBrowserLanguage before
$uri = $site->getRouter()->generateUri(...

    $iIdLang = $matchingSiteLanguage->getLanguageId();
    if (!$this->checkPageAvailableInLanguage($pageArguments->getPageId(), $iIdLang)) {
        return null;
    }

and these lines in method getRedirectResponseIfCookieIsSet before
$uri = $site->getRouter()->generateUri(

                $iIdLang = (int) $requestLanguage;
                if (!$this->checkPageAvailableInLanguage($pageArguments->getPageId(), $iIdLang)) {
                    return null;
                }

I hope you can use that (or something similar) for upcoming versions because I don't want to merge the code after every update.

Thanks and have a nice day.

@urbantrout
Copy link
Owner

Thanks for that! Could you create a pull request?

@DavRet
Copy link

DavRet commented Apr 30, 2021

Hey @malio10AC, I think i found a bug in your code.

In the method getRedirectResponseIfCookieIsSet:

$iIdLang = (int) $requestLanguage;

This way $iIdLang would always have the value "1" because $requestLanguage is an array.
I also think $requestLanguage is the wrong array to use here because you want to check if the page is available in the preferred language (set in the cookie), not in the requested language.

I've changed the code to:

$iIdLang = (int)$preferredSiteLanguage->getLanguageId();

Now it works for me. Thanks for your code! :)

@klodeckl
Copy link

Thanks for your code @malio10AC @DavRet, it works for me.

One thing would be missing: If a detail page of a record (e.g.) is displayed, it should be checked if the record is translated before redirecting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants