XF 2.2 Entity with multiple bbcode columns (<xf:editor>) + each editor to support each own attachments

Scandal

Well-known member
Hello guys!

Well I have a question: let's say we have an entity MyEntity with text column "description".
This column is populated via the:
HTML / template
HTML:
<xf:editorrow name="description" [...]>
<xf:formrow>
         <xf:if is="$attachmentData">
        <xf:macro template="public:helper_attach_upload" name="upload_block"
                    arg-attachmentData="{$attachmentData}"
                    arg-forceHash="{$myentity.draft_entity.attachment_hash}" />
        </xf:if>
</xf:formrow>
PHP
PHP:
// for getting the value after form submit
$myEntity->description = $this->plugin('XF:Editor')->fromInput('description');
[...]
PHP:
// during load of the template / form
            $attachments = [];

            $tempHash = $this->filter('attachment_hash', 'str');

            $attachmentRepo = $this->repository('XF:Attachment');
            $attachmentData = $attachmentRepo->getEditorData('sc_myentity', $myentity, $tempHash);
            $attachments = $attachmentData['attachments'];  
//************
// during insert
 $inserter = $this->service('XF:Attachment\Preparer');
            $associated = $inserter->associateAttachmentsWithContent($this->filter('attachment_hash', 'str'), 'sc_myentity', $myEntity->id);
            if ($associated)
            {
                $myEntity->fastUpdate('attach_count', $myEntity->attach_count + $associated);
            }
... and of course I have a "typical" attachment handler for MyEntity.
PHP:
namespace Scandals\System\Attachment;

use XF\Entity\Attachment;
use XF\Mvc\Entity\Entity;
use XF\Attachment\AbstractHandler;
use Scandals\System\Entity\MyEntity as MyEntity;

class MyEntity extends AbstractHandler
{
    public function canView(
        Attachment $attachment,
        Entity $container,
        &$error = null
    )
    {
        return true;
    }

    public function canManageAttachments(array $context, &$error = null)
    {
        return true;
    }

    public function onAttachmentDelete(
        Attachment $attachment,
        Entity $container = null
    )
    {
        if (!$container)
        {
            return;
        }

        $container->attach_count--;
        $container->save();
    }


    public function getConstraints(array $context)
    {
        return \XF::repository('XF:Attachment')->getDefaultAttachmentConstraints();
    }

    public function getContainerIdFromContext(array $context)
    {
        return isset($context['id']) ? intval($context['id']) : null;
    }

    public function getContainerLink(Entity $container, array $extraParams = [])
    {
        return \XF::app()->router('public')->buildLink(
            'mypub',
            $container,
            $extraParams
        );
    }
  
    public function getContext(Entity $entity = null, array $extraContext = [])
    {
        if ($entity instanceof MyEntity)
        {
            $extraContext['id'] = $entity->id;
        }
        else
        {
            //throw new \InvalidArgumentException(
            //    "Entity must be a MyEntity"
            //);
        }

        return $extraContext;
    }
}

My problem: I want to add multiple xf:editor form fields on the same entity/ form/ with each one editor to have its own attachments support.
HTML:
<xf:editorrow name="description1" [...]>
<xf:formrow>
         <xf:if is="$attachmentData1">
        <xf:macro template="public:helper_attach_upload" name="upload_block"
                    arg-attachmentData="{$attachmentData1}"
                    arg-forceHash="{$myentity.draft_entity_desc1.attachment_hash}" />
        </xf:if>
</xf:formrow>

<xf:editorrow name="description2" [...]>
<xf:formrow>
         <xf:if is="$attachmentData2">
        <xf:macro template="public:helper_attach_upload" name="upload_block"
                    arg-attachmentData="{$attachmentData2}"
                    arg-forceHash="{$myentity.draft_entity_desc2.attachment_hash}" />
        </xf:if>
</xf:formrow>
But I don't know what changes on the handler and php I have to do.
For me to use only one xf:editor with attachments support is easy, but I don't know how to create multiple xf:editor with their own attachments support.

Any hint? :)
 
Any idea? :)

Maybe there is a way to use multiple <xf:editorrow> and one <xf:macro template="public:helper_attach_upload" name="upload_block">?
 
Is each editor tied to a separate entity? Attachments are only associated with a single content type/value pair, so otherwise I don't believe there is any means to tie them to a particular editor instance on the same entity.
 
No it is not on separate entity.
I need two entity columns which will have bbcode text / attachments, in one entity.

It seems to work to have two <xf:editorrow name="descriptionX" [...]> and one <xf:macro template="public:helper_attach_upload" name="upload_block" [...], but it has some bugs on the insert function.
But if I have focus on the <xf:editorrow> I want, it inserts correctly the attachments, for example if I try to upload images. If I need to insert afterwards the attachments from the <xf:macro template="public:helper_attach_upload"> block at the bottom, it inserts it on the first xf:editor of the page, even if I have focus on the second one.

So no solution, right?
 
Not that I can readily think of. Consider editing such an entity for example, the attachments would have no way of knowing which editor they were uploaded with.
 
Yes, seems right.
But at least you can add the same $attachmentData on both <xf:editor's in order to show the uploaded images when the bbcode render mode is enabled:
HTML:
<xf:editorrow name="description1"
    attachments="{{ $attachmentData ? $attachmentData.attachments : [] }}"
   
<xf:editorrow name="description2"
    attachments="{{ $attachmentData ? $attachmentData.attachments : [] }}"
 
Hey guys, I have a new question:
Let's say I splitted the entities into two different, so now I could have one <xf:editor> (+ attachments) per entity.

Can I put both <xf:editor> fields on the same page / <xf:form> and via clicking Submit / Save, to save the attachments on the correct entity?
Could someone give some hints for this part of code which is related to one entity and attachent_hash?

PHP:
// during load of the template / form
            $attachments = [];

            $tempHash = $this->filter('attachment_hash', 'str');

            $attachmentRepo = $this->repository('XF:Attachment');
            $attachmentData = $attachmentRepo->getEditorData('sc_myentity', $myentity, $tempHash);
            $attachments = $attachmentData['attachments'];
            //************
            // during insert
            $inserter = $this->service('XF:Attachment\Preparer');
            $associated = $inserter->associateAttachmentsWithContent($this->filter('attachment_hash', 'str'), 'sc_myentity', $myEntity->id);
            if ($associated)
            {
                $myEntity->fastUpdate('attach_count', $myEntity->attach_count + $associated);
            }
I guess that each <xf:editor> have to POST its own attachment_hash, but I can't see any arg on <xf:editor> to change the default attachment_hash form field to something like attachment_hash1 for the one editor and attachment_hash2 for the other editor.

Any idea? :)
 
Haven't played around much with it but it's the attachment macros which contain the attachment_hash hidden input, not the editor. The macros take a hiddenName argument to set the input name:

HTML:
<xf:macro template="helper_attach_upload" name="upload_block"
    arg-attachmentData="{$yourEntityAttachmentData}"
    arg-forceHash="{$yourEntityHash}"
    arg-hiddenName="your_entity[attachment_hash]" />
 
Top Bottom