Skip to content

Commit

Permalink
[TASK] Move backend post header to dedicated renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed May 11, 2023
1 parent 4de4bcd commit 1fbde27
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 49 deletions.
72 changes: 72 additions & 0 deletions Classes/Backend/View/BlogPostHeaderContentRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types = 1);

/*
* This file is part of the package t3g/blog.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace T3G\AgencyPack\Blog\Backend\View;

use Psr\Http\Message\ServerRequestInterface;
use T3G\AgencyPack\Blog\Constants;
use T3G\AgencyPack\Blog\Domain\Repository\PostRepository;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

class BlogPostHeaderContentRenderer implements SingletonInterface
{
protected ExtensionConfiguration $extensionConfiguration;
protected PostRepository $postRepository;

public function __construct(
ExtensionConfiguration $extensionConfiguration,
PostRepository $postRepository
) {
$this->extensionConfiguration = $extensionConfiguration;
$this->postRepository = $postRepository;
}

public function render(ServerRequestInterface $request): string
{
$blogConfiguration = $this->extensionConfiguration->get('blog');
if ((bool)($blogConfiguration['disablePageLayoutHeader']) ?? true) {
return '';
}

$pageUid = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
$pageInfo = BackendUtility::readPageAccess($pageUid, $GLOBALS['BE_USER']->getPagePermsClause(Permission::PAGE_SHOW));
if (($pageInfo['doktype'] ?? 0) !== Constants::DOKTYPE_BLOG_POST) {
return '';
}

$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->addCssFile('EXT:blog/Resources/Public/Css/pagelayout.min.css', 'stylesheet', 'all', '', false);

$query = $this->postRepository->createQuery();
$querySettings = $query->getQuerySettings();
$querySettings->setIgnoreEnableFields(true);
$this->postRepository->setDefaultQuerySettings($querySettings);
$post = $this->postRepository->findByUidRespectQuerySettings($pageUid);

$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->getRenderingContext()->getTemplatePaths()->fillDefaultsByPackageName('blog');
$view->setTemplate('PageLayout/Header');
$view->assignMultiple([
'pageUid' => $pageUid,
'pageInfo' => $pageInfo,
'post' => $post,
]);

$content = $view->render();
return $content;
}
}
46 changes: 9 additions & 37 deletions Classes/Hooks/PageLayoutHeaderHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,23 @@

namespace T3G\AgencyPack\Blog\Hooks;

use T3G\AgencyPack\Blog\Constants;
use T3G\AgencyPack\Blog\Domain\Repository\PostRepository;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\StandaloneView;
use T3G\AgencyPack\Blog\Backend\View\BlogPostHeaderContentRenderer;

class PageLayoutHeaderHook
{
protected BlogPostHeaderContentRenderer $blogPostHeaderContentRenderer;

public function __construct(BlogPostHeaderContentRenderer $blogPostHeaderContentRenderer)
{
$this->blogPostHeaderContentRenderer = $blogPostHeaderContentRenderer;
}

/**
* @return string
*/
public function drawHeader()
{
$request = $GLOBALS['TYPO3_REQUEST'];
$pageUid = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
$pageInfo = BackendUtility::readPageAccess($pageUid, $GLOBALS['BE_USER']->getPagePermsClause(Permission::PAGE_SHOW));

// Early exit for non-blog pages
if (($pageInfo['doktype'] ?? 0) !== Constants::DOKTYPE_BLOG_POST) {
return '';
}

$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->addCssFile('EXT:blog/Resources/Public/Css/pagelayout.min.css', 'stylesheet', 'all', '', false);

$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$repository = $objectManager->get(PostRepository::class);
$query = $repository->createQuery();
$querySettings = $query->getQuerySettings();
$querySettings->setIgnoreEnableFields(true);
$repository->setDefaultQuerySettings($querySettings);
$post = $repository->findByUidRespectQuerySettings($pageUid);

$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->getRenderingContext()->getTemplatePaths()->fillDefaultsByPackageName('blog');
$view->setTemplate('PageLayout/Header');
$view->assignMultiple([
'pageUid' => $pageUid,
'pageInfo' => $pageInfo,
'post' => $post,
]);

return $view->render();
return $this->blogPostHeaderContentRenderer->render($request);
}
}
3 changes: 3 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ services:

T3G\AgencyPack\Blog\:
resource: '../Classes/*'

T3G\AgencyPack\Blog\Hooks\PageLayoutHeaderHook:
public: true
16 changes: 4 additions & 12 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,17 @@
die('Access denied.');
}

// Make the extension configuration accessible
$extensionConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class
);
$blogConfiguration = $extensionConfiguration->get('blog');

// PageTS
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPageTSConfig('<INCLUDE_TYPOSCRIPT: source="FILE:EXT:blog/Configuration/TsConfig/Page/All.tsconfig">');

// Register "blogvh" as global fluid namespace
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['blogvh'][] = 'T3G\\AgencyPack\\Blog\\ViewHelpers';

// Register page layout hooks to display additional information for posts.
if (!(bool)$blogConfiguration['disablePageLayoutHeader']) {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'][]
= \T3G\AgencyPack\Blog\Hooks\PageLayoutHeaderHook::class . '->drawHeader';
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['recordlist/Modules/Recordlist/index.php']['drawHeaderHook'][]
= \T3G\AgencyPack\Blog\Hooks\PageLayoutHeaderHook::class . '->drawHeader';
}
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'][]
= \T3G\AgencyPack\Blog\Hooks\PageLayoutHeaderHook::class . '->drawHeader';
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['recordlist/Modules/Recordlist/index.php']['drawHeaderHook'][]
= \T3G\AgencyPack\Blog\Hooks\PageLayoutHeaderHook::class . '->drawHeader';

// Register new form data provider
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][\T3G\AgencyPack\Blog\Backend\FormDataProvider\CategoryDefaultValueProvider::class] = [
Expand Down

0 comments on commit 1fbde27

Please sign in to comment.