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
PHP
[...]
... and of course I have a "typical" attachment handler for MyEntity.
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.
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?
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:
// 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);
}
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>
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?