简体   繁体   English

编辑记录时禁用 TCA 中的字段

[英]Disable field in TCA when editing a record

Is it possible to disable a field in the TCA config, only when editing a record?是否可以仅在编辑记录时禁用 TCA 配置中的字段?

TCA config for new record:记录的 TCA 配置:

'title' => [
    'exclude' => true,
    'label' => 'Title',
    'config' => [
        'type' => 'input',
        'size' => 30,
        'eval' => 'trim,required'
    ],
],

TCA config for existing records:现有记录的 TCA 配置:

'title' => [
    'exclude' => true,
    'label' => 'Title',
    'config' => [
        'type' => 'input',
        'size' => 30,
        'eval' => 'trim,required'
        'readOnly' => true,
    ],
],

I'm not aware of a built in solution for different TCA settings for new and existing records.我不知道针对新记录和现有记录的不同 TCA 设置的内置解决方案。

Since the final TCA is cached there is also no way to manipulate it with some PHP on runtime.由于最终的 TCA 被缓存,因此也无法在运行时使用一些 PHP 对其进行操作。

It is possible to add Javascript in the backend.可以在后端添加 Javascript。 With this Javascript your are able, to disable fields on the fly.使用此 Javascript,您可以即时禁用字段。 But be aware, that this is just a hacky workaround which can easily be overcome!但请注意,这只是一个很容易克服的hacky 解决方法

Add Javascript in ext_localconf.php :在 ext_localconf.php 中添加ext_localconf.php

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'
    );
}

In Backend.js you can do something like this: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);

            }

        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM