编辑记录时禁用 TCA 中的字段
Disable field in TCA when editing a record
是否可以仅在编辑记录时禁用 TCA 配置中的字段?
新 记录的 TCA 配置:
'title' => [
'exclude' => true,
'label' => 'Title',
'config' => [
'type' => 'input',
'size' => 30,
'eval' => 'trim,required'
],
],
现有 条记录的 TCA 配置:
'title' => [
'exclude' => true,
'label' => 'Title',
'config' => [
'type' => 'input',
'size' => 30,
'eval' => 'trim,required'
'readOnly' => true,
],
],
我不知道针对新记录和现有记录的不同 TCA 设置的内置解决方案。
由于最终的 TCA 被缓存,因此也无法在运行时使用某些 PHP 对其进行操作。
可以在后台添加Javascript。有了这个 Javascript 你就可以动态地禁用字段。但请注意,这只是一个 hacky workaround,可以轻松克服!
在ext_localconf.php
中添加Javascript:
if (TYPO3_MODE === 'BE') {
$renderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
$renderer->addJsFile(
'EXT:myext/Resources/Public/JavaScript/Backend.js'
);
}
在 Backend.js
中你可以这样做:
require(["jquery"], function ($) {
const queryString = decodeURI(window.location.search),
urlParams = new URLSearchParams(queryString);
if (urlParams.has('route') && urlParams.get('route') == '/record/edit') {
// check, that you just do changes, if you edit records of the desired model
if (queryString.indexOf('edit[tx_myextension_domain_model_model][')) {
let idPosStart = queryString.indexOf('edit[tx_myextension_domain_model_model][') + 40,
idPosEnd = queryString.indexOf(']=', idPosStart),
idLength = idPosEnd - idPosStart,
idEl = queryString.substr(idPosStart, idLength),
elVal = urlParams.get('edit[tx_myextension_domain_model_model][' + idEl + ']');
if (elVal == 'edit') {
// Delay everything a little bit, otherwise html is not fully loaded and can't be addressed
setTimeout(function () {
// disable desired fields, eg field "title"
let titleField = $('[data-field="title"]').parents('.form-section');
titleField.find('input').prop('disabled', true);
titleField.find('button.close').remove();
}, 800);
}
}
}
}
是否可以仅在编辑记录时禁用 TCA 配置中的字段?
新 记录的 TCA 配置:
'title' => [
'exclude' => true,
'label' => 'Title',
'config' => [
'type' => 'input',
'size' => 30,
'eval' => 'trim,required'
],
],
现有 条记录的 TCA 配置:
'title' => [
'exclude' => true,
'label' => 'Title',
'config' => [
'type' => 'input',
'size' => 30,
'eval' => 'trim,required'
'readOnly' => true,
],
],
我不知道针对新记录和现有记录的不同 TCA 设置的内置解决方案。
由于最终的 TCA 被缓存,因此也无法在运行时使用某些 PHP 对其进行操作。
可以在后台添加Javascript。有了这个 Javascript 你就可以动态地禁用字段。但请注意,这只是一个 hacky workaround,可以轻松克服!
在ext_localconf.php
中添加Javascript:
if (TYPO3_MODE === 'BE') {
$renderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
$renderer->addJsFile(
'EXT:myext/Resources/Public/JavaScript/Backend.js'
);
}
在 Backend.js
中你可以这样做:
require(["jquery"], function ($) {
const queryString = decodeURI(window.location.search),
urlParams = new URLSearchParams(queryString);
if (urlParams.has('route') && urlParams.get('route') == '/record/edit') {
// check, that you just do changes, if you edit records of the desired model
if (queryString.indexOf('edit[tx_myextension_domain_model_model][')) {
let idPosStart = queryString.indexOf('edit[tx_myextension_domain_model_model][') + 40,
idPosEnd = queryString.indexOf(']=', idPosStart),
idLength = idPosEnd - idPosStart,
idEl = queryString.substr(idPosStart, idLength),
elVal = urlParams.get('edit[tx_myextension_domain_model_model][' + idEl + ']');
if (elVal == 'edit') {
// Delay everything a little bit, otherwise html is not fully loaded and can't be addressed
setTimeout(function () {
// disable desired fields, eg field "title"
let titleField = $('[data-field="title"]').parents('.form-section');
titleField.find('input').prop('disabled', true);
titleField.find('button.close').remove();
}, 800);
}
}
}
}