Announcement

Collapse
No announcement yet.

Not all hooks are executing

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

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

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

    Comment


    • #3
      It's how it supposed to work. It's not a bug.

      Comment


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


        • #5
          It's how it supposed to work.

          Comment


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

            Comment


            • #7
              Yes. It's made to have an ability to override an existing one.

              Comment


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


                • #9
                  Originally posted by yurikuzn View Post
                  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


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

                    Comment


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

                      Comment


                      • #12
                        Originally posted by tom View Post

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