Restrict available parent entries when editing entry in Craft 4 #12475
-
Trying to work out how to achieve something in Craft 4 that I had working in version 3: In a module I’m building I need to restrict which entries out of a structure are available to select as a parent when creating/editing an entry. In Craft 3 I could hook into the edit screen and modify the parentOptionCriteria like this like: Craft::$app->view->hook('cp.entries.edit', function( array &$context )
{
$context['parentOptionCriteria'] = [
'siteId' => 1,
'sectionId' => $client->section->id,
'status' => null,
'where' => [
'in',
'elements.id',
$availableParentIds
],
'drafts' => null,
'draftOf' => false,
'level' => '<= 2'
];
} I understand that in Craft 4 this hook isn’t available as you’re now using the Unified Element Editor, but I can’t find any examples of how I could achieve the same functionality in this version. Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Just added a new use craft\elements\Entry;
use craft\events\ElementCriteriaEvent;
use yii\base\Event;
Event::on(
Entry::class,
Entry::EVENT_DEFINE_PARENT_SELECTION_CRITERIA,
function(ElementCriteriaEvent $event) {
$event->criteria['id'] = $availableParentId;
$event->criteria['level'] = '<= 2';
},
); |
Beta Was this translation helpful? Give feedback.
Just added a new
EVENT_DEFINE_PARENT_SELECTION_CRITERIA
event tocraft\elements\Entry
for Craft 4.4 (#12485). With it you’ll be able to do this: