ew_base/Classes/Form/FormDataProvider/UsercentricsDatabaseEditRow.php
2024-07-14 16:20:06 +02:00

68 lines
2.3 KiB
PHP
Executable File

<?php
declare(strict_types=1);
/*
* This file is developed by evoWeb.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Evoweb\EwBase\Form\FormDataProvider;
use TYPO3\CMS\Backend\Form\FormDataProvider\SiteDatabaseEditRow;
use TYPO3\CMS\Core\Configuration\SiteConfiguration;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
readonly class UsercentricsDatabaseEditRow extends SiteDatabaseEditRow
{
public function __construct(private SiteConfiguration $siteConfiguration)
{
parent::__construct($this->siteConfiguration);
}
/**
* First level of ['customData']['siteData'] to ['databaseRow']
*
* @throws \RuntimeException
*/
public function addData(array $result): array
{
if ($result['command'] !== 'edit' || $result['tableName'] !== 'site_usercentrics') {
return $result;
}
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class, $this->siteConfiguration);
$rootPageId = (int)($result['inlineTopMostParentUid'] ?? $result['inlineParentUid']);
try {
$rowData = $this->getRawConfigurationForSiteWithRootPageId($siteFinder, $rootPageId);
$parentFieldName = $result['inlineParentFieldName'];
if (!isset($rowData[$parentFieldName])) {
throw new \RuntimeException('Field "' . $parentFieldName . '" not found', 1520886092);
}
$rowData = $rowData[$parentFieldName][$result['vanillaUid']];
$result['databaseRow']['uid'] = $result['vanillaUid'];
} catch (SiteNotFoundException) {
$rowData = [];
}
foreach ($rowData as $fieldName => $value) {
// Flat values only - databaseRow has no "tree"
if (!is_array($value)) {
$result['databaseRow'][$fieldName] = $value;
}
}
// All "records" are always on pid 0
$result['databaseRow']['pid'] = 0;
return $result;
}
}