Guest User

Untitled

a guest
Dec 6th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. Event::on(
  2. Entry::class,
  3. Entry::EVENT_BEFORE_SAVE,
  4. function(ModelEvent $event){
  5. /** @var Entry $entry */
  6. $entry = $event->sender;
  7.  
  8. if($entry->section == 'Question' && $entry->type=='reply') {
  9. if($entry->newParentId != $entry->question->id[0]){
  10. $entry->newParentId = $entry->question->id[0];
  11. }
  12. }
  13. });
  14.  
  15. if ($entry->newParentId) {
  16. $parentEntry = Craft::$app->getEntries()->getEntryById($entry->newParentId, $entry->siteId);
  17. if (!$parentEntry) {
  18. throw new Exception('Invalid entry ID: ' . $entry->newParentId);
  19. }
  20. } else {
  21. $parentEntry = null;
  22. }
  23.  
  24. $entry->setParent($parentEntry);
Add Comment
Please, Sign In to add comment