Skip to content

Commit

Permalink
Merge pull request #23 from pagemachine/typo3v9-extbase-translations
Browse files Browse the repository at this point in the history
Support translated Extbase previews with TYPO3v9+
  • Loading branch information
mbrodala authored Oct 9, 2020
2 parents c30c49a + 583a8d1 commit 156fa03
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 15 deletions.
70 changes: 56 additions & 14 deletions Classes/Service/IndexingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
use PAGEmachine\Searchable\IndexManager;
use PAGEmachine\Searchable\PipelineManager;
use PAGEmachine\Searchable\Service\ExtconfService;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;

final class IndexingService
Expand All @@ -27,6 +31,19 @@ public function injectIndexerFactory(IndexerFactory $indexerFactory): void
$this->indexerFactory = $indexerFactory;
}

/**
* @var PersistenceManagerInterface $persistenceManager
*/
protected $persistenceManager;

/**
* @param PersistenceManagerInterface $persistenceManager
*/
public function injectPersistenceManager(PersistenceManagerInterface $persistenceManager): void
{
$this->persistenceManager = $persistenceManager;
}

/**
* @var Dispatcher $signalDispatcher
*/
Expand Down Expand Up @@ -228,13 +245,14 @@ protected function runIndexers(): void
$this->logger->debug(sprintf('Indexing language "%s"', $language));

$environment = ExtconfService::getIndexEnvironment(ExtconfService::getIndex($language));
$originalEnvironment = $this->applyEnvironment($environment);
$restoreEnvironment = $this->applyEnvironment((int)$language, $environment);

foreach ($indexers as $indexer) {
$this->runSingleIndexer($indexer);
}

$this->applyEnvironment($originalEnvironment);
$restoreEnvironment();
$this->resetPersistence();
} else {
$this->logger->warning(sprintf('No indexers found for language "%s", doing nothing', $language));
}
Expand Down Expand Up @@ -282,23 +300,47 @@ protected function runSingleIndexer(IndexerInterface $indexer): void
/**
* Apply the given environment, e.g. language and locale
*
* @return array the original environment to be restored with another call
* @return \Closure callback to restore the original environment
*/
protected function applyEnvironment(array $environment): array
protected function applyEnvironment(int $languageUid, array $environment): \Closure
{
$originalEnvironment = [];

if (!empty($environment['language'])) {
$originalEnvironment['language'] = $GLOBALS['BE_USER']->uc['lang'];
$GLOBALS['BE_USER']->uc['lang'] = $environment['language'];
$originalUserLanguage = $GLOBALS['BE_USER']->uc['lang'];
$originalLocale = setlocale(LC_ALL, '0');

$GLOBALS['BE_USER']->uc['lang'] = $environment['language'];
setlocale(LC_ALL, $environment['locale']);

if (class_exists(Context::class)) {
$context = GeneralUtility::makeInstance(Context::class);
$originalLanguageAspect = $context->getAspect('language');

$restoreEnvironment = function () use ($originalUserLanguage, $originalLocale, $originalLanguageAspect): void {
$GLOBALS['BE_USER']->uc['lang'] = $originalUserLanguage;
setlocale(LC_ALL, $originalLocale);

$context = GeneralUtility::makeInstance(Context::class);
$context->setAspect('language', $originalLanguageAspect);
};

$context->setAspect('language', new LanguageAspect($languageUid));
} else { // TYPO3v8
$restoreEnvironment = function () use ($originalUserLanguage, $originalLocale): void {
$GLOBALS['BE_USER']->uc['lang'] = $originalUserLanguage;
setlocale(LC_ALL, $originalLocale);
};
}

if (!empty($environment['locale'])) {
$originalEnvironment['locale'] = setlocale(LC_ALL, '0');
setlocale(LC_ALL, $environment['locale']);
}
return $restoreEnvironment;
}

return $originalEnvironment;
/**
* Reset the Extbase persistence
*
* This is essential e.g. for retrieving objects once per language.
*/
private function resetPersistence(): void
{
$this->persistenceManager->clearState();
}

/**
Expand Down
21 changes: 21 additions & 0 deletions Tests/Functional/AbstractElasticsearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Nimut\TestingFramework\TestCase\FunctionalTestCase;
use PAGEmachine\Searchable\Connection;
use PAGEmachine\Searchable\Indexer\PagesIndexer;
use PAGEmachine\Searchable\Indexer\TcaIndexer;
use PAGEmachine\Searchable\Service\IndexingService;
use Pagemachine\SearchableExtbaseL10nTest\Preview\ContentPreviewRenderer;
use TYPO3\CMS\Core\Configuration\SiteConfiguration;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Core\Environment;
Expand All @@ -27,6 +29,7 @@ abstract class AbstractElasticsearchTest extends FunctionalTestCase
*/
protected $testExtensionsToLoad = [
'typo3conf/ext/searchable',
'typo3conf/ext/searchable/Tests/Functional/Fixtures/Extensions/extbase_l10n_test',
];

/**
Expand Down Expand Up @@ -81,6 +84,24 @@ protected function setUp()
],
],
],
'content' => [
'className' => TcaIndexer::class,
'config' => [
'type' => 'content',
'collector' => [
'config' => [
'table' => 'tt_content',
'pid' => 1,
'fields' => [
'header',
],
],
],
'preview' => [
'className' => ContentPreviewRenderer::class,
],
],
],
],
]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types = 1);

namespace Pagemachine\SearchableExtbaseL10nTest\Domain\Model;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

final class Content extends AbstractEntity
{
/**
* @var string
*/
protected $header;

public function getHeader(): string
{
return $this->header;
}

public function setHeader(string $header): void
{
$this->header = $header;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types = 1);

namespace Pagemachine\SearchableExtbaseL10nTest\Domain\Repository;

use TYPO3\CMS\Extbase\Persistence\Repository;

final class ContentRepository extends Repository
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types = 1);

