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

[TASK] Improve TCA configuration #14

Merged
merged 11 commits into from
Jul 3, 2023
5 changes: 3 additions & 2 deletions Classes/Controller/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use RuntimeException;

/**
* Controller for rendering media.
Expand Down Expand Up @@ -94,7 +95,7 @@ private function renderMedia(Media $media): ResponseInterface
// We update the last changed register when a media record has changed because
// the content element will not get this information if no properties in the
// content element are changed.
$contentObject = $contentObject = $this->getCurrentContentObject();
$contentObject = $this->getCurrentContentObject();
$contentObject->lastChanged($media->getTstamp());

if ($mediaType->equals(MediaType::VIDEO)) {
Expand All @@ -105,7 +106,7 @@ private function renderMedia(Media $media): ResponseInterface
return (new ForwardResponse('audio'))->withArguments(['audio' => $media->getUid()]);
}

throw new \RuntimeException('An invalid media type is used.');
throw new RuntimeException('An invalid media type is used.'); // @codeCoverageIgnore
}

/**
Expand Down
2 changes: 2 additions & 0 deletions Classes/Domain/Model/Enumeration/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* Enumeration of allowed media types.
*
* @codeCoverageIgnore No code to test.
*/
class MediaType extends Enumeration
{
Expand Down
5 changes: 3 additions & 2 deletions Classes/Domain/Repository/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sto\Html5mediakit\Exception\MediaMissingException;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
use InvalidArgumentException;

/**
* Repository for retrieving media files from the database.
Expand Down Expand Up @@ -76,10 +77,10 @@ private function fetchMediaOrThrowMissingMediaException(QueryInterface $query):
private function validateParentRecordData($data): void
{
if (empty($data['parent_table'])) {
throw new \InvalidArgumentException('parent_table field is missing in content data.');
throw new InvalidArgumentException('parent_table field is missing in content data.');
}
if (empty($data['parent_record'])) {
throw new \InvalidArgumentException('parent_record field is missing in content data.');
throw new InvalidArgumentException('parent_record field is missing in content data.');
}
}
}
6 changes: 5 additions & 1 deletion Classes/Exception/MediaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Sto\Html5mediakit\Exception;

use RuntimeException;

/* *
* This script belongs to the TYPO3 Extension "html5mediakit". *
* *
Expand All @@ -16,7 +18,9 @@

/**
* Generic media related Exception. Translations should be available for the Exception code.
*
* @codeCoverageIgnore No code to test.
*/
class MediaException extends \RuntimeException
class MediaException extends RuntimeException
{
}
2 changes: 1 addition & 1 deletion Classes/Exception/MediaMissingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class MediaMissingException extends MediaException
{
public function __construct($message = '', $code = 0, \Exception $previous = null)
public function __construct($message = '', $code = 0, Exception $previous = null)
{
if ($message === '') {
$message = 'No media exists in the current content element.';
Expand Down
17 changes: 0 additions & 17 deletions Configuration/TCA/Overrides/sys_file_reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@

$lllPrefix = 'LLL:EXT:html5mediakit/Resources/Private/Language/locallang_db:sys_file_reference.';

$displayCondition = [
'AND' => [
'FIELD:tablenames:=:tx_html5mediakit_domain_model_media',
'FIELD:fieldname:=:tracks',
],
];

$additionalColumns = [
'tx_html5mediakit_track_kind' => [
'label' => $lllPrefix . 'tx_html5mediakit_track_kind',
Expand Down Expand Up @@ -43,7 +36,6 @@
],
'default' => 'subtitles',
],
'displayCond' => $displayCondition,
],
'tx_html5mediakit_track_label' => [
'label' => $lllPrefix . 'tx_html5mediakit_track_label',
Expand All @@ -53,7 +45,6 @@
'eval' => 'trim',
'size' => 10,
],
'displayCond' => $displayCondition,
],
'tx_html5mediakit_track_srclang' => [
'label' => $lllPrefix . 'tx_html5mediakit_track_srclang',
Expand All @@ -65,18 +56,10 @@
'max' => 2,
'size' => 10,
],
'displayCond' => $displayCondition,
],
];

ExtensionManagementUtility::addTCAcolumns(
'sys_file_reference',
$additionalColumns
);

