How would you approach making this plug-in?

Razasharp

Well-known member
I'd thought I'd start off with something simple as my first plugin... but it is proving more difficult than I envisaged :cry:

I'd like to show the prefix with the last post details on the forum home and forum listing pages.

There is a xf_forum table that holds the caches for forums, and adding a couple of columns into this should be simple enough. Then we just need to piggy-back off the existing methods to include the details when updating the table, and consequently reading from it.

So it seems relatively simple, but it is eluding me (and even other mod authors). Can you help? I am happy for whoever helps with this to release it as part of their portfolio, or release it here myself and transfer all rights to Xenforo Ltd. I'd really appreciate your help on this as I think it is the only thing left that I would really really like to include with the launch of my first XF forum :D

---------------------------------------------------
Thoughts so far

There is a plugin that is meant to do this already (it doesn't work as expected tho - and the author has said he's finding it difficult to achieve too) so we have a good starting point already. Here's how I imagine it would work so far:

Step one - create columns in xf_forum table.
This is the cache table that XF uses for forums. I propose we add two columns to this, one for the prefix class and one for the prefix text. Perhaps, prefix_plugin_last_post_prefix_class and prefix_plugin_last_post_prefix_text

Step two - include these columns when XF updates the cache normally. After a post is made/moved, XF updates the cache for that forum. In the Post Controller, and actionSave action, it says it is to update an existing post - should there be a create action anywhere? :confused: Ok this seems to be handled by the _discussionPostSave() and updateLastPost() methods in DataWriter.php. So we just need to update this to include our columns (?)

PHP:
    {
        $lastPost = $this->_getLastMessageInDiscussion();
        if ($lastPost)
        {
            $messageStructure = $this->_messageDefinition->getMessageStructure();

            $this->set('last_post_id', $lastPost[$messageStructure['key']]);
            $this->set('last_post_date', $lastPost['post_date']);
            $this->set('last_post_user_id', $lastPost['user_id']);
            $this->set('last_post_username', $lastPost['username']);
>
>
        }
        else
        {
            $this->set('last_post_id', $this->get('first_post_id'));
            $this->set('last_post_date', $this->get('post_date'));
            $this->set('last_post_user_id', $this->get('user_id'));
            $this->set('last_post_username', $this->get('username'));
>
>
        }
    }


Step three - making the info available to the necessary templates/controller actions. This seems to be the part that has stumped us. I guess we need to utilise getNodeDataForListDisplay which is in the Node Model. But I don't really understand what's going on here.

Am I on the right track? Does anyone know where to go from here? I am a PHP noob, but do have a fairly good understanding of OOP and MVC ...your help (and patience!) would be very much appreciated.

 
Last edited:
Anyone have any ideas please?

I would have thought it would be relatively easy using/piggy-backing off the existing methods? :unsure:
 
You'll need to extend updateLastPost in XenForo_DataWriter_Forum in your addon and add this line
PHP:
protected (public?) function updateLastPost()
{
    parent::updateLastPost();
    $this->set('prefix_plugin_last_prefix_id', ($lastPost) ? $lastPost['prefix_id'] : 0);
}

Then you'll need to extend XenForo_NodeHandler_Abstract as follows
PHP:
protected function _getForumLikePushableData(array $node, array $childPushable)
{
    if (!empty($node['last_post_date']))
    {
            $newPushable['prefix_plugin_last_prefix_id']  = $node['prefix_plugin_last_prefix_id'];
    }

    return parent::_getForumLikePushableData($node, $childPushable);
}

protected function _compileForumLikePushableData(array $newPushable, array $childPushable)
{
    $newPushable = parent::_compileForumLikePushableData($newPushable, $childPushable);
     
    foreach ($childPushable AS $childData)
    {
        if (!empty($childData['last_post_date']) && $childData['last_post_date'] > $newPushable['last_post_date'])
        {
                $newPushable['prefix_plugin_last_prefix_id']  = $childData['prefix_plugin_last_prefix_id'];
        }
    }
 
    $newPushable['lastPost']['prefixPluginLastPrefixId'] = newPushable['prefix_plugin_last_prefix_id'];

    return $newPushable;
}

And then in your template add something like:
Code:
{xen:helper threadPrefix, $category.lastPost.prefixPluginLastPrefixId, html, ''}

The only column you'll need to add to xf_forum is `prefix_plugin_last_prefix_id`.

Please understand this is all untested and I just wrote it here in the message box, but this is how I would do it. Should be very simple.
 
