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

Attachments in custom add-ons?

Discussion in 'XenForo Development Discussions' started by dmnkhhn, Oct 10, 2010.

  1. dmnkhhn

    dmnkhhn Active Member


    Has anyone found out how the attachment system works?

    An attachment requires a content_type and a content_id - from my understanding I have to create a custom content_type and the content_id refers to the unique id of the content with that custom type.

    There are two tables in the database related to the content_type

    xf_content_type_field has the name, field name and field value.
    content_type = whatever you want, seems to be lowercase, one word only
    field_name = no idea
    field_value = XenForo_AttachmentHandler_Post which means Post.php in the directory XenForo/AttachmentHandler/

    The tricky parts seems to be xf_content_type
    content_type = same as above
    addon_id = id of the add-on that uses this content type
    fields = this field is a mediumblob and I have no idea what it is and what it contains

    AFAIK there is no way to create a new content type without doing it in the database so I am wondering if it is even possible to use the XenForo 1 beta 1 attachment system in a custom add-on. :)

    Please change the thread title to: Attachments in custom add-ons?
  2. Brogan

    Brogan XenForo Moderator Staff Member

    Done :)
  3. g0rn

    g0rn Well-Known Member

    It contains serialized array of handlers as they are defined in xf_content_type_fields table (see XenForo_Model_ContentType class, rebuildContentTypeCache() method).
    Yes, I think you have to manually insert all needed things to the db.

    I can't see any problems: create new content_type, assign attachment handler to it, create attachment handler class and define methods like _canViewAttachment and then use XenForo_Model_Attachment model to get your attachments, upload them etc.
  4. dmnkhhn

    dmnkhhn Active Member

    Oh well, not complicated at all. ;)

    I just did a quick test, filled the fields in the database table and left 'fields' blank.

    Then, I added this to my model
    and (drum roll please)... 'fields' is not empty anymore. :)

    I guess the rebuilding needs to be done after the install process of an add-on created the database entries.
    ragtek likes this.
  5. dmnkhhn

    dmnkhhn Active Member

    Got it working. ;)

    It is a bit cobbled at the moment (I have removed all the permission stuff) but I can pick a file (any file, I am going to limit it to certain file extensions) and upload it.
    Next step: Use the DataWriter to save and attach the attachment to my custom content_type

    The uploading works, I can edit a car and attach/remove images.
    One important thing was missing in my DataWriter: A _postSave() method that calls the function responsible for the association of the attachment(s).
    The upload still works but the attachment does not know where it belongs to.

    Next step: Display the attachment on the detail page of the car.

    After that: Write a small guide about the stuff I am currently doing. ;)
  6. JulianD

    JulianD Well-Known Member

    Hey, did you write this guide? I'm very interested about it!

Share This Page