Skip to content

Commit

Permalink
feat: add page config
Browse files Browse the repository at this point in the history
REHLTYEXT-82
  • Loading branch information
dvdmlln committed Feb 13, 2024
1 parent 61fb2ea commit f2c6da4
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Classes/Utility/ConfigUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Remind\Headless\Utility;

use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ConfigUtility
{
public static function getConfig(): array
{
/** @var \TYPO3\CMS\Core\Site\Entity\Site $site */
$site = self::getRequest()->getAttribute('site');
$rootPageId = $site->getRootPageId();

$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('pages');
$flexFormStr = $queryBuilder
->select('tx_headless_config')
->from('pages')
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter($rootPageId, Connection::PARAM_INT)
)
)
->executeQuery()
->fetchOne();

$flexFormService = GeneralUtility::makeInstance(FlexFormService::class);
return $flexFormService->convertFlexFormContentToArray($flexFormStr);
}

/**
* @param array|string $dataStructure either a xml flexform file path, a xml flexform string or a flexform array
*/
public static function addFlexForm(array|string $dataStructure): void
{
$newFlexFormArray = self::getFlexFormArray($dataStructure);
$currentFlexFormArray = self::getFlexFormArray(
$GLOBALS['TCA']['pages']['columns']['tx_headless_config']['config']['ds']['default']
);

if (($currentFlexFormArray['ROOT']['el'] ?? null) === '') {
$currentFlexFormArray = [];
}

ArrayUtility::mergeRecursiveWithOverrule($currentFlexFormArray, $newFlexFormArray);
$flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
$newFlexFormString = $flexFormTools->flexArray2Xml($currentFlexFormArray, true);

$GLOBALS['TCA']['pages']['columns']['tx_headless_config']['config']['ds']['default'] = $newFlexFormString;
}

private static function getFlexFormArray(array|string $dataStructure): array
{
if (is_array($dataStructure)) {
return $dataStructure;
}
// Taken from TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
if (strpos(trim($dataStructure), 'FILE:') === 0) {
$file = GeneralUtility::getFileAbsFileName(substr(trim($dataStructure), 5));
if (empty($file) || !@is_file($file)) {
throw new RuntimeException(
'Data structure file ' . $file . ' could not be resolved to an existing file',
1478105826
);
}
$dataStructure = (string) file_get_contents($file);
}
return GeneralUtility::xml2arrayProcess($dataStructure);
}

private static function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}
6 changes: 6 additions & 0 deletions Configuration/FlexForms/Empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<T3DataStructure>
<ROOT>
<el></el>
</ROOT>
</T3DataStructure>
15 changes: 15 additions & 0 deletions Configuration/TCA/Overrides/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
'default' => null,
],
],
'tx_headless_config' => [
'label' => 'LLL:EXT:rmnd_headless/Resources/Private/Language/locallang_pages.xlf:page_config',
'config' => [
'type' => 'flex',
'ds' => [
'default' => 'FILE:EXT:rmnd_headless/Configuration/FlexForms/Empty.xml',
],
],
'displayCond' => 'FIELD:is_siteroot:REQ:true',
],
'tx_headless_overview_label' => [
'exclude' => 0,
'label' => 'LLL:EXT:rmnd_headless/Resources/Private/Language/locallang_pages.xlf:overview_label',
Expand All @@ -43,3 +53,8 @@
'title',
'--linebreak--,tx_headless_overview_label',
);

ExtensionManagementUtility::addToAllTCAtypes(
'pages',
'tx_headless_config',
);
3 changes: 3 additions & 0 deletions Resources/Private/Language/de.locallang_pages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<trans-unit id="breadcrumbs_background_color.none">
<target>Keine</target>
</trans-unit>
<trans-unit id="page_config">
<target>Page Config</target>
</trans-unit>
<trans-unit id="overview_label">
<target>Übersicht Beschriftung</target>
</trans-unit>
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang_pages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<trans-unit id="breadcrumbs_background_color.none">
<source>None</source>
</trans-unit>
<trans-unit id="page_config">
<source>Page Config</source>
</trans-unit>
<trans-unit id="overview_label">
<source>Overview Label</source>
</trans-unit>
Expand Down
1 change: 1 addition & 0 deletions ext_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ CREATE TABLE tt_content (

CREATE TABLE pages (
tx_headless_breadcrumbs_background_color VARCHAR(60),
tx_headless_config mediumtext,
tx_headless_overview_label VARCHAR(60) DEFAULT '' NOT NULL,
);

Expand Down

0 comments on commit f2c6da4

Please sign in to comment.