Skip to content

Commit

Permalink
[FEATURE] Make it possible to create preview-URLs for enabled languages
Browse files Browse the repository at this point in the history
Resolves: b13#4
  • Loading branch information
sypets committed Mar 6, 2024
1 parent 21d8a7f commit ade297f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
31 changes: 25 additions & 6 deletions Classes/Http/Middleware/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\HttpFoundation\Cookie;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;
use TYPO3\CMS\Core\Database\ConnectionPool;
Expand All @@ -36,9 +37,12 @@ class Preview implements MiddlewareInterface

protected Context $context;

public function __construct(Context $context)
protected array $extConf;

public function __construct(Context $context, ExtensionConfiguration $extensionConfiguration)
{
$this->context = $context;
$this->extConf = $extensionConfiguration->get('authorized_preview');
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand All @@ -57,8 +61,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $handler->handle($request);
}

if ($language->isEnabled()) {
return $handler->handle($request);
if (!($this->extConf['showPreviewForEnabledLanguages'] ?? false)) {
if ($language->isEnabled()) {
return $handler->handle($request);
}
}

$hash = $this->findHashInRequest($request);
Expand All @@ -76,7 +82,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// Store config in request
$request = $request->withAttribute(self::REQUEST_ATTRIBUTE, $config);

$this->initializePreviewUser($language);
$pageId = $config->getPageId();

if (!$this->initializePreviewUser($language, $pageId)) {
return $handler->handle($request);
}
$response = $handler->handle($request);

// If the GET parameter PreviewUriBuilder::PARAMETER_NAME is set, then a cookie is set for the next request
Expand Down Expand Up @@ -108,12 +118,21 @@ protected function findHashInRequest(ServerRequestInterface $request): string
/**
* Creates a preview user and sets the current page ID (for accessing the page)
*/
protected function initializePreviewUser(SiteLanguage $language): void
protected function initializePreviewUser(SiteLanguage $language, int $pageId = 0): bool
{
$previewUser = GeneralUtility::makeInstance(PreviewUserAuthentication::class, $language);
$previewUser->setWebmounts([$GLOBALS['TSFE']->id]);
if (!$pageId) {
if (isset($GLOBALS['TSFE'])) {
$pageId = $GLOBALS['TSFE']->id;
}
}
if (!$pageId) {
return false;
}
$previewUser->setWebmounts([$pageId]);
$GLOBALS['BE_USER'] = $previewUser;
$this->setBackendUserAspect($previewUser);
return true;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions Classes/Preview/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public function validForPageId(int $pageId): bool
{
return $pageId === $this->pageId;
}

public function getPageId(): int
{
return $this->pageId;
}
}
7 changes: 4 additions & 3 deletions Classes/SiteWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* of the License, or any later version.
*/

use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;

Expand All @@ -27,12 +28,12 @@ class SiteWrapper
*/
protected array $disabledLanguages = [];

public function __construct(Site $site)
public function __construct(Site $site, ExtensionConfiguration $extensionConfiguration)
{
$this->site = $site;

$showPreviewForEnabledLanguages = (bool)$extensionConfiguration->get('authorized_preview', 'showPreviewForEnabledLanguages');
foreach ($this->site->getAllLanguages() as $languageId => $language) {
if ($language->enabled() === false) {
if ($showPreviewForEnabledLanguages && $language->enabled() === false) {
$this->disabledLanguages[] = $language;
}
}
Expand Down
2 changes: 2 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cat=basic; type=bool; label=If this is enabled, it is also possible to show preview URLS for enabled languages
showPreviewForEnabledLanguages = 0

0 comments on commit ade297f

Please # to comment.