modxcms / revolution

MODX Revolution - Content Management Framework
https://modx.com/
GNU General Public License v2.0
1.36k stars 529 forks source link

Feature Request: Add readonly option to TVs #12917

Open pepebe opened 8 years ago

pepebe commented 8 years ago

Summary

I have some use cases where the value of a TV is set by a plugin. Depending on the task of the TV, it may be necessary to display the TV in readonly mode to prevent accidental editing.

This could be an additional option on the TV level or in Form Customization.

Setting fields to readonly for specific user groups / roles could also be useful.

Mark-H commented 8 years ago

Isn't there a "hidden" input type?

pepebe commented 8 years ago

Yes, hidden may be an option in some cases, but readonly would protect TVs from accidental editing.

wuuti commented 8 years ago

Isn't this covered by the "save" permission on element's and already possible with ACL? This should be improved then: the tv will look like it can be edited, and users may tend to edit it then. If any element for a user is missing the "save" permission, it should be displayed as "disabled" imho in the manager - this could help in a lot of other cases too.

labr1005 commented 7 years ago

I have these cases, too.

Until we have this feature, I'm using custom input types. ExtJs has a property readOnly for inputs.

Readonly text

core/model/modx/processors/element/tv/renders/mgr/input/text-readonly.class.php

<?php
/**
 * @package modx
 * @subpackage processors.element.tv.renders.mgr.input
 */
class modTemplateVarInputRenderTextReadonly extends modTemplateVarInputRender {
    public function getTemplate() {
        return 'element/tv/renders/input/textbox-readonly.tpl';
    }
}
return 'modTemplateVarInputRenderTextReadonly';

manager/templates/default/element/tv/renders/input/textbox-readonly.tpl

<input id="tv{$tv->id}" name="tv{$tv->id}"
    type="text" class="textfield"
    value="{$tv->get('value')|escape}"
    {$style}
    tvtype="{$tv->type}"
/>
<script type="text/javascript">
// <![CDATA[
{literal}
Ext.onReady(function() {
    var fld = MODx.load({
    {/literal}
        xtype: 'textfield'
        ,readOnly: true
        ,focusClass: ''
        ,applyTo: 'tv{$tv->id}'
        ,width: '99%'
        ,msgTarget: 'under'
    {literal}
    });
    Ext.getCmp('modx-panel-resource').getForm().add(fld);
    MODx.makeDroppable(fld);
});
{/literal}
// ]]>
</script>

Readonly textarea

core/model/modx/processors/element/tv/renders/mgr/input/textarea-readonly.class.php

<?php
/**
 * @package modx
 * @subpackage processors.element.tv.renders.mgr.input
 */
class modTemplateVarInputRenderTextAreaReadonly extends modTemplateVarInputRender {
    public function getTemplate() {
        return 'element/tv/renders/input/textarea-readonly.tpl';
    }
}
return 'modTemplateVarInputRenderTextAreaReadonly';

manager/templates/default/element/tv/renders/input/textarea-readonly.tpl

<textarea id="tv{$tv->id}" name="tv{$tv->id}" rows="15">{$tv->get('value')|escape}</textarea>
<script type="text/javascript">
// <![CDATA[
{literal}
Ext.onReady(function() {
    var fld = MODx.load({
    {/literal}
        xtype: 'textarea'
        ,readOnly: true
        ,focusClass: ''
        ,applyTo: 'tv{$tv->id}'
        ,value: '{$tv->get('value')|escape:'javascript'}'
        ,height: 140
        ,width: '99%'
    {literal}
    });
    MODx.makeDroppable(fld);
    Ext.getCmp('modx-panel-resource').getForm().add(fld);
});
{/literal}
// ]]>
</script>

Putting the files in core/model/... and manager/template/... is quick and dirty though, but it works.

Better do it with your own namespace, plugin, etc.: https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/adding-a-custom-tv-type-modx-2.2

pepebe commented 4 years ago

@labr1005 Better late than never... THANKS for sharing your solution.

That being said, this is still an open issue and I believe it should be added to the core set of available TVs.

Jako commented 4 years ago

And if the name attribute of the textarea/input is removed, a post is not possible without modifying the html code.

labr1005 commented 4 years ago

I'm thinking about to extend the current TV types "Text" and "Textarea" with an additional checkbox "Readonly" or similar instead of adding new TV types.

I'll see if I find the time and create a PR.

pepebe commented 4 years ago

Good point "readonly" is just a form field attribute. Adding a checkbox to textarea and text, seems to be a very good way to handle things.