Skip to content

Instantly share code, notes, and snippets.

@Schweriner
Created December 6, 2019 16:20
Show Gist options
  • Save Schweriner/ef4c32f27c131922fef7381be252766a to your computer and use it in GitHub Desktop.
Save Schweriner/ef4c32f27c131922fef7381be252766a to your computer and use it in GitHub Desktop.
TYPO3 File Reference Fields Required using a small Require JS Modul (Title, Alternative, Description...)
<?php
if (!defined('TYPO3_MODE')) {
die('Access denied.');
}
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
'TGM.' . 'tgm_copyright',
'Main',
[
'Copyright' => 'list,sitemap',
],
// non-cacheable actions
[
'Copyright' => 'sitemap',
]
);
if (TYPO3_MODE === "BE" && true === version_compare(TYPO3_branch, '9.5', '>=') ) {
if(true === (bool) \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)
->get('tgm_copyright', 'copyrightRequired')) {
$pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
$pageRenderer->loadRequireJsModule('TYPO3/CMS/TgmCopyright/RequiredFileReferenceFields');
}
}
define(['jquery'], function($) {
$(document).on('t3-formengine-postfieldvalidation',function() {
validateReferenceFields();
});
$(document).on('change','.t3js-form-field-eval-null-placeholder-checkbox input', function() {
validateReferenceFields();
});
function validateReferenceFields() {
var $referenceFields = $('.t3js-formengine-placeholder-formfield input[type="hidden"][name$="[copyright]"]');
$referenceFields.each(function() {
var $parentFieldGroup = $(this).closest('.t3js-formengine-palette-field');
if($(this).parents('.t3js-inline-record-deleted').length === 0) {
if(false === $parentFieldGroup.find('.t3js-form-field-eval-null-placeholder-checkbox input').is(':checked')
&& $parentFieldGroup.find('.t3js-formengine-placeholder-placeholder input').val().toString() === '') {
$parentFieldGroup.addClass('has-error');
} else if(true === $parentFieldGroup.find('.t3js-form-field-eval-null-placeholder-checkbox input').is(':checked')
&& $(this).val().toString() === '') {
$parentFieldGroup.addClass('has-error');
} else {
$parentFieldGroup.removeClass('has-error');
}
} else {
$parentFieldGroup.removeClass('has-error');
}
});
}
});
@Schweriner
Copy link
Author

Line 12 of the JS is made to fit only with the related extensions "copyright"-field of File References. But this selector can be replaced to fit any other field of file references like the title or alternative text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment