"on Page load" Hook

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohnewald
    Junior Member
    • Apr 2022
    • 5

    "on Page load" Hook

    Hi,

    is there any possiblity to Hook into the page load from PHP?

    I'd like to set certain fields when the user loads a certain Object.

    JavaScript is not an option, it must be PHP.
  • yuri
    Member
    • Mar 2014
    • 8451

    #2
    Hi,

    You can use the before-read record hook: https://docs.espocrm.com/development...kclassnamelist. It executes on read API call.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • mohnewald
      Junior Member
      • Apr 2022
      • 5

      #3
      Originally posted by yuri
      Hi,

      You can use the before-read record hook: https://docs.espocrm.com/development...kclassnamelist. It executes on read API call.

      Thanks for the suggestion. I've tried some sample code:

      Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.


      But it does not seem to work. Is there any working implementation that you can share?

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #4
        Given your example i assume your custom entity is called connection, try the steps below:

        1 - Create your php file under Espo\Custom\Hooks\Connection\BeforeReadConnectionC heck.php

        PHP Code:
        <?php
        
        namespace Espo\Custom\Hooks\Connection;
        
        use Espo\Core\Record\Hook\ReadHook;
        use Espo\Core\Record\ReadParams;
        
        use Espo\ORM\Entity;
        
        
        /**
         * @implements ReadHook<\Espo\Custom\Entities\Connection>
         */
        class BeforeReadConnectionCheck implements ReadHook
        {
            public function process(Entity $entity, ReadParams $params): void
            {
        
                $entity->set([
                    'status' => 'Read',
                    'description' => 'Status has been set as Read via ReadHook'
                ]);
        
            }
        }

        2 - map the file on the recordDefs under Espo\Custom\Reources\metadata\recordDefs\Connectio n.json

        PHP Code:
        {
            "beforeReadHookClassNameList": [
                "Espo\\Custom\\Hooks\\Connection\\BeforeReadConnectionCheck"
            ]
        } 
        

        This should work fine, tested.
        Rabii
        Web Dev

        Comment

        • mohnewald
          Junior Member
          • Apr 2022
          • 5

          #5
          Originally posted by rabii
          Given your example i assume your custom entity is called connection, try the steps below:

          1 - Create your php file under Espo\Custom\Hooks\Connection\BeforeReadConnectionC heck.php

          PHP Code:
          <?php
          
          namespace Espo\Custom\Hooks\Connection;
          
          use Espo\Core\Record\Hook\ReadHook;
          use Espo\Core\Record\ReadParams;
          
          use Espo\ORM\Entity;
          
          
          /**
          * @implements ReadHook<\Espo\Custom\Entities\Connection>
          */
          class BeforeReadConnectionCheck implements ReadHook
          {
          public function process(Entity $entity, ReadParams $params): void
          {
          
          $entity->set([
          'status' => 'Read',
          'description' => 'Status has been set as Read via ReadHook'
          ]);
          
          }
          }

          2 - map the file on the recordDefs under Espo\Custom\Reources\metadata\recordDefs\Connectio n.json

          PHP Code:
          {
          "beforeReadHookClassNameList": [
          "Espo\\Custom\\Hooks\\Connection\\BeforeReadConnectionCheck"
          ]
          } 
          

          This should work fine, tested.
          Thank you. I cannot find the sub directory Espo\Custom\Reources\metadata\recordDefs\ , do I just need to create this directory and the Connection.json?

          Comment


          • rabii
            rabii commented
            Editing a comment
            All of the should go into custom main directory (if you can't find find recordDefs folder you just need to create one)
        • mohnewald
          Junior Member
          • Apr 2022
          • 5

          #6
          Originally posted by mohnewald

          Thank you. I cannot find the sub directory Espo\Custom\Reources\metadata\recordDefs\ , do I just need to create this directory and the Connection.json?
          We tried what you said and cleared the cache, but the script is not triggered. We are on Espo version: 5.5.6

          Comment


          • rabii
            rabii commented
            Editing a comment
            I am using latest version. Not sure if that is available for version 5.5.6
        Working...