I have an event listener on elements.onPopulateElement, what I want is make a last minute decision if I want to populate or not. I have certain customfields on an admin CP user (eg company). All entries (in this case in the section "normalUsers", these are entries not actual users) have that same field company (companiesDropdown). I can detect in the code below that the entries' companiesDropdown field does not match the currentUsers companyDropdown field. But how can I tell the calling piece of code not the populate this element?
The following code it initialised in the plugin init method
private function _initEventPopulateNormalUser() {
craft()->on('elements.onPopulateElement', function(Event $event) {
$this->_getNormalUserSection();
// HERE we check if we actually save a normalUser section entry, may be a bit brute force, we will polish that later
$lCompanyIdEntry = $event->params['element']->getAttributes(array('sectionId'));
if (! isset($lCompanyIdEntry['sectionId'])) return;
if ($lCompanyIdEntry['sectionId'] != $this->mNormalUsersSection) return;
$lCurrentUser = craft()->userSession->getUser();
$lCompanyIdUser = $lCurrentUser->companiesDropdown[0]->id;
$lEntryCompanyId = $event->params['element']->companiesDropdown->first()->id;
if ($lCompanyIdUser != $lEntryCompanyId) {
/*
Somewhere here I want the element to be emptied and
all functionality calling it, like a elementindex template,
to not show the element in its listing
*/
}
});
}
private function _getNormalUserSection()
{
if (is_null($this->mNormalUsersSection)) {
$lSection = craft()->sections->getSectionByHandle('normalUsers');
$this->mNormalUsersSection = $lSection->id;
}
}