typo3 extbase fetch correct media image for translated pages

raw

CustomPageRepository.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
 
class CustomPageRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
    /**
     * Load translated main visual images for the page rows
     * Needed because of https://forge.typo3.org/issues/57272
     *
     * @param QueryResult $results Array of page objects
     *
     * @return array Page objects
     */
    protected function loadTranslatedMainVisuals($results)
    {
        if ($_GET['L'] == 0) {
            //default language
            return $results;
        }
 
        $ids = [];
        foreach ($results as $page) {
            $ids[] = intval($page->getUid());
        }
        $qb = GeneralUtility::makeInstance(ConnectionPool::class)
            ->getQueryBuilderForTable('pages_language_overlay');
        $stmt = $qb->select('uid', 'pid')
            ->from('pages_language_overlay')
            ->where($qb->expr()->in('pid', $ids))
            ->execute();
 
        $translations = [];
        while ($row = $stmt->fetch()) {
            $translations[$row['uid']] = $row['pid'];
        }
 
 
        $qb = GeneralUtility::makeInstance(ConnectionPool::class)
            ->getQueryBuilderForTable('sys_file_reference');
        $stmt = $qb->select('*')
            ->from('sys_file_reference')
            ->where(
                $qb->expr()->eq('tablenames', $qb->createNamedParameter('pages_language_overlay')),
                $qb->expr()->eq('fieldname', $qb->createNamedParameter('media')),
                $qb->expr()->in('uid_foreign', array_keys($translations))
            )
            ->execute();
        $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
        $files = [];
        while ($row = $stmt->fetch()) {
            //QueryResult does not support direct uid access,
            // so we have to use the intermediate files array
            $origPageUid = $translations[$row['uid_foreign']];
            $files[$origPageUid] = $resourceFactory->getFileReferenceObject(
                $row['uid'], $row
            );
        }
        foreach ($results as $page) {
            if (isset($files[$page->getUid()])) {
                $page->setMainVisual($files[$page->getUid()]);
            }
        }
 
        return $results;
    }
}
?>
 
Christian Weiske Christian Weiske
owner

History