AndrewSimm
Well-known member
I am creating an articles add-on that allows someone with permission to promote a post by clicking a link on the post (similar to the demo addon). Once the link is clicked an overlay loads where the title, date, section, and preview can be changed or added. I have everything working except adding an attachment.
My first question is around the entity. I am not extending anything, The article table stands on its own. What do I need to put into the entity and why does the existing article entity not work?
My second question is what would go in $article in step 4 below? When the form loads the article has yet to be created, so there is no key that I can enter. What I do grab is the thread_id and post_id and insert those into the articles table after the form is submitted.
1) Enter input into the template
2) Create an entity
3) Create a attachment_handler_class under development -> content_types
Content type: article
Field: attachment_handler_class
Value: Andrew\Articles\Attachment\Article
Add-on: [Andrew] Articles
4) Add the below code to your controller that will display the attachment upload button
5) Add the below code to your controller where you save the form
My first question is around the entity. I am not extending anything, The article table stands on its own. What do I need to put into the entity and why does the existing article entity not work?
My second question is what would go in $article in step 4 below? When the form loads the article has yet to be created, so there is no key that I can enter. What I do grab is the thread_id and post_id and insert those into the articles table after the form is submitted.
1) Enter input into the template
Code:
<xf:macro template="helper_attach_upload" name="upload" arg-attachmentData="{$attachmentData}" />
2) Create an entity
3) Create a attachment_handler_class under development -> content_types
Content type: article
Field: attachment_handler_class
Value: Andrew\Articles\Attachment\Article
Add-on: [Andrew] Articles
4) Add the below code to your controller that will display the attachment upload button
Code:
/** @var \XF\Repository\Attachment $attachmentRepo */
$attachmentRepo = $this->repository('XF:Attachment');
$attachmentData = $attachmentRepo->getEditorData('article', $article);
5) Add the below code to your controller where you save the form
Code:
/** @var \XF\Service\Attachment\Preparer $inserter */
$inserter = $this->service('XF:Attachment\Preparer');
$associated = $inserter->associateAttachmentsWithContent($this->filter('attachment_hash'), 'article', $article->article_id);
if ($associated)
{
$article->fastUpdate('attach_count', $article->attach_count + $associated);
}