Announcement

Collapse
No announcement yet.

How I can access file content from Document hook ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How I can access file content from Document hook ?

    How I can access file content from Document hook ?

    ----------------------------------------

    namespace Espo\Custom\Hooks\Document;

    use Espo\ORM\Entity;
    use Espo\Core\Utils\Util;


    class CustomDocHook extends \Espo\Core\Hooks\Base
    {
    ......
    public function beforeSave(Entity $entity, array $options = [])
    {
    $GLOBALS['log']->warning( 'beforeSave CustomDocHook ' . $entity->id);

    try
    {

    $entity ??????? how I can get file content here ?

  • #2
    The file is linked to the attachment entity. You can call the getFileData function in the attachment service class and pass the file id to it.

    Comment


    • #3
      Thank you very much for your answer.
      But how I can get attachment entity from document hook ?

      Comment


      • #4
        Inject the service class in the constructor like:
        Code:
        use Espo\Services\Attachment;
        
        class Document{
          public function __construct(Attachment $att)
          {
            $this->attachmentService = $att;
          }
        }
        Then use $this->attachmentService from the beforeSave function to call the getFileData function.

        Comment


        • #5
          I'm very much sorry. How I can get attachment entity from Document entity ?

          Comment


          • #6
            You don't really need the Attachment entity, you just need it's ID.

            You can get the ID by going: $entity->get('fileId').

            If you need the Attachment, then:
            Code:
            $attachmentEntity = $this->entityManager->getEntity('Attachment', $entity->get('fileId'));

            Comment

            Working...
            X