Addpixel / KirbyComments

[Kirby 2] File-based comments stored as subpages for the Kirby CMS.
MIT License
68 stars 5 forks source link

Updating parent page when posting comment #41

Closed chrisgwynne closed 6 years ago

chrisgwynne commented 6 years ago

Is there any way to update a field in the parent page when posting a comment?

florianpircher commented 6 years ago

Yes, you can use a hook to run your logic for specific events, e.g. did-save-comment.

chrisgwynne commented 6 years ago

c::set('comments.hooks.did-save-comment', function ($comments, $comment) { $page = $page->$parent()->update(array('lastupdated' => 'd-m-Y H:i:s')); return false; });

I'm not the best with this unfortunately florian, keeps throwing up errors.

florianpircher commented 6 years ago

$page is undefined in config.php. You can access the page object using the $comment variable:

c::set('comments.hooks.did-save-comment', function ($comments, $comment, $commentPage) {
  try {
    $comment->page()->update(...);
  } catch (Exception $e) {
    // do something with the exception
    echo $e->getMessage();
  }
});

Some other notes:

chrisgwynne commented 6 years ago

Florian, this works perfectly, thank you so much! :)