Not all hooks are executing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tom
    Junior Member
    • Jan 2019
    • 6

    Not all hooks are executing

    I have entity MyEntity provided in module MyModuleOne. In this module I wrote hook for this entity:
    PHP Code:
    namespace Espo\Modules\MyModuleOne\Hooks\MyEntity;
    use Espo\Orm\Entity;  
    class MyEntityHook extends \Espo\Core\Hooks\Base
    {
        public function afterSave(Entity $entity, array $options = [])
        {
            ...
        }
    } 
    

    I have another module MyModuleTwo. In this module I wrote another hook for this entity:
    PHP Code:
    namespace Espo\Modules\MyModuleTwo\Hooks\MyEntity;  
    use Espo\Orm\Entity;  
    class MyEntityHook extends \Espo\Core\Hooks\Base
    {
       public function beforeSave(Entity $entity, array $options = [])
       {
           ...
       }
    
       public function afterSave(Entity $entity, array $options = [])
       {
           ...
       }
    } 
    

    MyModuleOne has order 50, MyModuleTwo is 100.

    When I edit MyEntity in UI I see that hooks are executing:
    1) beforeSave from MyModuleTwo
    2) afterSave from MyModuleOne
    ... thats it
    afterSave from MyModuleTwo is not executed.

    When I changed filename and classname in MyModuleTwo - every hooks are execute as expected.


    The reason is - in HookManager checks the class name instead of checking namespace https://github.com/espocrm/espocrm/b...nager.php#L144
    This does not guarantee that all hooks will be executed. It would be better to check for a unique namespaces with class names.
    Last edited by tom; 02-06-2019, 02:41 PM.
  • yuri
    Member
    • Mar 2014
    • 8483

    #2
    Hi Tom,

    We have multiple hooks w/ the same name working. I don't have a chance to check it now. Looking through code, I don't see any issues.
    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

    • yuri
      Member
      • Mar 2014
      • 8483

      #3
      It's how it supposed to work. It's not a bug.
      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

      • tom
        Junior Member
        • Jan 2019
        • 6

        #4
        Hi Yuri,

        Look what debugger says (attached file).
        Method `hookExists()` returned `true` because it finded in other module - it's not correct, my hook is ignored.

        When it check hook from one module, it check its everywere - in other modules or in custom. And if it find in other place with the same className - its ignored.

        Comment

        • yuri
          Member
          • Mar 2014
          • 8483

          #5
          It's how it supposed to work.
          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

          • tom
            Junior Member
            • Jan 2019
            • 6

            #6
            So you say that hook classes should not be the same in different places?

            Comment

            • yuri
              Member
              • Mar 2014
              • 8483

              #7
              Yes. It's made to have an ability to override an existing one.
              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

              • tom
                Junior Member
                • Jan 2019
                • 6

                #8
                If a hook with this name has already been found, then it is ignored and does not fall into the array of all hooks https://github.com/espocrm/espocrm/b...nager.php#L171
                Accordingly, it will never be executed.

                Comment

                • tom
                  Junior Member
                  • Jan 2019
                  • 6

                  #9
                  Originally posted by yurikuzn
                  Yes. It's made to have an ability to override an existing one.
                  But if I do not want to override? I want just to add another one to the same Entity.

                  Different modules are written by different developers and no one knows the name of their classes.
                  If several modules have the same class names, none of them will work correctly.
                  Last edited by tom; 02-07-2019, 10:02 AM.

                  Comment

                  • yuri
                    Member
                    • Mar 2014
                    • 8483

                    #10
                    > But if I do not want to override? I want just to add another one to the same Entity.
                    I don't see any problems here.


                    Sorry, I'm busy. I don't have time to continue this discussion.
                    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

                    • yuri
                      Member
                      • Mar 2014
                      • 8483

                      #11
                      \Espo\Core\Hooks\Base has been deprecated for long. Not to be extended.
                      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

                      • axyl
                        Member
                        • Jun 2021
                        • 32

                        #12
                        Originally posted by tom

                        But if I do not want to override? I want just to add another one to the same Entity.

                        Different modules are written by different developers and no one knows the name of their classes.
                        If several modules have the same class names, none of them will work correctly.
                        When naming my hooks, I always use my own prefix as part of the name. You should do the same.

                        Comment

                        Working...