XF 2.2 Is there a way to add an "Add Resource" button to a forum where resource threads are generated?

Zaytri

New member
People who are going to be joining my forum won't have much experience with forums, and even less experience dealing with XenForo resources. I have some forums set up where users can't directly make threads in, but if they create a resource, then it will automatically generate a discussion thread in those forums.

However, when going directly to those forums, it is unclear that they need to create a resource to create a thread there. I could create a pinned thread explaining how that works, but is there a way I can improve the user experience by adding the "Add resource" button directly on those forums, similar to the "Post thread" button on forums where users are allowed to post?

And is it possible to do this in such a way that the button will be there for any resource forum, rather than having to edit the code to add that button in for every resource forum I add later?
 
Solution
Ended up just hardcoding conditionals like @Brogan suggested, I'll just have to remember to do this for every resource forum I add, with a fallback in case I add it to the array but forget to set the category ID. Thanks for your help!

Added to forum_view template:
HTML:
<xf:pageaction if="$xf.visitor.canAddResource() AND in_array($forum.node_id, [11,12])">
    <xf:if is="$forum.node_id == 11">
        <xf:set var="$resourceCategoryId" value="2" />
        <xf:elseif is="$forum.node_id == 12"/>
        <xf:set var="$resourceCategoryId" value="3" />
    </xf:if>
  
    <xf:if is="{$resourceCategoryId}">
            <xf:button href="{{ link("resources/categories/{$resourceCategoryId}/add") }}" class="button--cta" icon="write">{{...
You will need to get the code from the xfrm_overview template for the add resource button and add it to the forum_view template.

You can use a conditional statement with an array of forum IDs to ensure it only shows in the relevant forums.

HTML:
<xf:if is="in_array($__globals.forum.node_id, [2,3,4])">
    Add resource button code here
</xf:if>

You may need to modify the code to link directly to the resource page as I suspect the relevant data won't be available to the forum view pages.
 
You will need to get the code from the xfrm_overview template for the add resource button and add it to the forum_view template.

You can use a conditional statement with an array of forum IDs to ensure it only shows in the relevant forums.

HTML:
<xf:if is="in_array($__globals.forum.node_id, [2,3,4])">
    Add resource button code here
</xf:if>

You may need to modify the code to link directly to the resource page as I suspect the relevant data won't be available to the forum view pages.
Got it to work with this, since using $__globals just gives null

HTML:
<xf:pageaction if="$xf.visitor.canAddResource() AND in_array($forum.node_id, [11,12])">
    <xf:button href="{{ link('resources/add') }}" class="button--cta" icon="write" overlay="true">{{ phrase('xfrm_add_resource...') }}</xf:button>
</xf:pageaction>

This is closer to what I want, and does work better than creating a sticky, but clicking the button opens the selector to add to any resource category, rather than just the category related to the forum. Is there a way to reference the resource category from the forum?
 
Last edited:
Learned about {{ dump(vars()) }} which has helped a lot. Forums don't seem have a reference to resource categories (as far as I can tell), but resource categories do have a reference to their linked forums. If I could iterate through that I could probably figure out how to set this up nicely, but I'm not sure how to access the list of resource categories from a forum, and $__globals is null so that doesn't help me either.
 
Ended up just hardcoding conditionals like @Brogan suggested, I'll just have to remember to do this for every resource forum I add, with a fallback in case I add it to the array but forget to set the category ID. Thanks for your help!

Added to forum_view template:
HTML:
<xf:pageaction if="$xf.visitor.canAddResource() AND in_array($forum.node_id, [11,12])">
    <xf:if is="$forum.node_id == 11">
        <xf:set var="$resourceCategoryId" value="2" />
        <xf:elseif is="$forum.node_id == 12"/>
        <xf:set var="$resourceCategoryId" value="3" />
    </xf:if>
  
    <xf:if is="{$resourceCategoryId}">
            <xf:button href="{{ link("resources/categories/{$resourceCategoryId}/add") }}" class="button--cta" icon="write">{{ phrase('xfrm_add_resource') }}</xf:button>
        <xf:else />
        <xf:button href="{{ link('resources/add') }}" class="button--cta" icon="write" overlay="true">{{ phrase('xfrm_add_resource...') }}</xf:button>
    </xf:if>
</xf:pageaction>
Button code taken from xfrm_category_view and xfrm_overview templates
 
Last edited:
Solution
Top Bottom