ExtensionManagementUtility::addToAllTCAtypes(
'sys_file_reference',
'tx_html5mediakit_track_kind, tx_html5mediakit_track_label, tx_html5mediakit_track_srclang',
'',
'after:title',
);
1 change: 1 addition & 0 deletions Configuration/TCA/Overrides/tt_content.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
--palette--;;general,
--palette--;;headers,
--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.media,
tx_html5mediakit_media,
--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.appearance,
--palette--;;frames,
Expand Down
120 changes: 70 additions & 50 deletions Configuration/TCA/tx_html5mediakit_domain_model_media.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,58 @@
$languagePrefixCsh = 'LLL:EXT:html5mediakit/Resources/Private/Language/locallang_csh_media.xlf:';
$lllAddImageFileReference = 'LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:images.addFileReference';

$buildFileFieldConfig = function (array $allowedFileTypes) use ($languagePrefix) {
return [
$buildFileFieldConfig = function (
string|array $allowedFileTypes,
int $maxitems = 1,
string $showitem = '',
string $createNewRelationLinkTitle = '',
) use (
$languagePrefix
) {
$showitem .= ',--palette--;;filePalette';
$showitem = ltrim($showitem, ',');
$allowsMultipleFiles = $maxitems === 0 || $maxitems > 1;

if ($createNewRelationLinkTitle === '') {
$createNewRelationLinkTitle = $languagePrefix . 'choose_file';
}

$config = [
'type' => 'file',
'allowed' => $allowedFileTypes,
'appearance' => [
'createNewRelationLinkTitle' => $languagePrefix . 'choose_file',
'useSortable' => false,
'headerThumbnail' => [
'field' => '',
'width' => '0',
'height' => '0',
],
'showPossibleLocalizationRecords' => true,
'showAllLocalizationLink' => $allowsMultipleFiles,
'createNewRelationLinkTitle' => $createNewRelationLinkTitle,
'useSortable' => $allowsMultipleFiles,
'enabledControls' => [
'info' => false,
'new' => false,
'dragdrop' => false,
'sort' => false,
'hide' => false,
'info' => true,
'new' => $allowsMultipleFiles,
'dragdrop' => $allowsMultipleFiles,
'sort' => $allowsMultipleFiles,
'hide' => $allowsMultipleFiles,
'delete' => true,
'localize' => false,
'localize' => true,
],
],
'overrideChildTca' => [
'types' => [
'0' => ['showitem' => '--palette--;;filePalette'],
AbstractFile::FILETYPE_AUDIO => ['showitem' => '--palette--;;filePalette'],
AbstractFile::FILETYPE_VIDEO => ['showitem' => '--palette--;;filePalette'],
AbstractFile::FILETYPE_APPLICATION => ['showitem' => '--palette--;;filePalette'],
AbstractFile::FILETYPE_APPLICATION => ['showitem' => $showitem],
AbstractFile::FILETYPE_AUDIO => ['showitem' => $showitem],
AbstractFile::FILETYPE_IMAGE => ['showitem' => $showitem],
AbstractFile::FILETYPE_TEXT => ['showitem' => $showitem],
AbstractFile::FILETYPE_UNKNOWN => ['showitem' => $showitem],
AbstractFile::FILETYPE_VIDEO => ['showitem' => $showitem],
],
],
'maxitems' => 1,
'security' => ['ignorePageTypeRestriction' => true],
];

if ($maxitems > 0) {
$config['maxitems'] = $maxitems;
}

return $config;
};

return [
Expand Down Expand Up @@ -90,10 +109,11 @@
'tracks' => [
'label' => $languagePrefixColumn . 'tracks',
'description' => $languagePrefixCsh . 'tracks.description',
'config' => [
'type' => 'file',
'allowed' => 'vtt',
],
'config' => $buildFileFieldConfig(
['vtt'],
0,
'tx_html5mediakit_track_kind, tx_html5mediakit_track_label, tx_html5mediakit_track_srclang',
),
],
'caption' => [
'label' => $languagePrefixColumn . 'caption',
Expand Down Expand Up @@ -158,19 +178,10 @@
],
'poster' => [
'label' => $languagePrefixColumn . 'poster',
'config' => [
'type' => 'file',
'allowed' => 'common-image-types',
'appearance' => ['createNewRelationLinkTitle' => $lllAddImageFileReference],
'maxitems' => 1,
'behaviour' => ['allowLanguageSynchronization' => true],
'overrideChildTca' => [
'types' => [
'0' => ['showitem' => '--palette--;;filePalette'],
AbstractFile::FILETYPE_IMAGE => ['showitem' => '--palette--;;filePalette'],
],
],
],
'config' => $buildFileFieldConfig(
allowedFileTypes: 'common-image-types',
createNewRelationLinkTitle: $lllAddImageFileReference
),
],
'sys_language_uid' => [
'exclude' => true,
Expand All @@ -193,27 +204,36 @@
'0' => ['showitem' => 'type,--palette--;;hiddenFields'],
'video' => [
'showitem' => '
type, h264, web_m, ogv, poster,
--palette--;' . $languagePrefixColumn . 'palette.metadata;metadata,
--palette--;;hiddenFields
--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.media,
type, h264, web_m, ogv, poster,
--palette--;;hiddenFields,
--div--;' . $languagePrefixColumn . 'tracks,
tracks,
--div--;' . $languagePrefixColumn . 'palette.metadata;metadata,
caption, description,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language,
--palette--;;language,
',
],
'audio' => [
'showitem' => '
type, mp3, ogg,
--palette--;' . $languagePrefixColumn . 'palette.metadata;metadata,
--palette--;;hiddenFields
--div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.media,
type, mp3, ogg,
--palette--;;hiddenFields,
--div--;' . $languagePrefixColumn . 'tracks,
tracks,
--div--;' . $languagePrefixColumn . 'palette.metadata;metadata,
caption, description,
--div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language,
--palette--;;language,
',
],
],
'palettes' => [
'metadata' => [
'showitem' => 'tracks, --linebreak--, caption, --linebreak--, description',
'canNotCollapse' => 1,
],
'hiddenFields' => [
'showitem' => 'sys_language_uid, l10n_parent',
'isHiddenPalette' => true,
'language' => [
'showitem' => '
sys_language_uid, l10n_parent
',
],
],
];
17 changes: 17 additions & 0 deletions Tests/Acceptance/Backend/ContentCreationCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ public function html5MediaCanBeCreated(BackendTester $I, PageTree $pageTree, Mod
$I->waitForElement($headerInputSelector);
$I->fillField($headerInputSelector, 'Testheader');

// Switch to tab "Media"
$I->click('.typo3-TCEforms > ' . $this->buildTabSelector(2));

$I->click('Create new');

$I->waitForElement('div[data-title="Media file"] select[name$="[type]"]');

// Tab "Meta data"
$I->click('div[data-foreign-table="tx_html5mediakit_domain_model_media"] ' . $this->buildTabSelector(3));

$I->fillField('div[data-title="Media file"] input[data-formengine-input-name$="[caption]"]', 'The caption');

$I->click('Save');
Expand All @@ -51,8 +57,19 @@ public function html5MediaCanBeCreated(BackendTester $I, PageTree $pageTree, Mod

$I->seeInField($headerInputSelector, 'Testheader');

// Switch to tab "Media"
$I->click('.typo3-TCEforms > ' . $this->buildTabSelector(2));

$I->click('#data-1-tt_content-1-tx_html5mediakit_media-tx_html5mediakit_domain_model_media-1_label');
$I->waitForElement('div[data-title="Media file"] select[name$="[type]"]');
$I->seeInField('div[data-title="Media file"] input[data-formengine-input-name$="[caption]"]', 'The caption');
}

private function buildTabSelector(int $tabNumber): string
{
return sprintf(
'div[role="tabpanel"] > ul.nav-tabs > li.t3js-tabmenu-item:nth-child(%d) > a',
$tabNumber
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Codeception\Event\SuiteEvent;
use TYPO3\TestingFramework\Core\Acceptance\Extension\BackendEnvironment;
use RuntimeException;

class BackendHtml5mediakitEnvironment extends BackendEnvironment
{
Expand Down Expand Up @@ -38,7 +39,7 @@ public function bootstrapTypo3Environment(SuiteEvent $suiteEvent): void
$typo3RootPath = (string)getenv('TYPO3_PATH_ROOT');

if ($typo3RootPath === '') {
throw new \RuntimeException('TYPO3_PATH_ROOT environment variable is not set');
throw new RuntimeException('TYPO3_PATH_ROOT environment variable is not set');
}

$putenvCode = PHP_EOL
Expand Down
Loading