XF 2.1 How to soft delete/un-delete a thread, get posts for thread

Edman

Member
Every time I make a news post on my CMS, I want to make a new thread in the forums.

My knowledge of Xenforo is quite limited, just downloaded the software last week, but I have found snippets of code on these forums that allow me to do this.

However, when I delete a news post or set it to unpublished, I want to soft-delete the thread, so I can restore it when a news item is published again.

It looks like I need to change the discussion_state from "visible" to "deleted". But how to do that within the XF framework?

As far as I understand, doing
$thread = \XF::em()->find('XF:Thread', $threadID);
$thread->delete();
Deletes the thread entirely?

$thread = \XF::em()->find('XF:Thread', $threadID);
$thread->setDiscussionState('deleted');
Doesn't work

Also, I figured out how to get all the thread information and make a reply, but how do you get all the replies within a thread for a particular thread ID?

Thanks!
 
You'll want to use the \XF\Service\Thread\Deleter service to delete threads (or, if you want to do it more directly, you can use the softDelete method in the Thread entity).

To find replies in a thread, use the post finder:

PHP:
$finder = \XF::finder('XF:Post');
$messages = $finder->inThread($thread)->fetch();
 
Fantastic, thanks!

If I use the softDelete method on Thread entity, how do I set it back to visible? Is there some list of these functions available?
 
Just look in the src/XF/Entity/Thread.php file - ideally your IDE would suggest available methods.

You can just modify the discussion_state field directly:

PHP:
$thread = xxx;
$thread->discussion_state = 'visible';
$thread->save();
 
Only now managed to finish this up and started testing, but discussion_state gave me the following error.

Code:
InvalidArgumentException: Column 'discusion_state' is unknown src/XF/Mvc/Entity/Entity.php:582

So I looked up the available methods in IDE like you said, and I managed to find a way to soft delete a thread

Code:
$reason = "News Unpublished";
$thread = \XF::em()->find('XF:Thread', $threadid);
$user = \XF::em()->find('XF:User', $userid);
$thread->softDelete($reason, $user);

Question now is, how do I set it back to visible again?

If I do the following:

Code:
        $dir = "./forums";
        require($dir . '/src/XF.php');
        XF::start($dir);


        $thread = \XF::em()->find('XF:Thread', $threadid);
        $thread->discusion_state = 'visible';
        $thread->save();

I get the same error

Code:
     InvalidArgumentException: Column 'discusion_state' is unknown src/XF/Mvc/Entity/Entity.php:582
 
Back
Top Bottom