XF 2.1 When are attachments linked to posts?

Jaxel

Well-known member
I am trying to run a function when a post is made. It's based on what images are attached to that post.

I've tried running the function in the _postSave() function of a post; but at that point, the post will still have zero attachments linked to it... even though I am posting with attachments.

How would I get the attachments to a post immediately after a post is made?
 
Would it be alright to ask what your function is/looks like? I might be able to help with this since I had to wrestle with the attachment system not too long ago.

The attachment system is surprisingly complicated and attachment_ids aren't directly stored in the Post entity like one might anticipate. Instead they're fetched via a relation. In my own addons I typically use a handler to insert the attachment_id into an entity column, but posts don't follow that pattern. Instead the Attachment entity stores the post_id in its content_id entry. Take a look at the Attachment relation in \XF\Entity\Post and you'll see what I mean.

Are you looking exclusively for attachments embedded in a post? You might be able to grab them from the $post->embed_metadata JSON array. Otherwise I think the $post->Attachments relation is your only option, but I'm not certain if that's available during _postSave(). If not, you might be able to create a handler for your attachments and run the function there. Take a look at \XF\Attachment\AbstractHandler and the onAssociation() function in particular.
 
Last edited:
Back
Top Bottom