Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Fix order of fields and tabs defined for backend layouts #315

Merged
merged 1 commit into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Classes/CodeGenerator/AbstractCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ public function __construct(StorageRepository $storageRepository = null)
$this->storageRepository = $storageRepository;
}
}

/**
* @return StorageRepository
*/
public function getStorageRepository(): StorageRepository
{
return $this->storageRepository;
}
}
63 changes: 36 additions & 27 deletions Classes/CodeGenerator/TcaCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* ************************************************************* */

use Exception;
use MASK\Mask\Domain\Repository\StorageRepository;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -40,6 +41,20 @@
*/
class TcaCodeGenerator extends AbstractCodeGenerator
{
/**
* @var FieldHelper
*/
protected $fieldHelper;

public function __construct(StorageRepository $storageRepository = null, FieldHelper $fieldHelper = null)
{
parent::__construct($storageRepository);
if ($fieldHelper) {
$this->fieldHelper = $fieldHelper;
} else {
$this->fieldHelper = GeneralUtility::makeInstance(FieldHelper::class);
}
}

/**
* Generates and sets the correct tca for all the inline fields
Expand Down Expand Up @@ -151,36 +166,30 @@ public function setElementsTca($tca): void
}

/**
* Generates and sets the tca for all the extended pages
*
* @param array $tca
* @param string $key
* @return string
*/
public function setPageTca($tca): void
public function getPageTca(string $key)
{
$fieldHelper = GeneralUtility::makeInstance(FieldHelper::class);
$prependTabs = '--div--;Content-Fields,';
if ($tca) {
$i = 0;
foreach ($tca as $fieldKey => $config) {
// no information about which element this field is for at this point
$elements = $fieldHelper->getElementsWhichUseField($fieldKey, 'pages');
$element = $elements[0];

// check if this field is of type tab
$formType = $fieldHelper->getFormType($fieldKey, $element['key'], 'pages');
if ($formType === 'Tab') {
$label = $fieldHelper->getLabel($element['key'], $fieldKey, 'pages');
// if a tab is in the first position then change the name of the general tab
if ($i === 0) {
$prependTabs = '--div--;' . $label . ',';
} else {
// otherwise just add new tab
$fieldArray[] = '--div--;' . $label;
}
$prependTabs = ',--div--;Content-Fields,';
$tca = $this->storageRepository->load();
$columns = $tca['pages']['elements'][$key]['columns'] ?? [];
for ($i = 0; $i < count($columns); $i++) {
$fieldKey = $columns[$i];

// check if this field is of type tab
$formType = $this->fieldHelper->getFormType($fieldKey, $key, 'pages');
if ($formType === 'Tab') {
$label = $this->fieldHelper->getLabel($key, $fieldKey, 'pages');
// if a tab is in the first position then change the name of the general tab
if ($i === 0) {
$prependTabs = ',--div--;' . $label . ',';
} else {
$fieldArray[] = $fieldKey;
// otherwise just add new tab
$fieldArray[] = '--div--;' . $label;
}
$i++;
} else {
$fieldArray[] = $fieldKey;
}
}

Expand All @@ -189,7 +198,7 @@ public function setPageTca($tca): void
} else {
$pageFieldString = $prependTabs;
}
ExtensionManagementUtility::addToAllTCAtypes('pages', $pageFieldString);
return $pageFieldString;
}

/**
Expand Down
36 changes: 0 additions & 36 deletions Classes/CodeGenerator/TyposcriptCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,42 +99,6 @@ public function generateTsConfig($json): string
return $content;
}

/**
* Generates the typoscript for pages
* @param array $json
* @return string
*/
public function generatePageTyposcript($json): string
{
$pageColumns = [];
$disableColumns = '';
$pagesContent = '';
if ($json['pages']['elements']) {
foreach ($json['pages']['elements'] as $element) {
// Labels for pages
$pagesContent .= "\n[maskBeLayout('" . $element['key'] . "')]\n";
// if page has backendlayout with this element-key
if ($element['columns']) {
foreach ($element['columns'] as $index => $column) {
$pagesContent .= ' TCEFORM.pages.' . $column . '.label = ' . $element['labels'][$index] . "\n";
}
$pagesContent .= "\n";
foreach ($element['columns'] as $index => $column) {
$pageColumns[] = $column;
$pagesContent .= ' TCEFORM.pages.' . $column . ".disabled = 0\n";
}
}
$pagesContent .= "[end]\n";
}
}
// disable all fields by default and only activate by condition
foreach ($pageColumns as $column) {
$disableColumns .= 'TCEFORM.pages.' . $column . ".disabled = 1\n";
}
$pagesContent = $disableColumns . "\n" . $pagesContent;
return $pagesContent;
}

/**
* Generates the typoscript for the setup field
* @param array $configuration
Expand Down
8 changes: 6 additions & 2 deletions Classes/Domain/Repository/StorageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ class StorageRepository
/**
* is called before every action
*/
public function __construct()
public function __construct(SettingsService $settingsService = null)
{
$this->settingsService = GeneralUtility::makeInstance(SettingsService::class);
if ($settingsService) {
$this->settingsService = $settingsService;
} else {
$this->settingsService = GeneralUtility::makeInstance(SettingsService::class);
}
$this->extSettings = $this->settingsService->get();
}

Expand Down
2 changes: 0 additions & 2 deletions Classes/ExpressionLanguage/MaskFunctionsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

namespace MASK\Mask\ExpressionLanguage;

use Doctrine\DBAL\FetchMode;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace MASK\Mask\Form\FormDataProvider;

use MASK\Mask\CodeGenerator\TcaCodeGenerator;
use TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class TcaTypesShowitemMaskBeLayoutFields implements FormDataProviderInterface
{
public function addData(array $result)
{
$tcaCodeGenerator = GeneralUtility::makeInstance(TcaCodeGenerator::class);
$json = $tcaCodeGenerator->getStorageRepository()->load();
if ($json['pages']['elements'] ?? false) {
$conditionMatcher = GeneralUtility::makeInstance(ConditionMatcher::class, null, $result['vanillaUid'], $result['rootline']);
foreach ($json['pages']['elements'] as $element) {
$key = $element['key'];
if ($conditionMatcher->match("[maskBeLayout($key)]")) {
$result['processedTca']['types'][$result['recordTypeValue']]['showitem'] .= $tcaCodeGenerator->getPageTca($key);
break;
}
}
}
return $result;
}
}
14 changes: 7 additions & 7 deletions Classes/Helper/FieldHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ public function getFormType($fieldKey, $elementKey = '', $type = 'tt_content'):
}

// load tca for field from $GLOBALS
$tca = $GLOBALS['TCA'][$type]['columns'][$fieldKey];
if (!$tca['config']) {
$tca = $GLOBALS['TCA'][$type]['columns']['tx_mask_' . $fieldKey];
$tca = $GLOBALS['TCA'][$type]['columns'][$fieldKey] ?? [];
if (array_key_exists('config', $tca) && !$tca['config']) {
$tca = $GLOBALS['TCA'][$type]['columns']['tx_mask_' . $fieldKey] ?? [];
}
if (!$tca['config']) {
$tca = $element['tca'][$fieldKey];
if (array_key_exists('config', $tca) && !$tca['config']) {
$tca = $element['tca'][$fieldKey] ?? [];
}

// if field is in inline table or $GLOBALS["TCA"] is not yet filled, load tca from json
if ($tca === null || !in_array($type, ['tt_content', 'pages'])) {
if (!$tca || !in_array($type, ['tt_content', 'pages'])) {
$tca = $this->storageRepository->loadField($type, $fieldKey);
if (!$tca['config']) {
$tca = $this->storageRepository->loadField($type, 'tx_mask_' . $fieldKey);
Expand All @@ -152,7 +152,7 @@ public function getFormType($fieldKey, $elementKey = '', $type = 'tt_content'):
}


if ($tca['options'] === 'file') {
if (($tca['options'] ?? '') === 'file') {
$formType = 'File';
}

Expand Down
1 change: 0 additions & 1 deletion Configuration/TCA/Overrides/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// Generate TCA for Pages
$pagesColumns = $tcaCodeGenerator->generateFieldsTca($configuration['pages']['tca']);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('pages', $pagesColumns);
$tcaCodeGenerator->setPageTca($configuration['pages']['tca']);

// Generate TCA for Inline-Fields
$tcaCodeGenerator->setInlineTca($configuration);
Expand Down
Loading