XF 2.0 Thread Create Code Event Listener

RyanStonebraker

New member
I was trying to create a code event listener that would register when a thread was initially created. When this happened, I wanted to get the post_id of the first post in this thread. In order to do this, I looked into the Forum class extension of Entity and found a field called last_post_id. However, when I logged last_post_id, it was consistently 0. To get around this, I found the fields last_thread_title and last_post_date, also present in the Forum class extension. I used these fields in conjunction with the Finder looking at the table xf_thread to try and get last_post_id from here, but this also came back as 0. What I ended up having to do was get the thread_id and then use the Finder again to look at xf_post and get the post_id from there. My question now is, is last_post_id being 0 a bug? Also, is there a better way to create a code event listener for thread creation?

Code Used:
PHP:
$thread_title = $entity->last_thread_title;
$post_date = $entity->last_post_date;
$thread_finder = \XF::finder('XF::Thread');
$thread_id = $thread_finder->where('title', $thread_title)->where('post_date', $post_date)->fetchOne()->thread_id;
$post_finder = \XF::finder('XF::Post');
$post_id = $post_finder->where('thread_id', $thread_id)->fetchOne()->post_id;
 
I was in a similar situation lately and just dropped the whole listener process after fiddeling around for a while and not getting any kind of positive results. Even post_save did not work, so yea.
Instead I went with extending the thread creator service. Depending on what you want to achieve, there are several methods you could override. Just call the parent method first and then, if needed, return your modified object.
protected function _save()
After this function your post will be up for sure. So you could hook into that.
 
Top Bottom