namespace Pagemachine\SearchableExtbaseL10nTest\Preview;

use PAGEmachine\Searchable\Preview\PreviewRendererInterface;
use Pagemachine\SearchableExtbaseL10nTest\Domain\Repository\ContentRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

final class ContentPreviewRenderer implements PreviewRendererInterface
{
/**
* @param array $record
* @return string
*/
public function render($record)
{
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$repository = $objectManager->get(ContentRepository::class);
$content = $repository->findByIdentifier($record['uid']);
$preview = sprintf(
'Preview: %s [%d]',
$content->getHeader(),
$content->_getProperty('_localizedUid')
);

return $preview;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types = 1);

return [
\Pagemachine\SearchableExtbaseL10nTest\Domain\Model\Content::class => [
'tableName' => 'tt_content',
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pagemachine/searchable-extbase-l10n-test",
"autoload": {
"psr-4": {
"Pagemachine\\SearchableExtbaseL10nTest\\": "Classes/"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$EM_CONF[$_EXTKEY] = [
'title' => 'Extbase Localization Test',
'version' => '0.0.0',
'constraints' => [
'depends' => [
'typo3' => '0.0.0',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
config.tx_extbase {
persistence {
classes {
Pagemachine\SearchableExtbaseL10nTest\Domain\Model\Content {
mapping {
tableName = tt_content
}
}
}
}
}
44 changes: 44 additions & 0 deletions Tests/Functional/Service/IndexingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,50 @@ public function indexesRecordTranslations(): void
], 1);
}

/**
* @test
*/
public function appliesLanguageForRecordTranslationIndexing(): void
{
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9', '<')) {
$this->markTestSkipped('TYPO3v9+ only');
}

$this->getDatabaseConnection()->insertArray('tt_content', [
'uid' => 1,
'pid' => 1,
'header' => 'Test content',
]);
$this->getDatabaseConnection()->insertArray('tt_content', [
'uid' => 2,
'pid' => 1,
'l18n_parent' => 1, // [sic!]
'sys_language_uid' => 1,
'header' => 'Translated test content',
]);

$this->assertIndexEmpty(0);
$this->assertIndexEmpty(1);

$this->indexingService->setup();
$this->indexingService->indexFull('content');

$this->assertDocumentInIndex([
'uid' => 1,
'header' => 'Test content',
'searchable_meta' => [
'preview' => 'Preview: Test content [1]',
],
], 0);
$this->assertDocumentInIndex([
'uid' => 1,
'header' => 'Translated test content',
'searchable_meta' => [
'preview' => 'Preview: Translated test content [2]',
],
], 1);
}

/**
* @test
*/
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"autoload-dev": {
"psr-4": {
"PAGEmachine\\Searchable\\Tests\\": "Tests/",
"Pagemachine\\SearchableExtbaseL10nTest\\": "Tests/Functional/Fixtures/Extensions/extbase_l10n_test/Classes/",
"TYPO3\\CMS\\Core\\Tests\\": "vendor/typo3/cms/typo3/sysext/core/Tests/"
},
"classmap": [
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ parameters:
- message: '#Psr\\Http\\Server\\MiddlewareInterface#'
path: Classes/Middleware/UriBuilder.php
- message: '#TYPO3\\CMS\\Core\\Context\\.+#'
path: Classes/ViewHelpers/SiteLanguageViewHelper.php
paths:
- Classes/Service/IndexingService.php
- Classes/ViewHelpers/SiteLanguageViewHelper.php
- message: '#TYPO3\\CMS\\Core\\Database\\PostProcessQueryHookInterface#'
paths:
- ext_localconf.php
Expand Down

0 comments on commit 156fa03

Please sign in to comment.