• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Include Content Title in URLs and xen:link

AndyB

Well-known member
#1
Some forums use the "Include Content Title in URLs" and some don't. I would like my add-on to create a link for either situation.

pic001.jpg

In one of my add-ons, I would like to include a link to a forum. How would I code the xen:link to take into account the option of "Include Content Title in URLs".

This template example works fine if the "Include Content Title in URLs" is NOT checked.
Code:
<xen:foreach loop="$posts" value="$post">

<xen:if is="{$xenOptions.includeTitleInUrls} == 0">
<a href="{xen:link 'forums/{$post.forum_id}'}" />{$post.forum_title}</a>
</xen:if>

<xen:if is="{$xenOptions.includeTitleInUrls} == 1">
<a href="{xen:link 'forums/{$post.forum_id}'}" />{$post.forum_title}</a>
</xen:if>

</xen:foreach>

How would I change this code so that if will include the "Title in URLs".
Code:
<xen:if is="{$xenOptions.includeTitleInUrls} == 1">
<a href="{xen:link 'forums/{$post.forum_id}'}" />{$post.forum_title}</a>
</xen:if>
Can this be done at the template level? Or do I need to pass in a variable with the TitleURL, if so what is the PHP code?

Thank you.
 
Last edited:

AndyB

Well-known member
#2
Looks like I can create a variable called $linkTitle and pass that into my template.

PHP:
// includeTitleInUrls option
$forumLink = XenForo_Link::buildIntegerAndTitleUrlComponent($node['node_id'], $node['title'], true);
Edit: This is not that easy because I'm dealing with a multi-dimensional array and I would need to add $forumLink to my variable.
 
Last edited:

Jeremy

Well-known member
#3
xen:link handles a lot of what you do manually. Look around the templates for how xen:link is used (ie, the latest posts on the forum home)
 

AndyB

Well-known member
#4
Okay I got it.

PHP:
// foreach count
$i = -1;

// get child node titles
foreach ($posts as $k => $v)
{
    // foreach count
    $i = $i + 1;
                
    // includeTitleInUrls option
    $forum_link = XenForo_Link::buildIntegerAndTitleUrlComponent($v['forum_id'], $v['forum_title'], true);
    
    // merge arrays
    $posts[$i] = array_merge($posts[$i], array('forum_link' => $forum_link));
}
and the template code:

Code:
<xen:foreach loop="$posts" value="$post">

<a href="{xen:link 'forums/{$post.forum_link}'}" />{$post.forum_title}</a>

</xen:foreach>
 

Jeremy

Well-known member
#5
You've ignored everything I've said. xen:link already does all of the work for you if you were to follow naming conventions.

Take a look at how xen:link is used in the templates. Than take a look at XenForo_Link::_buildLink, than take a look at XenForo_Route_Prefix_Forums::buildLink().