How I can access file content from Document hook ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iioi
    Member
    • Jun 2022
    • 48

    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 ?
  • AgentT
    Member
    • Aug 2021
    • 77

    #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

    • iioi
      Member
      • Jun 2022
      • 48

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

      Comment

      • AgentT
        Member
        • Aug 2021
        • 77

        #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

        • iioi
          Member
          • Jun 2022
          • 48

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

          Comment

          • AgentT
            Member
            • Aug 2021
            • 77

            #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...