XF 2.3 Class extension issues for new ThreadType

Taylor J

Well-known member
So now I'm having an issue with a class extension using the extension system in the acp.

I believe I have everything setup properly to create a new ThreadType but whenever it gets to the portion where it should be using my TaylorJ\Blogs\XF\ForumType\Discussion instance but instead it goes to the one that XFRM uses/created when $this->getExtraAllowedThreadTypes from the ForumTypes abstract handler is called and fails out even though the thread type blogPost is added as allowed I still get back a phrase error saying to "please_select_valid_thread_type".

My TaylorJ\Blogs\ForumType\Discussion:
PHP:
<?php

namespace TaylorJ\Blogs\XF\ForumType;

use XF\Entity\Forum;

class Discussion extends XFCP_Discussion
{
    public function getExtraAllowedThreadTypes(Forum $forum): array
    {
        $allowed = parent::getExtraAllowedThreadTypes($forum);
        $allowed[] = 'blogPost';

        return $allowed;
    }

    public function getCreatableThreadTypes(Forum $forum): array
    {
        $creatable = parent::getCreatableThreadTypes($forum);
        $this->removeBlogPostTypeFromList($creatable);

        return $creatable;
    }

    public function getFilterableThreadTypes(Forum $forum): array
    {
        $filterable = parent::getFilterableThreadTypes($forum);

        $resourceTarget = \XF::db()->fetchOne("
            SELECT 1
            FROM xf_option
            WHERE option_id = 'taylorjBlogsBlogPostForum' AS option AND
            WHERE option.option_value = ?
            LIMIT 1
        ", $forum->node_id);
        if (!$resourceTarget)
        {
            $this->removeBlogPostTypeFromList($filterable);
        }

        return $filterable;
    }

    protected function removeBlogPostTypeFromList(array &$list)
    {
        $blogPostKey = array_search('blogPost', $list);
        if ($blogPostKey !== false)
        {
            unset($list[$blogPostKey]);
        }
    }
}

My class extension:
1723564983964.webp

And how I'm creating the thread on BlogPost creation:

PHP:
protected function setupResourceThreadCreation(Forum $forum)
    {
        /** @var Creator $creator */
        $creator = $this->service('XF:Thread\Creator', $forum);
        $creator->setIsAutomated();

        $creator->setContent($this->blogPost->getExpectedThreadTitle(), $this->getThreadMessage(), false);

        $creator->setDiscussionTypeAndDataRaw('blogPost');

        $thread = $creator->getThread();
        $thread->discussion_state = $this->blogPost->blog_post_state;

        return $creator;
    }

Is there something else I'm missing that needs to be added in to make it know 'blogPost' is an allowed threadType?
 
Last edited:
Forgot to add my actual thread type code

PHP:
<?php

namespace TaylorJ\Blogs\ThreadType;

use XF\Entity\Thread;
use XF\Http\Request;
use XF\ThreadType\AbstractHandler;

class BlogPost extends AbstractHandler
{
    public function getTypeIconClass(): string
    {
        return '';
    }

    public function getThreadViewAndTemplate(Thread $thread): array
    {
        return ['TaylorJ\Blogs:Thread\ViewTypeBlogPost', 'taylorj_blogs_thread_view_type_blog_post'];
    }

    public function adjustThreadViewParams(Thread $thread, array $viewParams, Request $request): array
    {
        $thread = $viewParams['thread'] ?? null;
        if ($thread)
        {
            /** @var \TaylorJ\Blogs\Entity\BlogPost $blogPost */
            $blogPost = \XF::repository('TaylorJ\Blogs:BlogPost')->findResourceForThread($thread)->fetchOne();
            if ($blogPost && $blogPost->canView())
            {
                $viewParams['blogPost'] = $blogPost;
            }
        }

        return $viewParams;
    }

    public function allowExternalCreation(): bool
    {
        return false;
    }

    public function canThreadTypeBeChanged(Thread $thread): bool
    {
        return false;
    }

    public function canConvertThreadToType(bool $isBulk): bool
    {
        return false;
    }
}

I also have the thread type in the db
1723568613806.webp
Looking that that though, maybe my handler class is wrong?
 
I've been pretty lost on this all day to the fact I even took some PTO for the day to try and figure this out lol.

I've now tried using my content type as the thread type and adding that in as a new one the same way I did above but that didn't change anything.

After undoing those changes and going back I am now seeing that blogPost is an allowed thread type that is setup after xfrm sets its thread type with getExtraAllowedThreadTypes

I did also find out that in this function

PHP:
    public function threadType($type, $throw = true)
    {
        $threadType = $this->container->create('threadType', $type);
        if (!$threadType && $throw)
        {
            throw new \InvalidArgumentException("Invalid thread type '$type'");
        }

        return $threadType;
    }

I am getting a null back for $threadType here so I'm sort of making progress on running backwards with the debugger but now I'm not sure why it wasn't in the container.

After I'm done with this addon I'm going to have so much documentation for all of these "hard" parts lmao.

Edit: So digging further into XF\Container.php\Create I am seeing that my thread type isn't in the $this->factoryObjects object. Gotta figure out why

Edit: Forget the above that's fine. I'm failing at $object = $callable($key, $params, $this); in the create function.
My thread type isn't showing up in the $threadTypes variable in the below function so more digging to do on why it's not there.
PHP:
$container->factory('threadType', function ($type, array $params, Container $c)
        {
            $threadTypes = $c['threadTypes'];
            if (!isset($threadTypes[$type]))
            {
                return null;
            }

            $typeClass = \XF::stringToClass($threadTypes[$type], '%s\ThreadType\%s');
            $typeClass = \XF::extendClass($typeClass);

            return new $typeClass($type);
        });
 
Last edited:
After a few more hours of going through everything with the debugger and not seeing where I strayed wrong I just am at a loss here. When comparing what I have to what XFRM has I don't seem to be missing anything which maybe makes me think it's my handler class column for the blogPost thread type??

I know I don't have the Finder setup yet for BlogPosts but that shouldn't cause this from working as I can't even get to those steps yet.

Edit: holy guacamole it was the addon id for the thread type db entry.....
 
Last edited:
Back
Top Bottom