Created
March 16, 2019 15:50
-
-
Save Anubarak/4143bb375a96a8fe79b9c4fabbe158c9 to your computer and use it in GitHub Desktop.
Remove fields in the field layout for an entry for certain users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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