Skip to content

Commit

Permalink
[WIP][!!!][TASK] New Content Block folder structure
Browse files Browse the repository at this point in the history
todo:
- Documentation
  • Loading branch information
nhovratov committed Sep 16, 2024
1 parent 5d37c15 commit eb1f798
Show file tree
Hide file tree
Showing 145 changed files with 378 additions and 231 deletions.
2 changes: 1 addition & 1 deletion Classes/Builder/ContentBlockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function createEditorInterfaceYaml(LoadedContentBlock $contentBlock, s

protected function createLabelsXlf(LoadedContentBlock $contentBlock, string $basePath): void
{
GeneralUtility::mkdir_deep($basePath . '/' . ContentBlockPathUtility::getLanguageFolderPath());
GeneralUtility::mkdir_deep($basePath . '/' . ContentBlockPathUtility::getLanguageFolder());
$xliffContent = $this->languageFileGenerator->generate($contentBlock);
GeneralUtility::writeFile(
$basePath . '/' . ContentBlockPathUtility::getLanguageFilePath(),
Expand Down
2 changes: 1 addition & 1 deletion Classes/Command/GenerateLanguageFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
protected function writeLabelsXlf(LoadedContentBlock $contentBlock): void
{
$contentBlockPath = GeneralUtility::getFileAbsFileName($contentBlock->getExtPath());
$labelsFolder = $contentBlockPath . '/' . ContentBlockPathUtility::getLanguageFolderPath();
$labelsFolder = $contentBlockPath . '/' . ContentBlockPathUtility::getLanguageFolder();
$labelsXlfPath = $contentBlockPath . '/' . ContentBlockPathUtility::getLanguageFilePath();
$result = $this->languageFileGenerator->generate($contentBlock);
GeneralUtility::mkdir_deep($labelsFolder);
Expand Down
10 changes: 5 additions & 5 deletions Classes/Loader/ContentBlockLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ protected function loadContentBlocksInExtension(string $path, string $extensionK
$contentBlockFolderName = $splFileInfo->getRelativePathname();
$contentBlockExtPath = ContentBlockPathUtility::getContentBlockExtPath($extensionKey, $contentBlockFolderName, $contentType);
$editorInterfaceYaml = $this->parseEditorInterfaceYaml($absoluteContentBlockPath, $contentBlockExtPath, $contentType);
if ($editorInterfaceYaml === null) {
continue;
}
$result[] = $this->loadSingleContentBlock(
$editorInterfaceYaml['name'],
$contentType,
Expand All @@ -157,15 +160,12 @@ protected function loadContentBlocksInExtension(string $path, string $extensionK
return $result;
}

protected function parseEditorInterfaceYaml(string $absoluteContentBlockPath, string $contentBlockExtPath, ContentType $contentType): array
protected function parseEditorInterfaceYaml(string $absoluteContentBlockPath, string $contentBlockExtPath, ContentType $contentType): ?array
{
$contentBlockDefinitionFileName = ContentBlockPathUtility::getContentBlockDefinitionFileName();
$yamlPath = $absoluteContentBlockPath . '/' . $contentBlockDefinitionFileName;
if (!file_exists($yamlPath)) {
throw new \RuntimeException(
'Found Content Block folder in "' . $contentBlockExtPath . '" but ' . $contentBlockDefinitionFileName . ' is missing.',
1711039210
);
return null;
}
$editorInterfaceYaml = Yaml::parseFile($yamlPath);
if (!is_array($editorInterfaceYaml) || strlen($editorInterfaceYaml['name'] ?? '') < 3 || !str_contains($editorInterfaceYaml['name'], '/')) {
Expand Down
11 changes: 4 additions & 7 deletions Classes/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,16 @@ public static function getContentBlockTypoScript(ContainerInterface $container):
if ($tableDefinition->getContentType() === ContentType::CONTENT_ELEMENT) {
$extPath = $contentBlockRegistry->getContentBlockExtPath($typeDefinition->getName());
$privatePath = $extPath . '/' . ContentBlockPathUtility::getPrivateFolder();
$template = ContentBlockPathUtility::getFrontendTemplateFileNameWithoutExtension();
$template = ContentBlockPathUtility::getFrontendTemplateFileName();
$typoScript = <<<HEREDOC
tt_content.{$typeDefinition->getTypeName()} =< lib.contentBlock
tt_content.{$typeDefinition->getTypeName()} {
templateName = {$template}
templateRootPaths {
20 = $privatePath/
}
file = $privatePath/$template
partialRootPaths {
20 = $privatePath/Partials/
20 = $privatePath/partials/
}
layoutRootPaths {
20 = $privatePath/Layouts/
20 = $privatePath/layouts/
}
}
HEREDOC;
Expand Down
149 changes: 149 additions & 0 deletions Classes/Update/ContentBlockFolderStructureMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* 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.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\ContentBlocks\Update;

use Symfony\Component\Finder\Finder;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Install\Attribute\UpgradeWizard;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

#[UpgradeWizard('contentBlocksFolderStructureMigration')]
class ContentBlockFolderStructureMigration implements UpgradeWizardInterface
{
public function __construct(
protected readonly PackageManager $packageManager,
) {}

public function getTitle(): string
{
return 'Content Block folder structure migration';
}

public function getDescription(): string
{
return 'Migrates to the new lowercase folder structure for single Content Blocks.';
}

public function executeUpdate(): bool
{
foreach ($this->findContentBlockPaths() as $path) {
$this->migrate($path);
}
return true;
}

public function updateNecessary(): bool
{
return $this->findContentBlockPaths() !== [];
}

public function getPrerequisites(): array
{
return [];
}

/**
* @return string[]
*/
protected function findContentBlockPaths(): array
{
$contentBlockPaths = [];
foreach ($this->packageManager->getActivePackages() as $package) {
$contentElementsFolder = $package->getPackagePath() . 'ContentBlocks/ContentElements';
$pageTypesFolder = $package->getPackagePath() . 'ContentBlocks/PageTypes';
$recordTypesFolder = $package->getPackagePath() . 'ContentBlocks/RecordTypes';
if (is_dir($contentElementsFolder)) {
$contentBlockPaths[] = $this->loadContentBlocksInExtension($contentElementsFolder);
}
if (is_dir($pageTypesFolder)) {
$contentBlockPaths[] = $this->loadContentBlocksInExtension($pageTypesFolder);
}
if (is_dir($recordTypesFolder)) {
$contentBlockPaths[] = $this->loadContentBlocksInExtension($recordTypesFolder);
}
}
$contentBlockPaths = array_merge(...$contentBlockPaths);
return $contentBlockPaths;
}

/**
* @return string[]
*/
protected function loadContentBlocksInExtension(string $path): array
{
$result = [];
$finder = new Finder();
$finder->directories()->depth(0)->in($path);
foreach ($finder as $splFileInfo) {
$absoluteContentBlockPath = $splFileInfo->getPathname();
if (file_exists($absoluteContentBlockPath . '/EditorInterface.yaml')) {
$result[] = $absoluteContentBlockPath;
}
}
return $result;
}

protected function migrate(string $path): void
{
rename($path . '/EditorInterface.yaml', $path . '/config.yaml');
if (file_exists($path . '/Source')) {
rename($path . '/Source', $path . '/templates');
}
if (file_exists($path . '/templates/Frontend.html')) {
rename($path . '/templates/Frontend.html', $path . '/templates/frontend.html');
}
if (file_exists($path . '/templates/EditorPreview.html')) {
rename($path . '/templates/EditorPreview.html', $path . '/templates/preview.html');
}
if (file_exists($path . '/templates/Language')) {
rename($path . '/templates/Language', $path . '/language');
}
if (file_exists($path . '/language/Labels.xlf')) {
rename($path . '/language/Labels.xlf', $path . '/language/labels.xlf');
$this->migrateLocalizedLabelFiles($path . '/language');
}
if (file_exists($path . '/Assets')) {
rename($path . '/Assets', $path . '/assets');
}
if (file_exists($path . '/assets/Icon.svg')) {
rename($path . '/assets/Icon.svg', $path . '/assets/icon.svg');
}
if (file_exists($path . '/assets/IconHideInMenu.svg')) {
rename($path . '/assets/IconHideInMenu.svg', $path . '/assets/icon-hide-in-menu.svg');
}
if (file_exists($path . '/assets/IconRoot.svg')) {
rename($path . '/assets/IconRoot.svg', $path . '/assets/icon-root.svg');
}
}

protected function migrateLocalizedLabelFiles(string $languagePath): void
{
$finder = new Finder();
$finder->files()->name('*.xlf')->in($languagePath);
foreach ($finder as $splFileInfo) {
$fileName = $splFileInfo->getFilename();
$parts = explode('.', $fileName);
if (count($parts) !== 3) {
continue;
}
$parts[1] = 'labels';
$newFileName = implode('.', $parts);
rename($splFileInfo->getPathname(), $languagePath . '/' . $newFileName);
}
}
}
28 changes: 14 additions & 14 deletions Classes/Utility/ContentBlockPathUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public static function getContentBlockExtPath(string $extensionKey, string $cont

public static function getContentBlockDefinitionFileName(): string
{
return 'EditorInterface.yaml';
return 'config.yaml';
}

public static function getBackendPreviewFileName(): string
{
return 'EditorPreview.html';
return 'preview.html';
}

public static function getBackendPreviewFileNameWithoutExtension(): string
Expand All @@ -57,7 +57,7 @@ public static function getBackendPreviewPath(): string

public static function getFrontendTemplateFileName(): string
{
return 'Frontend.html';
return 'frontend.html';
}

public static function getFrontendTemplateFileNameWithoutExtension(): string
Expand All @@ -70,29 +70,24 @@ public static function getFrontendTemplatePath(): string
return self::getPrivateFolder() . '/' . self::getFrontendTemplateFileName();
}

public static function getLanguageFolderPath(): string
{
return self::getPrivateFolder() . '/Language';
}

public static function getLanguageFilePath(): string
{
return self::getLanguageFolderPath() . '/Labels.xlf';
return self::getLanguageFolder() . '/labels.xlf';
}

public static function getIconNameWithoutFileExtension(): string
{
return 'Icon';
return 'icon';
}

public static function getIconHideInMenuNameWithoutFileExtension(): string
{
return 'IconHideInMenu';
return 'icon-hide-in-menu';
}

public static function getIconRootNameWithoutFileExtension(): string
{
return 'IconRoot';
return 'icon-root';
}

public static function getIconPathWithoutFileExtension(): string
Expand Down Expand Up @@ -142,12 +137,17 @@ public static function getRecordTypesFolder(): string

public static function getPublicFolder(): string
{
return 'Assets';
return 'assets';
}

public static function getPrivateFolder(): string
{
return 'Source';
return 'templates';
}

public static function getLanguageFolder(): string
{
return 'language';
}

public static function getHostExtPublicContentBlockBasePath(string $hostExtension): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ <h1>{data.header}</h1>
<p>creationDate:{data.systemProperties.createdAt.timestamp}</p>
<p>updateDate:{data.systemProperties.lastUpdatedAt.timestamp}</p>
<span><f:translate key="{cb:languagePath()}:title"/></span>
<img src="{f:uri.resource(path: '{cb:assetPath()}/Icon.svg')}" alt="">
<img src="{f:uri.resource(path: '{cb:assetPath()}/Icon.svg', absolute: '1')}" alt="">
<img src="{f:uri.resource(path: '{cb:assetPath()}/icon.svg')}" alt="">
<img src="{f:uri.resource(path: '{cb:assetPath()}/icon.svg', absolute: '1')}" alt="">
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public function variablesAndAssetsRendered(): void
// @todo The path should be absolute. See: https://github.com/TYPO3/testing-framework/issues/577
self::assertStringContainsString('<link href="typo3conf/ext/test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple/Frontend.css', $html);
self::assertStringContainsString('<script src="typo3conf/ext/test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple/Frontend.js', $html);
self::assertStringContainsString('<img src="typo3conf/ext/test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple/Icon.svg', $html);
self::assertStringContainsString('<img src="http://localhost/typo3conf/ext/test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple/Icon.svg', $html);
self::assertStringContainsString('<img src="typo3conf/ext/test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple/icon.svg', $html);
self::assertStringContainsString('<img src="http://localhost/typo3conf/ext/test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple/icon.svg', $html);
}

#[Test]
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/Generator/IconGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function registeredContentBlockIconsHaveCorrectConfigurationDataPr
[
'provider' => SvgIconProvider::class,
'options' => [
'source' => 'EXT:test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple/Icon.svg',
'source' => 'EXT:test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple/icon.svg',
],
],
];
Expand All @@ -62,7 +62,7 @@ public static function registeredContentBlockIconsHaveCorrectConfigurationDataPr
[
'provider' => SvgIconProvider::class,
'options' => [
'source' => 'EXT:test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple2/Icon.svg',
'source' => 'EXT:test_content_blocks_c/Resources/Public/ContentBlocks/simple/simple2/icon.svg',
],
],
];
Expand All @@ -71,7 +71,7 @@ public static function registeredContentBlockIconsHaveCorrectConfigurationDataPr
[
'provider' => SvgIconProvider::class,
'options' => [
'source' => 'EXT:test_content_blocks_c/Resources/Public/ContentBlocks/simple/basics/Icon.svg',
'source' => 'EXT:test_content_blocks_c/Resources/Public/ContentBlocks/simple/basics/icon.svg',
],
],
];
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/Generator/TcaGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function coreLabelsAreNotOverriddenIfMissingInLanguageFile(): void
public function coreLabelsAreOverriddenIfTranslationExistsInLanguageFile(): void
{
self::assertSame(
'LLL:EXT:test_content_blocks_c/ContentBlocks/ContentElements/simple2/Source/Language/Labels.xlf:header.label',
'LLL:EXT:test_content_blocks_c/ContentBlocks/ContentElements/simple2/language/labels.xlf:header.label',
$GLOBALS['TCA']['tt_content']['types']['simple_simple2']['columnsOverrides']['header']['label']
);
}
Expand Down Expand Up @@ -138,11 +138,11 @@ public function typeFieldSelectAddedForRecordType(): void
self::assertSame(
[
[
'label' => 'LLL:EXT:test_content_blocks_c/ContentBlocks/RecordTypes/record1/Source/Language/Labels.xlf:title',
'label' => 'LLL:EXT:test_content_blocks_c/ContentBlocks/RecordTypes/record1/language/labels.xlf:title',
'value' => 'record1',
'icon' => 'custom_record-record1-cc2849f',
'group' => null,
'description' => 'LLL:EXT:test_content_blocks_c/ContentBlocks/RecordTypes/record1/Source/Language/Labels.xlf:description',
'description' => 'LLL:EXT:test_content_blocks_c/ContentBlocks/RecordTypes/record1/language/labels.xlf:description',
],
[
'label' => 'content-blocks/record2',
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/ViewHelpers/LanguagePathViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public static function renderReturnsStringDataProvider(): array
return [
'name explicitly set' => [
'<cb:languagePath name="typo3tests/content-element-b" />',
'LLL:EXT:test_content_blocks_b/ContentBlocks/ContentElements/content-element-b/Source/Language/Labels.xlf',
'LLL:EXT:test_content_blocks_b/ContentBlocks/ContentElements/content-element-b/language/labels.xlf',
],
'fallback to name from context' => [
'<cb:languagePath />',
'LLL:EXT:test_content_blocks_b/ContentBlocks/ContentElements/content-element-b/Source/Language/Labels.xlf',
'LLL:EXT:test_content_blocks_b/ContentBlocks/ContentElements/content-element-b/language/labels.xlf',
],
];
}
Expand Down
Loading

0 comments on commit eb1f798

Please sign in to comment.