XF 2.2 Does xf/pub/controller/post do anything

Dannymh

Active member
I'm trying to parse a few variables into posts on display to modify them, assumed this was the place to do that but it seems I was not correct and I can't get it to react to anything not even exiting out.

Is there another easier method of passing some conditional Params to each post in a thread?
 
In debug mode you can hover over the cog icon on the bottom right to see which controller and action are being invoked. You're probably looking for \XF\Pub\Thread::actionIndex in this case.
 
In debug mode you can hover over the cog icon on the bottom right to see which controller and action are being invoked. You're probably looking for \XF\Pub\Thread::actionIndex in this case.
woah, I did not know that, thats really helpful, thank you
 
In debug mode you can hover over the cog icon on the bottom right to see which controller and action are being invoked. You're probably looking for \XF\Pub\Thread::actionIndex in this case.
Still can't quite figure this out, and wondering if perhaps you could give me some guidance here as its completely baked my noodle

What I am trying to do i look at each post, if that post ID exists in a new table then on the fly change the Username and avatar to those stored in the new table.

The new table contains posts built from api calls between sites, and part of that puts the username of the user from the original site. The post comes from a user made for the API calls.

So essentially we say

if post is in the table, get the username and avatar from that row, then replace the ones displayed in the postbit/user info of the post

In theory I would have thought I could do the evaluation part in code and then pass off to a template modification, but I cant even get the variable across to the template and I am just so so confused no
 
You'd probably want to approach this by creating an entity for the new table, extending the post entity to add a relation to your entity, and including that relation in the full "with alias" so that it is eager-loaded (joined) on the thread view. Then you can access it in your template modification via something like $post.YourRelation.some_column.
 
You'd probably want to approach this by creating an entity for the new table, extending the post entity to add a relation to your entity, and including that relation in the full "with alias" so that it is eager-loaded (joined) on the thread view. Then you can access it in your template modification via something like $post.YourRelation.some_column.
Hey thanks for this its given me a path to follow. I am not quite getting the results I need here but wanted to check I am on the right track

I created an entity for my table

PHP:
namespace Silvertails\CrossForumChat;

use XF\Mvc\Entity\Entity;
use XF\Mvc\Entity\Structure;

class CrossForumChat extends \XF\Mvc\Entity\Entity
{

    public static function getStructure(Structure $structure)
    {
        $structure->table = 'xf_stcc_posts';
        $structure->shortName = 'Silvertails:Stcc';
        $structure->primaryKey = 'id';
        $structure->columns = [
            'id' => ['type' => self::UINT, 'autoIncrement' => true, 'nullable' => true],
            'site' => ['type' => self::STR, 'maxLength' => 255, 'default' => ''],
            'endpoint' => ['type' => self::STR, 'maxlength' =>255, 'default' => ''],
            'localpost' => ['type' => self::UINT, 'required' => true],
            'extpost' => ['type' => self::UINT, 'required' => true],
            'extuser' => ['type' => self::UINT, 'required' => true],
            'intuser' => ['type' => self::INT, 'default' => 0],
            'deleted' => ['type' => self::UINT, 'default' => 0],
            'origUsername' => ['type' => self::UINT, 'default' => 0],
            'isLocal' => ['type' => self::UINT, 'default' => 0],   
        ];
        $structure->getters = [];
        $structure->relations = [ ];

        */
        \XF::dump($structure);
    }

And then extended the XF/Entity/Post with the following

PHP:
namespace Silvertails\CrossForumChat\XF\Entity;

//use Silvertails\CrossForumChat\XF\Pub\Controller\XFCP_Post;
use XF\Mvc\Entity\Structure;

class Post extends XFCP_Post
{
    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);

        $structure->relations['Stcc'] = [
            'entity' => 'Silvertails:Stcc',
            'type' => self::TO_ONE,
            'conditions' => [
                ['localpost', '=', '$post_id']
            ],
        ];
        return $structure;
    }
}

