Announcement

Collapse
No announcement yet.

Case hook

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

  • Case hook

    Hi guys,
    i need help creating a case hook.
    This is what i have done so far:
    Created the Hook file in "custom\Espo\Custom\Hooks\Caseobj\CaseName.php " with this content (i used "Caseobj" instead of "Case" to avoid php keyword problem as explained in another topic) :

    PHP Code:
    <?php
    namespace Espo\Custom\Hooks\Caseobj;

    use 
    Espo\ORM\Entity;


    class 
    CaseName extends \Espo\Core\Hooks\Base
    {

        public function 
    beforeSave(Entity $entity, array $options = [])
        {                            
                
    $GLOBALS['log']->debug('Test');                      

        }

    }

    ?>

    After Rebuilding the Backend and cleaning the cache i tried to create a new Case but the Hook was not fired as expected.
    So i checked the content of hooks.php in "data\cache\application":

    PHP Code:
    <?php
    return array (
      
    'Caseobj' => 
      array (
        
    'afterSave' => 
        array (
          
    => 
          array (
            
    'className' => '\\Espo\\Custom\\Hooks\\Caseobj\\CaseName',
            
    'order' => 9,
          ),
        ),
      ),................
    And, as you can see, the code is referencing the wrong entity "Caseobj" instead of "Case".
    By changing hooks.php from "return array ( 'Caseobj'" to "return array ( 'Case'" the hook start to fire as expected, but every time i rebuild the backend, hooks.php is recreated with the wrong Case entity (caseobj).
    So i really don't understand how am i supposed to create a correct Hook for the Case entity.
    Thank you very much in advance for the support.











  • #2
    Hi and welcome to this forum

    When you state:
    namespace Espo\Custom\Hooks\Caseobj;
    you are telling Espo to look for a file located at the custom/Espo/Custom/Hooks/CaseObj/ folder.

    If you need to create a custom hook for the Case entity, you need to create your file as:
    custom\Espo\Custom\Hooks\Case\CaseName.php
    and specify the namespace as
    namespace Espo\Custom\Hooks\Case;
    The way Espo routing works for Case, is that it will look for hooks defined in the "Custom" module folder for the entity Case and expexts to find it in (custom\Espo\Custom\Hooks\Case\), so when you stated "Caseobj" in the namespace declaration it is looking for a hook for a "Caseobj" entity which doesn't exist.

    The folder data\cache\application is a temporary storage place (I believe that is meant to improve performance as part of Espo's design) and it is re-created each time that you rebuild the backend, that's why any changes here are lost.

    Here's an example of a Custom hook for the out of the box Contact entity that we use for our implementation, note how the folder where the hook script is stored and last part of the namespace are "Contact" which is the entity that we want to affect:

    file: custom\Espo\Custom\Hooks\Contact\ContactHooks.php
    Code:
    <?php
    
    namespace Espo\Custom\Hooks\[COLOR=#FF0000]Contact[/COLOR];
    
    use Espo\ORM\Entity;
    
    class ContactHooks extends \Espo\Core\Hooks\Base
    {
    
        public function beforeSave(Entity $entity, array $options=array())
        {    
            // fill the displayPhoneNumer field using the main contact telephone number
            $phoneNumberObjects = $entity->get('phoneNumbers');
            if(count($phoneNumberObjects)>0) {
                $phoneNumber = $phoneNumberObjects[0]->get('name');
                $entity->set('displayPhoneNumber',$phoneNumber);
            }        
        }
    
    }
    Last edited by telecastg; 03-03-2020, 08:52 PM.

    Comment


    • yuri
      yuri commented
      Editing a comment
      \Espo\Core\Hooks\Base has been deprecated for long. Not to be extended.

  • #3

    Originally posted by telecastg View Post
    Hi and welcome to this forum

    When you state: you are telling Espo to look for a file located at the custom/Espo/Custom/Hooks/CaseObj/ folder.

    If you need to create a custom hook for the Case entity, you need to create your file as: and specify the namespace as

    The way Espo routing works for Case, is that it will look for hooks defined in the "Custom" module folder for the entity Case and expexts to find it in (custom\Espo\Custom\Hooks\Case\), so when you stated "Caseobj" in the namespace declaration it is looking for a hook for a "Caseobj" entity which doesn't exist.

    The folder data\cache\application is a temporary storage place (I believe that is meant to improve performance as part of Espo's design) and it is re-created each time that you rebuild the backend, that's why any changes here are lost.

    Here's an example of a Custom hook for the out of the box Contact entity that we use for our implementation, note how the folder where the hook script is stored and last part of the namespace are "Contact" which is the entity that we want to affect:

    file: custom\Espo\Custom\Hooks\Contact\ContactHooks.php
    Code:
    <?php
    
    namespace Espo\Custom\Hooks\[COLOR=#FF0000]Contact[/COLOR];
    
    use Espo\ORM\Entity;
    
    class ContactHooks extends \Espo\Core\Hooks\Base
    {
    
    public function beforeSave(Entity $entity, array $options=array())
    {
    // fill the displayPhoneNumer field using the main contact telephone number
    $phoneNumberObjects = $entity->get('phoneNumbers');
    if(count($phoneNumberObjects)>0) {
    $phoneNumber = $phoneNumberObjects[0]->get('name');
    $entity->set('displayPhoneNumber',$phoneNumber);
    }
    }
    
    }
    Hi telecastg, thank you very much for your response.
    The problem is that i can't create a PHP class specifying the namespace 'Espo\Custom\Hooks\Case' as you suggested, because "Case" is a reserved word in PHP.
    So , as suggested in this thread https://forum.espocrm.com/forum/deve...se-entity-hook, i used the Caseobj word, but, as you can see from the generated hooks.php, it's not working.
    Can you give me an example on how i should write a correct case hook while avoiding PHP reserved words?

    Thank you in advance for your support, i really appreciate it.


    Comment


    • #4
      Hi Ceonello, I apologize for giving you the wrong information for your specific issue.

      I don't have experience customizing the "Case" entity, because we don't use it in our application, and what I told you only applies for entities which names are not reserved PHP words.

      Doing some further research, I have learned that CaseObj is the actual name of the Case entity inside the php code so the routing will work if you name your class CaseObj and don't create a "Case" namespace.

      Following the namespace syntax used by Maximus in this post https://forum.espocrm.com/forum/deve...-to-new-entity I beleive that a hook for a Case entity should be like this:

      file: /custom/Espo/Custom/Hooks/CaseObj.php
      Code:
      <?php
      
      namespace Espo\Custom\Hooks;
      
      use Espo\ORM\Entity;
      
      class CaseObj extends \Espo\Core\Hooks\Base
      {
        public function beforeSave(Entity $entity, array $options=array())
        {
          // do something
        }
      }
      I haven't tested it because as I said, we don't use the Case entity in our application, but please post the results here after you have tested it.

      Thanks
      Last edited by telecastg; 03-10-2020, 04:08 AM.

      Comment


      • #5
        Originally posted by telecastg View Post
        Hi Ceonello, I apologize for giving you the wrong information for your specific issue.

        I don't have experience customizing the "Case" entity, because we don't use it in our application, and what I told you only applies for entities which names are not reserved PHP words.

        Doing some further research, I have learned that CaseObj is the actual name of the Case entity inside the php code so the routing will work if you name your class CaseObj and don't create a "Case" namespace.

        Following the namespace syntax used by Maximus in this post https://forum.espocrm.com/forum/deve...-to-new-entity I beleive that a hook for a Case entity should be like this:

        file: /custom/Espo/Custom/Hooks/CaseObj.php
        Code:
        <?php
        
        namespace Espo\Custom\Hooks;
        
        use Espo\ORM\Entity;
        
        class CaseObj extends \Espo\Core\Hooks\Base
        {
        public function beforeSave(Entity $entity, array $options=array())
        {
        // do something
        }
        }
        I haven't tested it because as I said, we don't use the Case entity in our application, but please post the results here after you have tested it.

        Thanks
        Sorry for the late response, and thank you very much for the info.
        I will test it as soon as i can and post the results.

        Many thanks

        Comment


        • #6
          \Espo\Core\Hooks\Base has been deprecated for long. Not to be extended.

          Comment

          Working...
          X