ds-site/Classes/EventListener/PageLayoutHeader.php

97 lines
3.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/*
* Copyright (C) 2019 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
2022-12-06 08:18:42 +01:00
namespace DanielSiepmann\DsSite\EventListener;
2022-12-06 08:18:42 +01:00
use TYPO3\CMS\Backend\Controller\Event\ModifyPageLayoutContentEvent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Frontend\Resource\FileCollector;
class PageLayoutHeader
{
2024-02-08 08:04:56 +01:00
private function getView(): StandaloneView
{
return GeneralUtility::makeInstance(StandaloneView::class);
}
private function resolvePageMedia(int $pageUid): array
{
$files = GeneralUtility::makeInstance(FileCollector::class);
$files->addFilesFromRelation('pages', 'media', ['uid' => $pageUid]);
return $files->getFiles();
}
2022-12-06 08:18:42 +01:00
public function __invoke(ModifyPageLayoutContentEvent $event): void
{
$request = $event->getRequest();
$pageinfo = BackendUtility::readPageAccess((int) ($request->getQueryParams()['id'] ?? 0), '');
2020-01-10 15:50:09 +01:00
// TODO: Check whether two levels up is uid 2, which holds all blog posts
// To prevent rendering on non blog posts
// Maybe we have rootline ?
$view = $this->getView();
2022-12-06 08:18:42 +01:00
$view->setRequest($request);
$view->assignMultiple([
2022-12-06 08:18:42 +01:00
'record' => $pageinfo,
'metaInfo' => [
[
'label' => 'meta-description',
2022-12-06 08:18:42 +01:00
'value' => $pageinfo['description'] ?? '',
'field' => 'description',
'type' => 'string',
],
[
'label' => 'introduction',
2022-12-06 08:18:42 +01:00
'value' => $pageinfo['abstract'] ?? '',
'field' => 'abstract',
'type' => 'string',
],
[
'label' => 'published',
2022-12-06 08:18:42 +01:00
'value' => $pageinfo['lastUpdated'] ?? '',
'field' => 'lastUpdated',
'type' => 'date',
],
[
'label' => 'updated',
2022-12-06 08:18:42 +01:00
'value' => $pageinfo['SYS_LASTCHANGED'] ?? '',
'type' => 'date',
],
[
'label' => 'media',
2022-12-06 08:18:42 +01:00
'value' => $this->resolvePageMedia((int) $pageinfo['uid']),
'field' => 'media',
'type' => 'files',
],
],
]);
$view->setTemplatePathAndFilename('EXT:ds_site/Resources/Private/Templates/Backend/Page/MetaInfo.html');
2022-12-06 08:18:42 +01:00
$event->addHeaderContent($view->render());
}
}