Skip to content

Instantly share code, notes, and snippets.

@Anubarak
Last active April 4, 2019 06:25
Show Gist options
  • Save Anubarak/289258ece1d0c7a605b300f79fe48a1d to your computer and use it in GitHub Desktop.
Save Anubarak/289258ece1d0c7a605b300f79fe48a1d to your computer and use it in GitHub Desktop.
<?php
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);
}
}
}
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment