Announcement

Collapse
No announcement yet.

Entity Hooks

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

  • Entity Hooks

    Hi,

    I have created a custom module and now I would like to apply one of the entity hooks to it. From what I can see there is the following hooks:
    • beforeSave
    • afterSave
    • beforeRemove
    • afterRemove

    Can you provide any instructions or advise for applying a basic "beforeSave" to a module?

    Any help is always appreciated.

    Thanks,
    cs1h

  • #2
    Hi

    Use this one as example
    application/Espo/Hooks/Note/Notifications.php

    Create cusom/Espo/Custom/Hooks/{ENTITY_NAME}/HookName.php

    PHP Code:

    namespace Espo\Custom\Hooks\{ENTITY_NAME};

    use 
    Espo\ORM\Entity;

    class 
    HookName extends \Espo\Core\Hooks\Base
    {    
        
        public function 
    afterSave(Entity $entity)
        {
            
    $meeting $this->getEntityManager()->getEntity('Meeting');
            
    $meeting->set('name''Hello ' $entity->get('name')) ;
            
    $this->getEntityManager()->saveEntity($meeting);
        }



    Clear Cache after that.

    Comment


    • #3
      Hi yurikuzn,

      Thank you for the quick response.

      I will give it a go.

      Thanks,
      cs1h

      Comment


      • #4
        Hi yurikuzn,

        Is it possible to update the record that you are currently saving (beforeSave)?

        For example if I wanted to add a time stamp to the description field.

        Thank you for your help so far,
        cs1h

        Comment


        • #5
          PHP Code:
              public function beforeSave(Entity $meeting)
              {
                  
          $description $meeting->get('description');
                  
          $dateStart $meeting->get('dateStart');
                  
          $dateTime = new DateTime($dateStart);
                  if (
          $dateTime) {
                      
          $description .= "\n" $dateTime->format('U');
                  }
                  
                  
          $meeting->set('description'$description);
              } 
          Note that all date/time in EspoCRM is in UTC. If you need Date String in user format with Time Zone you need to use 'dateTime' object (service) that is available in container. To be able to use dateTime in your Hook you need to define it in the dependency list. See Notifications hook as an example.

          Comment


          • #6
            Hi yurikuzn,

            Thank you for the quick response.

            Your example was a lot of help.

            Best Regards,
            cs1h

            Comment


            • #7
              I currently use SugarCRM for the past year and have built many modules for it. I am very excited to discover Espo as I feel it is very similar to Sugar but much cleaner with a better UI and UX and better wrote codebase. It just feels write and seeing that you have hooks like this among other features I feel I can re-create most of my modules that I built for my SugarCRM. Thanks also is there a doc section that shows stuff like this for developing modules and other stuff? I saw a blog post about working with entities which is half the battle and this post about hooks is another great step in the right direction

              Comment


              • #8
                Hello

                We will keep posting to the blog such manuals.

                The only hooks are beforeSave and afterSave. I think will be enhanced in future.

                Comment


                • #9
                  Hello,
                  I am trying to create hook beforeSave hook for Email entity.

                  My Aim is to add some text in email body before it is being sent.
                  Problem:
                  My problem right now is, hook is being called two times. Can you tell me what should I need to do ?

                  Jaydeep

                  Comment


                  • #10
                    Hi
                    try to check if entity is new
                    Code:
                        public function beforeSave(Entity $entity)
                        {
                            if ($entity->isNew()) {
                            ////
                            }
                       }

                    Comment

                    Working...
                    X