Where it should join the records if localpost and the post id match from my understanding, but it doesn't appear to be getting any records for each post and spitting out what I want.

Is there a simple way to test that relation and see if it is outputting relations, or is there a way to see the query it is creating and running?
 
You're on the right track. You'll want to place your entity in an Entity sub-directory of your add-on directory. I'd recommend naming it after your table, so your file would be src/addons/Silvertails/CrossForumChat/Entity/Posts.php, the namespace would be Silvertails\CrossForumChat\Entity, and the class name would be Posts. Your short-name must match the add-on ID and class name, so Silvertails\CrossForumChat:Posts. This applies to the relation on the post entity too.

Make sure you've created the class extension for the posts class in the control panel. You'll want to add the relation to the full with-alias in your class extension so it gets eager-loaded (joined):
PHP:
$structure->withAliases['full'][] = 'Stcc'; // or whatever you name the relation

Then you should be able to access the values on your table: $post.Stcc.origUsername
 
You're on the right track. You'll want to place your entity in an Entity sub-directory of your add-on directory. I'd recommend naming it after your table, so your file would be src/addons/Silvertails/CrossForumChat/Entity/Posts.php, the namespace would be Silvertails\CrossForumChat\Entity, and the class name would be Posts. Your short-name must match the add-on ID and class name, so Silvertails\CrossForumChat:Posts. This applies to the relation on the post entity too.

Make sure you've created the class extension for the posts class in the control panel. You'll want to add the relation to the full with-alias in your class extension so it gets eager-loaded (joined):
PHP:
$structure->withAliases['full'][] = 'Stcc'; // or whatever you name the relation

Then you should be able to access the values on your table: $post.Stcc.origUsername
Ok so I have the entity in the sub-directory already but its named with the addon ID instead of the posts currently its called
"CrossForumChat" and the table name is xf_stcc_posts
I will chnage the name now and namespace to be StccPosts and start on the rest

im a confused on
Make sure you've created the class extension for the posts class in the control panel.

Do you mean add the class extensions for the entity to point at my entity extension as below
Screen Shot 2023-05-25 at 6.35.51 pm.webp
 
Ok so I have the entity in the sub-directory already but its named with the addon ID instead of the posts currently its called
"CrossForumChat" and the table name is xf_stcc_posts
I will chnage the name now and namespace to be StccPosts and start on the rest
What you name it isn't especially important, but the file location, namespace, class name, and short-name all must match up accordingly.

Do you mean add the class extensions for the entity to point at my entity extension as below
Yes.
 
What you name it isn't especially important, but the file location, namespace, class name, and short-name all must match up accordingly.


Yes.
ok all in place and getting this one here

Code:
ErrorException: [E_WARNING] Creating default object from empty value in src/XF/Mvc/Entity/Manager.php at line 73
XF::handlePhpError() in src/XF/Mvc/Entity/Manager.php at line 73
XF\Mvc\Entity\Manager->getEntityStructure() in src/XF/Mvc/Entity/Finder.php at line 774
XF\Mvc\Entity\Finder->join() in src/XF/Mvc/Entity/Finder.php at line 761
XF\Mvc\Entity\Finder->join() in src/XF/Mvc/Entity/Finder.php at line 664
XF\Mvc\Entity\Finder->with() in src/XF/Repository/Post.php at line 16
XF\Repository\Post->findPostsForThreadView() in src/XF/Pub/Controller/Thread.php at line 45

Could this be due to there not being a match for the join or there being a null value somewhere?
 
Make sure you're returning your $structure object from your getStructure method.
SO that looks like that was the issue there.
Unfortunately everything loads but doesnt look like anything is loading under

$post.Stcc.origUsername

I might have to dump out all the $post object if I can to see if its being included
 
Make sure you're returning your $structure object from your getStructure method.
Ok that's all working, looks like the post variable isn't pushed into the message_macro which is why it wouldn't show up there. That's ok it's in post_macro so I can just run a template modification on that to insert the code I want.

Thank you so much for your help, it's very much appreciated
 
Back
Top Bottom