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

[FEATURE] Support re-definition of File Reference types #314

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions Classes/Builder/ContentBlockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,26 @@ public function create(LoadedContentBlock $contentBlock, string $skeletonPath =
$this->initializeRegistries($contentBlock);

// Create base directories for a Content Block.
$contentType = $contentBlock->getContentType();
$assetsPath = $basePath . '/' . ContentBlockPathUtility::getAssetsFolder();
$templatePath = $basePath . '/' . ContentBlockPathUtility::getTemplatesFolder();
$languagePath = $basePath . '/' . ContentBlockPathUtility::getLanguageFolder();
GeneralUtility::mkdir_deep($assetsPath);
GeneralUtility::mkdir_deep($templatePath);
if ($contentType !== ContentType::FILE_TYPE) {
GeneralUtility::mkdir_deep($assetsPath);
GeneralUtility::mkdir_deep($templatePath);
}
GeneralUtility::mkdir_deep($languagePath);

$this->createLabelsXlf($contentBlock, $basePath);
$this->createConfigYaml($contentBlock, $basePath);

$contentType = $contentBlock->getContentType();
if ($contentType === ContentType::CONTENT_ELEMENT) {
$this->createFrontendHtml($contentBlock, $basePath);
$this->createBackendPreviewHtml($contentBlock, $basePath);
}
$this->copyDefaultIcon($contentType, $basePath);
if ($contentType !== ContentType::FILE_TYPE) {
$this->copyDefaultIcon($contentType, $basePath);
}
if ($contentType === ContentType::PAGE_TYPE) {
$this->copyHideInMenuIcon($basePath);
$this->createBackendPreviewHtml($contentBlock, $basePath);
Expand All @@ -107,7 +111,7 @@ protected function createConfigYaml(LoadedContentBlock $contentBlock, string $ba
$yamlContent = $contentBlock->getYaml();
unset($yamlContent['title']);
unset($yamlContent['description']);
if ($contentType === ContentType::CONTENT_ELEMENT || $contentType === ContentType::PAGE_TYPE) {
if ($contentType !== ContentType::RECORD_TYPE) {
unset($yamlContent['table']);
unset($yamlContent['typeField']);
}
Expand Down
80 changes: 76 additions & 4 deletions Classes/Command/CreateContentBlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Package\PackageInterface;
use TYPO3\CMS\Core\Resource\FileType;
use TYPO3\CMS\Core\Utility\GeneralUtility;

#[Autoconfigure(tags: [
Expand Down Expand Up @@ -181,6 +182,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
PageTypeNameValidator::validate($typeName, $vendor . '/' . $name);
$typeName = (int)$typeName;
}
if ($contentType === ContentType::FILE_TYPE) {
$choices = ['text', 'image', 'audio', 'video', 'application'];
if ($typeName === null) {
$whatIsTheTypeName = new ChoiceQuestion('Choose a file type.', $choices, 'image');
$typeName = $io->askQuestion($whatIsTheTypeName);
}
$result = FileType::tryFromMimeType($typeName);
if ($result === FileType::UNKNOWN) {
throw new \InvalidArgumentException(
'Please choose a valid file type. Valid types are: ' . implode(', ', $choices),
1734180384
);
}
}

$contentBlockName = $vendor . '/' . $name;
if ($this->contentBlockRegistry->hasContentBlock($contentBlockName)) {
Expand All @@ -191,9 +206,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::INVALID;
}

if ($input->getOption('title')) {
$title = $input->getOption('title');
} else {
$title = $input->getOption('title');
if ($contentType !== ContentType::FILE_TYPE && $title === null) {
$defaultTitle = $vendor . '/' . $name;
$question = new Question('Define title', $defaultTitle);
$title = $io->askQuestion($question);
Expand All @@ -203,6 +217,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
ContentType::CONTENT_ELEMENT => $this->createContentBlockContentElementConfiguration($vendor, $name, $title, $typeName),
ContentType::PAGE_TYPE => $this->createContentBlockPageTypeConfiguration($vendor, $name, $title, $typeName),
ContentType::RECORD_TYPE => $this->createContentBlockRecordTypeConfiguration($vendor, $name, $title, $typeName),
ContentType::FILE_TYPE => $this->createContentBlockFileTypeConfiguration($vendor, $name, $typeName),
};

if ($input->getOption('extension')) {
Expand Down Expand Up @@ -297,7 +312,8 @@ protected function getExtPath(string $extension, ContentType $contentType): stri
return match ($contentType) {
ContentType::CONTENT_ELEMENT => $base . ContentBlockPathUtility::getRelativeContentElementsPath(),
ContentType::PAGE_TYPE => $base . ContentBlockPathUtility::getRelativePageTypesPath(),
ContentType::RECORD_TYPE => $base . ContentBlockPathUtility::getRelativeRecordTypesPath()
ContentType::RECORD_TYPE => $base . ContentBlockPathUtility::getRelativeRecordTypesPath(),
ContentType::FILE_TYPE => $base . ContentBlockPathUtility::getRelativeFileTypesPath(),
};
}

Expand Down Expand Up @@ -415,4 +431,60 @@ private function createContentBlockRecordTypeConfiguration(string $vendor, strin
];
return $configuration;
}

private function createContentBlockFileTypeConfiguration(string $vendor, string $name, ?string $typeName = ''): array
{
$fullName = $vendor . '/' . $name;
$configuration = [
'name' => $fullName,
'table' => 'sys_file_reference',
];
if ($typeName !== '' && $typeName !== null) {
$configuration['typeName'] = $typeName;
}
$configuration['fields'] = [
[
'identifier' => 'image_overlay_palette',
'type' => 'Palette',
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_file_reference.imageoverlayPalette',
'fields' => [
[
'identifier' => 'alternative',
'useExistingField' => true,
],
[
'identifier' => 'description',
'useExistingField' => true,
],
[
'type' => 'Linebreak',
],
[
'identifier' => 'link',
'useExistingField' => true,
],
[
'identifier' => 'title',
'useExistingField' => true,
],
[
'type' => 'Linebreak',
],
[
'identifier' => 'example_custom_field',
'type' => 'Text',
'label' => 'My custom Field',
],
[
'type' => 'Linebreak',
],
[
'identifier' => 'crop',
'useExistingField' => true,
],
],
],
];
return $configuration;
}
}
2 changes: 1 addition & 1 deletion Classes/Command/ListContentBlocksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ protected function getAvailableContentBlocks(): array
$list = [];
foreach ($this->contentBlockRegistry->getAll() as $loadedContentBlock) {
$table = match ($loadedContentBlock->getContentType()) {
ContentType::CONTENT_ELEMENT, ContentType::PAGE_TYPE => $loadedContentBlock->getContentType()->getTable(),
ContentType::RECORD_TYPE => $loadedContentBlock->getYaml()['table'],
default => $loadedContentBlock->getContentType()->getTable(),
};
$typeName = $loadedContentBlock->getYaml()['typeName'];
$list[] = [
Expand Down
8 changes: 6 additions & 2 deletions Classes/Definition/ContentType/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ enum ContentType: string
{
case CONTENT_ELEMENT = 'content-element';
case PAGE_TYPE = 'page-type';
case FILE_TYPE = 'file-type';
case RECORD_TYPE = 'record-type';

public function getTable(): ?string
{
return match ($this) {
self::CONTENT_ELEMENT => 'tt_content',
self::PAGE_TYPE => 'pages',
self::FILE_TYPE => 'sys_file_reference',
self::RECORD_TYPE => null,
};
}
Expand All @@ -40,7 +42,7 @@ public function getTypeField(): ?string
return match ($this) {
self::CONTENT_ELEMENT => 'CType',
self::PAGE_TYPE => 'doktype',
self::RECORD_TYPE => null,
self::FILE_TYPE, self::RECORD_TYPE => null,
};
}

Expand All @@ -49,6 +51,7 @@ public static function getByTable(string $table): self
return match ($table) {
'tt_content' => self::CONTENT_ELEMENT,
'pages' => self::PAGE_TYPE,
'sys_file_reference' => self::FILE_TYPE,
default => self::RECORD_TYPE,
};
}
Expand All @@ -58,6 +61,7 @@ public function getHumanReadable(): string
return match ($this) {
self::CONTENT_ELEMENT => 'Content Element',
self::PAGE_TYPE => 'Page Type',
self::FILE_TYPE => 'File Type',
self::RECORD_TYPE => 'Record Type',
};
}
Expand All @@ -66,7 +70,7 @@ public function getDefaultGroup(): ?string
{
return match ($this) {
self::CONTENT_ELEMENT, self::PAGE_TYPE => 'default',
self::RECORD_TYPE => null,
self::FILE_TYPE, self::RECORD_TYPE => null,
};
}
}
4 changes: 3 additions & 1 deletion Classes/Definition/Factory/ContentTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function create(array $typeDefinition, string $table): ContentTypeInterfa
return match ($contentType) {
ContentType::CONTENT_ELEMENT => ContentElementDefinition::createFromArray($typeDefinition, $table),
ContentType::PAGE_TYPE => PageTypeDefinition::createFromArray($typeDefinition, $table),
ContentType::RECORD_TYPE => RecordTypeDefinition::createFromArray($typeDefinition, $table)
// @todo It's not ideal that FileType reuses RecordTypeDefinition.
// @todo It actually only needs showItems, overrideColumns and typeName. Create new interface?
ContentType::FILE_TYPE, ContentType::RECORD_TYPE => RecordTypeDefinition::createFromArray($typeDefinition, $table)
};
}
}
9 changes: 8 additions & 1 deletion Classes/Definition/Factory/Processing/ProcessingInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ private function getTypeFieldNative(SimpleTcaSchemaFactory $simpleTcaSchemaFacto

private function resolveTypeName(): string|int
{
if (array_key_exists('typeName', $this->yaml)) {
return $this->yaml['typeName'];
}
if ($this->typeField === null) {
return '1';
}
return $this->yaml['typeName'];
// This should never happen.
throw new \InvalidArgumentException(
'Unexpected exception: Missing "typeName" detected.',
1734179318
);
}
}
60 changes: 47 additions & 13 deletions Classes/Generator/TcaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use TYPO3\CMS\ContentBlocks\Definition\ContentType\ContentType;
use TYPO3\CMS\ContentBlocks\Definition\ContentType\ContentTypeInterface;
use TYPO3\CMS\ContentBlocks\Definition\ContentType\PageTypeDefinition;
use TYPO3\CMS\ContentBlocks\Definition\ContentType\RecordTypeDefinition;
use TYPO3\CMS\ContentBlocks\Definition\PaletteDefinition;
use TYPO3\CMS\ContentBlocks\Definition\TableDefinition;
use TYPO3\CMS\ContentBlocks\Definition\TableDefinitionCollection;
Expand Down Expand Up @@ -152,7 +151,7 @@ protected function generateTableTca(TableDefinition $tableDefinition, array $bas
}
$fieldType->setDataStructure($dataStructure);
}
if ($tableDefinition->hasTypeField()) {
if ($tableDefinition->hasTypeField() || $tableDefinition->getContentType() === ContentType::FILE_TYPE) {
$tca['columns'][$column->getUniqueIdentifier()] = $this->getColumnTcaForTableWithTypeField($tableDefinition, $column, $baseTca);
// Ensure label exists for the standard column definition. This is used e.g. in the List module.
if (!$column->useExistingField()) {
Expand Down Expand Up @@ -300,20 +299,23 @@ protected function fillTypeFieldSelectItems(): void
protected function processTypeDefinition(ContentTypeInterface $typeDefinition, TableDefinition $tableDefinition): array
{
$columnsOverrides = $this->getColumnsOverrides($typeDefinition, $tableDefinition);
$tca = match ($typeDefinition::class) {
ContentElementDefinition::class => $this->processContentElement($typeDefinition, $columnsOverrides),
PageTypeDefinition::class => $this->processPageType($typeDefinition, $columnsOverrides),
RecordTypeDefinition::class => $this->processRecordType($typeDefinition, $columnsOverrides, $tableDefinition),
default => throw new \InvalidArgumentException(
'Unsupported type definition: ' . $typeDefinition::class,
1714050376
),
$tca = match ($tableDefinition->getContentType()) {
ContentType::CONTENT_ELEMENT => $this->processContentElement($typeDefinition, $columnsOverrides),
ContentType::PAGE_TYPE => $this->processPageType($typeDefinition, $columnsOverrides),
ContentType::FILE_TYPE => $this->processFileType($typeDefinition, $columnsOverrides),
ContentType::RECORD_TYPE => $this->processRecordType($typeDefinition, $columnsOverrides, $tableDefinition),
};
return $tca;
}

protected function processContentElement(ContentElementDefinition $typeDefinition, array $columnsOverrides): array
protected function processContentElement(ContentTypeInterface $typeDefinition, array $columnsOverrides): array
{
if (!$typeDefinition instanceof ContentElementDefinition) {
throw new \InvalidArgumentException(
'Expected ContentElementDefinition, got ' . get_class($typeDefinition),
1733344806
);
}
$typeDefinitionArray = [
'previewRenderer' => PreviewRenderer::class,
'showitem' => $this->getContentElementStandardShowItem($typeDefinition),
Expand All @@ -327,7 +329,7 @@ protected function processContentElement(ContentElementDefinition $typeDefinitio
return $typeDefinitionArray;
}

protected function processPageType(PageTypeDefinition $typeDefinition, array $columnsOverrides): array
protected function processPageType(ContentTypeInterface $typeDefinition, array $columnsOverrides): array
{
$typeDefinitionArray = [
'showitem' => $this->getPageTypeStandardShowItem($typeDefinition),
Expand All @@ -338,7 +340,18 @@ protected function processPageType(PageTypeDefinition $typeDefinition, array $co
return $typeDefinitionArray;
}

protected function processRecordType(RecordTypeDefinition $typeDefinition, array $columnsOverrides, TableDefinition $tableDefinition): array
protected function processFileType(ContentTypeInterface $typeDefinition, array $columnsOverrides): array
{
$typeDefinitionArray = [
'showitem' => $this->getFileTypeStandardShowItem($typeDefinition),
];
if ($columnsOverrides !== []) {
$typeDefinitionArray['columnsOverrides'] = $columnsOverrides;
}
return $typeDefinitionArray;
}

protected function processRecordType(ContentTypeInterface $typeDefinition, array $columnsOverrides, TableDefinition $tableDefinition): array
{
$typeDefinitionArray = [
'showitem' => $this->getRecordTypeStandardShowItem($typeDefinition, $tableDefinition),
Expand Down Expand Up @@ -775,6 +788,27 @@ protected function getRecordTypeStandardShowItem(ContentTypeInterface $typeDefin
return $showItem;
}

protected function getFileTypeStandardShowItem(ContentTypeInterface $typeDefinition): string
{
$showItemArray = $typeDefinition->getShowItems();
$firstItemIsTab = ($showItemArray[0] ?? null) instanceof TabDefinition;
$generalTab = '--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general';
if ($firstItemIsTab) {
$tabDefinition = array_shift($showItemArray);
$generalTab = $this->processShowItem([$tabDefinition]);
}
$showItem = $this->processShowItem($showItemArray);
$parts = [];
$parts[] = $generalTab;
if ($showItem !== '') {
$parts[] = $showItem;
}
// Add hidden palette with system fields and uid_local.
$parts[] = '--palette--;;filePalette';
$showItem = implode(',', $parts);
return $showItem;
}

protected function buildSystemFields(SystemFieldPalettesInterface $capability): array
{
$parts = [];
Expand Down
Loading
Loading