XF 2.0 Show thread title prefix

AndyB

Well-known member
In my Similar threads add-on, I'm able to show the thread title prefix by doing this:

PHP:
            $finder = \XF::finder('XF:Thread');
            $similarThreads = $finder
                ->whereOr($conditions)
                ->order('post_date', 'DESC')
                ->fetch();


and in the template I have:

Code:
<xf:cell href="{{ link('threads/', $thread) }}" target="_blank">{{ prefix('thread', $thread.prefix_id, 'html', '') }} {$thread.title}</xf:cell>


However I'm not able to do the same with my Calendar add-on.

PHP:
        $finder = \XF::finder('Andy\Calendar:Calendar');
        $threads = $finder
            ->with('Thread')
            ->where('calendar_date', 'LIKE', $finder->escapeLike($yearMonth, '%?%'))
            ->fetch();


and in the template I have:

Code:
<a href="{{ prefix('thread', $thread.prefix_id, 'html', '') }} {{ link('threads/', $thread) }}" >{$thread.Thread.title}</a>


I get the following error message when I view the calendar:

1510256690306.webp

What do I need to do?

Thank you.
 
This is my Andy\Calendar\Entity\Calendar.php file:

PHP:
<?php

namespace Andy\Calendar\Entity;

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

class Calendar extends Entity
{
	public static function getStructure(Structure $structure)
	{
		$structure->table = 'xf_andy_calendar';
		$structure->shortName = 'Andy\Calendar:Calendar';
		$structure->primaryKey = 'calendar_id';
		$structure->columns = [
			'calendar_id' => ['type' => self::UINT, 'autoIncrement' => true, 'nullable' => true],
			'thread_id' => ['type' => self::UINT, 'required' => true],
			'calendar_date' => ['type' => self::STR, 'maxLength' => 8, 'default' => ''],
			'user_id' => ['type' => self::UINT, 'required' => true],
		];
		$structure->getters = [];
		$structure->relations = [
			'Thread' => [
				'entity' => 'XF:Thread',
				'type' => self::TO_ONE,
				'conditions' => 'thread_id',
				'primary' => true
			]
		];

		return $structure;
	}	
}
 
Hi XenConcept,

Thank you for your suggestion, but I get the same pink template errors message as shown in post #1.
 
However I'm not able to do the same with my Calendar add-on.

PHP:
$finder = \XF::finder('Andy\Calendar:Calendar');
$threads = $finder
->with('Thread')
->where('calendar_date', 'LIKE', $finder->escapeLike($yearMonth, '%?%'))
->fetch();
and in the template I have:
$threads is a collection of Calendar entries right? Therefore your calendar entity doesn't have a prefix_id - that would be on the thread entity. So you need to get from the Calendar entity to the related Thread entity. Therefore the template code would be:
HTML:
<a href="{{ prefix('thread', $thread.Thread.prefix_id, 'html', '') }} {{ link('threads/', $thread.Thread) }}" >{$thread.Thread.title}</a>
Note that you were already accessing the thread title properly... same principle.
 
Than you, Chris. That worked perfectly.

Code:
<a href="{{ link('threads/', $thread) }}">{{ prefix('thread', $thread.Thread.prefix_id, 'html', '') }} {$thread.Thread.title}</a>
 
Last edited:
Shouldn’t the link be $thread.Thread too?

Here's a bit more of the code, perhaps it will explain why either way works.

Code:
            <xf:foreach loop="$threads" value="$thread">

                <xf:if is="{$thread.calendar_date} == {$listday.calendarDate}">
                    <a href="{{ link('threads/', $thread) }}">{{ prefix('thread', $thread.Thread.prefix_id, 'html', '') }} {$thread.Thread.title}</a>
                    <br /><br />
                </xf:if>
                
            </xf:foreach>
 
I know it will work, as I assume the Calendar Entity has a thread_id field. But it doesn’t have a title field. So it will generate links like ‘threads/123’ rather than ‘threads/thread-title.123’.
 
Thank you, Chris. What great support you provide.

Now working perfectly.

Code:
            <xf:foreach loop="$threads" value="$thread">

                <xf:if is="{$thread.calendar_date} == {$listday.calendarDate}">
                    <a href="{{ link('threads/', $thread.Thread) }}">{{ prefix('thread', $thread.Thread.prefix_id, 'html', '') }} {$thread.Thread.title}</a>
                    <br /><br />
                </xf:if>
                
            </xf:foreach>
 
Top Bottom