XF 2.1 Implement Tags to Entity

peregm

Member
Hi there!
I want to implement the usage of Tags for my custom entity, just like you can do with Threads. I've been doing some research reading the code that is used when creating a Thread, and so far this is what I got:

* I am assuming that my new Entity is called Attachment, and its content_type is defined as mf_attachment.

Entity structure behaviour (MyAddon/Entity/Attachment.php)
I've defined my behaviour and content_type just like it is done in the Thread entity:
PHP:
public static function getStructure(Structure $structure)
    // Other structure definitions...
    $structure->behaviors = [
        'XF:Taggable' => ['stateField' => 'processed']
    ];

    $structure->contentType = 'mf_attachment';
}

And, with this, I've also defined a new content type through the admin panel with the following values:
  • Content type: mf_attachment
  • Field: entity
  • Value: MyAddon:Attachment
in order to associate my custom content type to my entity.

Tag handler
I've seen that I must create a Tag handler, and just like with the Thread entity, I've created a class inside my Tags folder: MyAddon/Tag/Attachment.php.
This class just extends the \XF\Tag\AbstractHandler class and implements all of its abstract methods.

Following with that, I also needed to create another content type through the admin panel:

  • Content type: mf_attachment
  • Field: tag_handler_class
  • Value: MyAddon\Tag\Attachment
Attachment service
I've seen that tags get created inside the Thread service creator, so I also created a service class which in fact, uses the TagChanger service.
So, in the part of my code where I get to create the attachment entities, I'm using this TagChanger service for creating my tags:

PHP:
/** @var XF\Entity\Post $post */
$this->tagChanger = $this->service('XF:Tag\Changer', 'mf_attachment', $post);

// ...

if ($this->tagChanger->canEdit()) {
    // $tags = 'tag1test, tag2test'
    $this->tagChanger->setEditableTags($tags);
}

// ...

$this->tagChanger->save();

So far with this, I can successfully create tags via my custom attachment form, but I cannot get it to associate to my attachments.
In my xf_tag table, I can see my newly created records (tag1test and tag2test):
Screenshot 2019-10-16 at 17.08.38.webp

But in my xf_tag_content I can't see my content associated (the first two entries were already there, created for a thread):
Screenshot 2019-10-16 at 17.10.11.webp

I've I'm not mistaken, I should have one entry for each tag I associated with my Attachment entity, with the content_type column having a "mf_attachment" value.

So, what I guess I'm missing is the part that I actually associate the tags to my Attachment entity, but I can't find anywhere in the Thread creation process where this is done.
I've also noticed that there is a tags column in the xf_thread table, which holds a JSON array with the names of the tags. I assume it would be nice to also have this with my attachment.

What am I missing? I know I could create a procedure to actually create those records in the corresponding tables, but I know there is some part of the Thread code that actually does that. So instead of repeating code, it would be nice to reuse it.

Thanks!
 
Back
Top Bottom