Announcement

Collapse
No announcement yet.

"on Page load" Hook

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

  • "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.

  • #2
    Hi,

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

    Comment


    • #3
      Originally posted by yuri View Post
      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


      • #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 $entityReadParams $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


        • #5
          Originally posted by rabii View Post
          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 $entityReadParams $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)

        • #6
          Originally posted by mohnewald View Post

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