XF 2.0 Throwing more specific error for assertViewableThread

Snog

Well-known member
I've got a couple of applications where I throw a more specific error for assertViewableThread.

The problem is that in order to do it, I basically have to run the entire assertViewableThread function to locate the exact reason for the error. And if it's not specific to my application, then return the parent function so as not to interfere with other add-ons that may extend the same function. That results in a lot of duplicate work.

For the life of me, I can't come up with a better way to do it, so I'm asking here. :)

What would be nice is if assertViewableThread returned the thread and any errors, that way $thread would already contain all of the info I need and if there wasn't a more specific error that could be thrown the extension could just return the thread and original error back into the mix. (HINT, HINT... @Chris D , @Mike :D)
 
You might need to be a bit more specific about the errors you're wanting to return and what the criteria for those errors is as the use case isn't particularly clear right now. Can you provide a more specific code example?
 
You might need to be a bit more specific about the errors you're wanting to return and what the criteria for those errors is as the use case isn't particularly clear right now. Can you provide a more specific code example?
Sure, this is just one example.

I already add a relation to the node a thread is in, so that node carries the information for a social group. And if the visitor is not a member of the group, the more specific error would be 'You must join this group to view thread contents'.
Code:
if (isset($thread->Forum->Node->Group->approval))
{
   if (isset($visitor->SocialGroups->groups) &&
      $thread->Forum->Node->Group->approval && !$thread->Forum->Node->Group->postview &&
      !in_array($thread->Forum->Node->Group->groupid, $visitor->SocialGroups->groups))
   {
      throw $this->exception($this->noPermission(\XF::phrase('snog_groups_must_join')));
   }
}

That is wrapped by a near duplicate of the assertViewableThread funtion in XF itself.

Now if I could do something like this:
Code:
$parent = parent::assertViewableThread($threadId, $extraWith);

if(isset($parent->thread->Forum->Node->Group->approval))
{
   .... code ...
}
else
{
   return $parent;
}

That would be ideal. :D
 
Last edited:
This is directly related to whether the visitor can view a thread, or not.

In which case, is there any particular reason you don't do the checks in the Thread entity canView method?
PHP:
public function canView(&$error = null)
{
    $canView = parent::canView($error);
    
    if ($canView)
    {
        $visitor = \XF::visitor();
        
        if (isset($visitor->SocialGroups->groups)
           && $this->Forum->Node->Group->approval
           && !$this->Forum->Node->Group->postview
           && !in_array($this->Forum->Node->Group->groupid, $visitor->SocialGroups->groups)
        )
        {
            $error = \XF::phrase('snog_groups_must_join');
            $canView = false;
        }
    }
    
    return $canView;
}
The assertViewableThread method will handle throwing an exception if the view check fails.
 
Back
Top Bottom