You'll need to extend updateLastPost in XenForo_DataWriter_Forum in your addon and add this line
PHP:
protected (public?) function updateLastPost()
{
    parent::updateLastPost();
    $this->set('prefix_plugin_last_prefix_id', ($lastPost) ? $lastPost['prefix_id'] : 0);
}

Then you'll need to extend XenForo_NodeHandler_Abstract as follows
PHP:
protected function _getForumLikePushableData(array $node, array $childPushable)
{
    if (!empty($node['last_post_date']))
    {
            $newPushable['prefix_plugin_last_prefix_id']  = $node['prefix_plugin_last_prefix_id'];
    }

    return parent::_getForumLikePushableData($node, $childPushable);
}

protected function _compileForumLikePushableData(array $newPushable, array $childPushable)
{
    $newPushable = parent::_compileForumLikePushableData($newPushable, $childPushable);
   
    foreach ($childPushable AS $childData)
    {
        if (!empty($childData['last_post_date']) && $childData['last_post_date'] > $newPushable['last_post_date'])
        {
                $newPushable['prefix_plugin_last_prefix_id']  = $childData['prefix_plugin_last_prefix_id'];
        }
    }

    $newPushable['lastPost']['prefixPluginLastPrefixId'] = newPushable['prefix_plugin_last_prefix_id'];

    return $newPushable;
}

And then in your template add something like:
Code:
{xen:helper threadPrefix, $category.lastPost.prefixPluginLastPrefixId, html, ''}

The only column you'll need to add to xf_forum is `prefix_plugin_last_prefix_id`.

Please understand this is all untested and I just wrote it here in the message box, but this is how I would do it. Should be very simple.

Hi Jeff, thank you very much for going to the trouble of posting this :)

A developer actually offered to help me with this privately, and I believe he will be releasing it here once he polishes it up.

Always interesting to see how different people approach the same problem - so thanks once again for posting.
 
You'll need to extend updateLastPost in XenForo_DataWriter_Forum in your addon and add this line
PHP:
protected (public?) function updateLastPost()
{
    parent::updateLastPost();
    $this->set('prefix_plugin_last_prefix_id', ($lastPost) ? $lastPost['prefix_id'] : 0);
}

Then you'll need to extend XenForo_NodeHandler_Abstract as follows
PHP:
protected function _getForumLikePushableData(array $node, array $childPushable)
{
    if (!empty($node['last_post_date']))
    {
            $newPushable['prefix_plugin_last_prefix_id']  = $node['prefix_plugin_last_prefix_id'];
    }

    return parent::_getForumLikePushableData($node, $childPushable);
}

protected function _compileForumLikePushableData(array $newPushable, array $childPushable)
{
    $newPushable = parent::_compileForumLikePushableData($newPushable, $childPushable);
    
    foreach ($childPushable AS $childData)
    {
        if (!empty($childData['last_post_date']) && $childData['last_post_date'] > $newPushable['last_post_date'])
        {
                $newPushable['prefix_plugin_last_prefix_id']  = $childData['prefix_plugin_last_prefix_id'];
        }
    }

    $newPushable['lastPost']['prefixPluginLastPrefixId'] = newPushable['prefix_plugin_last_prefix_id'];

    return $newPushable;
}

And then in your template add something like:
Code:
{xen:helper threadPrefix, $category.lastPost.prefixPluginLastPrefixId, html, ''}

The only column you'll need to add to xf_forum is `prefix_plugin_last_prefix_id`.

Please understand this is all untested and I just wrote it here in the message box, but this is how I would do it. Should be very simple.
:D I don't think this way will working on Category *have some forums parent*
 
I'm not sure I understand. He was looking for something that logged the last prefix used in a forum, similarly to logging the last post title and last post time from a forum. Since a category cannot have posts in it, it would not make sense to log the last used prefix since there are no posts to have prefixes used on.

Am I misunderstanding your statement? Maybe I am and I can maybe help you through it if you can explain it more in depth.
 
I'm not sure I understand. He was looking for something that logged the last prefix used in a forum, similarly to logging the last post title and last post time from a forum. Since a category cannot have posts in it, it would not make sense to log the last used prefix since there are no posts to have prefixes used on.

Am I misunderstanding your statement? Maybe I am and I can maybe help you through it if you can explain it more in depth.
http://xenforo.com/community/threads/lastpost-prefix.63359/#post-719706
:D Won't work with this state :)
 
Oh I see. I'm sorry I didn't realize the add-on was already released. That add-on was developed by someone else so it doesn't use my code from above, unless the developer decided to go off what I posted. What I posted is completely untested though, I wrote it right here in the reply box, so it may not work 100% first try but it should.
 
Back
Top Bottom