1

We would like to have a 'translators' user group, where the members can edit only the 'translatable' fields, but not the non translatable things (images, categories etc).

May be i missed something, but giving users permission to edit a specific site lets them edit all attributes.

Is there a way (permission/plugin/code snippet/best practice) to achieve that?

Thanks.

Thinking of writing a custom validation comparing old to new, or hiding/disabling fields via specific css.

1 Answer 1

1

Of course you can just remove the fields via CSS/JS but you could as well remove those fields from the visible layout from the beginning

Craft::$app->getView()->hook(
/**
 * @param array $context
 */
    'cp.entries.edit',
    function(array &$context) {
        /** @var Section $section */
        $section = $context['section'];

        /** @var \craft\models\EntryType $entryType */
        $entryType = $context['entryType'];

        /** @var Entry $entry */
        $entry = $context['entry'];
        /**
         * Array of field handles you want to remove
         */
        $fieldsYouWantToRemove = [
            'relation',
            'tag',
            'category'
        ];

        // include certain conditions you like, either by registering custom permissions and do
        // Craft::$app->getUser()->checkPermission('myFancyPermission');
        // or just search for the current users groups
        // Craft::$app->getUser()->getIdentity()->getGroups();
        if($entry->siteId === 4 && $section->handle === 'insertYourSectionHandle' && $entryType->handle === 'yourEntryTypeHandle'){
            // grab all the fields in the layout
            foreach ($entryType->getFieldLayout()->getTabs() as $tab){
                /** @var \craft\base\Field[] $fields */
                $fields = $tab->getFields();
                foreach ($fields as $key => $field){
                    // check if the fields handle is forbidden
                    if(in_array($field->handle, $fieldsYouWantToRemove, true) === true){
                        // remove the field
                        unset($fields[$key]);
                    }
                }
                // set the fields back
                $tab->setFields($fields);
            }    
        }
    }
);

'cp.entries.edit' is a hook that enables you to manipulate all variables in the given context, so you check if the entry has a certain type and is in a certain section and remove all the fields from the tabs that you don't want to show.

You can register custom permissions for each field, for each site or you can even use events to check if the content has changed if you really want to make sure they didn't manipulate the HTML

edit

You can as well check for the translation setting in the field

if($field->translationMethod === Field::TRANSLATION_METHOD_SITE){
    unset($fields[$key]);
}

or

if($field->translationMethod !== Field::TRANSLATION_METHOD_NONE){
    unset($fields[$key]);
}

Whatever you need

3
  • Thanks, will definitly use this for hiding some adminitrative things.
    – user8315
    Commented Mar 18, 2019 at 10:26
  • However i still have mixed feelings when it comes to extending the 'hide fields only' method to matrix or supertable fields, that might result in a weird authoring experience (like completly empty blocks). and still allows to move/delete blocks. Will also investigate building a dedicated translation tool.
    – user8315
    Commented Mar 18, 2019 at 10:32
  • Yeah the code above won't work for nested fields. You can only hide/show fields in the layout of your main element, not fields in nested elements. That's going to be more tricky and you'll need to use beforeSave events to merge the fields authors may not edit with the fields authors may edit. Hiding those via CSS might be easier but once a Author knows how to unhide them they can edit it Commented Mar 18, 2019 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.