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

XF 2.0 How to get first post attachment?

CyberAP

Well-known member
#1
Pretty self-explanatory, I want to get the first post attachment or just an attachments array which I can work with. Template is thread_list_macros. Is it doable with just template syntax?
 

Chris D

XenForo developer
Staff member
#2
It will probably generate queries unless you add code to fetch these things in advance but if you have a thread record it should be:

Code:
{$thread.FirstPost.Attachments.first()}
 

CyberAP

Well-known member
#4
By using these variables I automatically agree to do all the content checks by myself, correct?
If so what's the best way to do them? I can see that Attachment entity has a has_thumbnail variable, but it is not visible in the dump:

1507893739606.png
 

Chris D

XenForo developer
Staff member
#5
There's a bit of a paradigm shift in XF2 which comes with the move away from plain arrays to objects. Some of the biggest benefits of objects is that things can be dynamically loaded on demand rather than being loaded up front.

You might find, for example, that you get an entity and you know it should have a relation to another table, but you might not see its data listed when you dump the object. That's because it hasn't been fetched in advance but calling it will generate a query to bring that data in. You can see the Data relation here because it is called automatically.

But additionally objects contain a bunch of methods - you can call these directly if you like, e.g. {$attachment.canView()} can be called but you won't see that when you're dumping the entity either.

Sometimes there are methods which are mapped directly to field names, these are called getters. has_thumbnail is a getter and that maps to the hasThumbnail() method. Similary thumbnail_url maps to the getThumbnailUrl() method. These methods and their values won't be visible in the dump, but they are there.

The way to know they are there is to look at the Entity structure, which is generally at the bottom of each Entity class.
 

CyberAP

Well-known member
#6
Thank you Chris, this is crucial for understanding on how to call objects in XF2. Can we hope to see searchable data structures added to XF2 docs in some future?
 

CyberAP

Well-known member
#7
Also, when to use a mapped getter and when to call the method directly? If I try to use $thread.FirstPost.Attachments.first().hasThumbnail() directly I get an error:
Cannot call method hasThumbnail on a non-object (boolean)
But it still gives me the desired value.
 

Chris D

XenForo developer
Staff member
#8
If you are getting the desired value, you shouldn’t be getting the error.

If you are still in the context of a list of threads, does every thread have attachments?
 

CyberAP

Well-known member
#9
If you are getting the desired value, you shouldn’t be getting the error.

If you are still in the context of a list of threads, does every thread have attachments?
Figured this out, when I use first() it gets the first attachment, but if there are none it will return false, which doesn't have a hasThumbnail() method. Now I get why you would use mapped properties instead of direct method calls.