beforeRead hook

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kharg
    Senior Member
    • Jun 2021
    • 410

    beforeRead hook

    So, I am trying to setup a beforeRead hook but it isn't working correctly.

    I just want to update my status field from "Unread" to "Read" once the record has been opened but after I refresh the list view the status field is changed back to "Unread"
    List api returns a "Unread" status but the record api gives the status "Read"


    This is my hook:

    Code:
    <?php
    
    namespace Espo\Custom\Hooks\WhatsApp;
    
    use Espo\ORM\Entity;
    use Espo\Core\Record\ReadParams;
    use Espo\Core\Record\Hook\ReadHook;
    
    class HasBeenRead implements ReadHook
    {
        public function process(Entity $entity, ReadParams $params): void
        {
            $entity->set('status', 'Read');
        }
    }
    Any tips?
    Last edited by Kharg; 08-24-2022, 07:18 PM.
  • mozkomor
    Junior Member
    • Feb 2021
    • 12

    #2
    You have to save the entity. Example:


    PHP Code:
    <?php
    
    namespace Espo\Custom\Hooks\WhatsApp;
    
    use Espo\ORM\Entity;
    use Espo\ORM\EntityManager;
    use Espo\Core\Record\ReadParams;
    use Espo\Core\Record\Hook\ReadHook;
    
    class HasBeenRead implements ReadHook
    {
        private EntityManager $entityManager;
    
        public function __construct(EntityManager $entityManager)
        {
            $this->entityManager = $entityManager;
        }
    
       public function process(Entity $entity, ReadParams $params): void
       {
          $entity->set('status', 'Read');
          $this->entityManager->saveEntity($entity);
       }
    }

    Comment

    • Kharg
      Senior Member
      • Jun 2021
      • 410

      #3
      This solved my issue, thank you so much!

      Comment

      Working...