XF 2.2 help with implementing new XenForo content vote system to an add-on, build link, route

Earl

Well-known member
I have vote_count and vote_score columns on my new suggestion entity's table which uses ContentVoteTrait;.

Then again it calls static::addVotableStructureElements($structure); from the inside of
public static function getStructure(Structure $structure){....}

I don't know why these vote_count and vote_score fields are required, since those values also in the xf_content_vote XenForo built-in table, But I guess that's the correct procedure. (Please correct me if I'm wrong)

Here is where my problem comes.
I have this route setup
1607305240855.png


When I use this vote_control macro, it requires this arg-link so it can build the link to the content.

HTML:
    <span class="actionBar-action actionBar-action--vote">
                        <xf:macro template="content_vote_macros" name="vote_control"
                                  arg-link="threads/community-suggestions"
                                  arg-content="{$suggestion}"/>
                    </span>
But it fails because this entity doesn't have the thread_id column. It builds this incorrect link: /index.php?threads/community-suggestion/1 (this link doesn't get built with the thread_id)

But I do have :+int<thread_id,title> part in route format as you can see in the previous screenshot.
Suggestion entity only has a 'content_id' column which is a relation to XF:Thread entity.

My question is: Is having a getter for thread_id in $suggestion entity is the only way to build the link correctly? Isn't there a way to get the thread_id from the relation of the Suggestion entity, and build the link?
Please help

Actually, this whole question is about building links and routes, But I also have posted about this voting system because I haven't seen anyone have had to try to implement it yet. So this thread will help, and also if you spot I'm doing something wrong, please correct me.
 
Last edited:
Solution
When building links, the second argument should be an array (or an object implementing \ArrayAccess, like entities do) containing all of the necessary parameters. The macro in question uses the passed $content entity for this purpose, so that entity will need to have either columns or getters matching the necessary parameters to work properly.

In a more general case, you can also build an array with the parameters and pass it through:
HTML:
{{ link('threads', {'thread_id': $suggestion.content_id, 'title': 'Thread title'}) }}
...though it's usually more convenient to just have them available on the pertinent entity and pass that in instead.

I don't know why these vote_count and vote_score fields are...
When building links, the second argument should be an array (or an object implementing \ArrayAccess, like entities do) containing all of the necessary parameters. The macro in question uses the passed $content entity for this purpose, so that entity will need to have either columns or getters matching the necessary parameters to work properly.

In a more general case, you can also build an array with the parameters and pass it through:
HTML:
{{ link('threads', {'thread_id': $suggestion.content_id, 'title': 'Thread title'}) }}
...though it's usually more convenient to just have them available on the pertinent entity and pass that in instead.

I don't know why these vote_count and vote_score fields are required, since those values also in the xf_content_vote XenForo built-in table, But I guess that's the correct procedure. (Please correct me if I'm wrong)
While the canonical data for votes is in that table, caching these values to the content table is better for performance and makes it trivial to do things like sort by the vote count or score.
 
Solution
Back
Top Bottom