Developing Hook managing mailadresses of contacts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scarecrow
    Junior Member
    • Sep 2019
    • 19

    Developing Hook managing mailadresses of contacts

    Hey there,

    First of all to emillod thank you for your tutorials. They are really great
    At the moment I'm working on my own BeforeSafeHook after sucessfull working my way throug the corresponding Hook tutorial.

    What I want do is managing Mailadresses and Phonenumbers of Contacts according to a specific set of rules.
    But when I run
    PHP Code:
    $entity->get('emailAddresses'); 
    
    it returns only the id's of the Mailadresses linked to the contact and no further details even though those details are listed in the Fields Part.

    Here the returned data converted into a string by converting it into an Array and using json_decode afterwards:
    PHP Code:
    [
    {
    "id":"XXXXXXXXXXXXXXXXX", // <- here is a valid emailadress-id linked to the contact that is saved. 
    "fields":
    {"id":
    {"dbType":"varchar",
    "len":24,
    "type":"id"},
    "name":
    {"type":"varchar",
    "fieldType":"varchar",
    "len":255},
    "deleted":
    {"type":"bool",
    "default":false},
    "lower":
    {"type":"varchar",
    "index":true,
    "fieldType":"varchar",
    "len":255},
    "invalid":
    {"type":"bool",
    "notNull":true,
    "fieldType":"bool",
    "default":false},
    "optOut":
    {"type":"bool",
    "notNull":true,
    "fieldType":"bool",
    "default":false},
    "primary":
    {"type":"bool",
    "notNull":true,
    "notStorable":true,
    "fieldType":"bool",
    "default":false}
    }
    }, ........ // <- Next id followed by the Fields list until all linked mailadresses are listed with the id. 
    
    is there a way to get the values of the other fields? At least i need the mailadress ("lower" or "name") and the bool for primary.

    Thank you for your help in Advance

    Best wishes

    Scarecrow



    Here the full code used for the Hook at the Moment:

    PHP Code:
    namespace Espo\Custom\Hooks\Contact;
    
    use Espo\ORM\Entity;
    use Espo\ORM\EntityManager;
    
    function object_to_array($data)
    {
        if (is_array($data) || is_object($data))
        {
            $result = [];
            foreach ($data as $key => $value)
        {
            if (is_array($value) || is_object($value))
        {
            $result[$key] = object_to_array($value);
        } else
        {
            $result[$key] = $value;
        }
        }
            return $result;
        }
        return $data;
    }
    
    
    class ManageMailAdress
    {
        protected $entityManager;
        public function __construct(EntityManager $entityManager)
        {
            $this->entityManager = $entityManager;
        }
    
        public function beforeSave(Entity $entity, array $options, array $data): void
        {
            $mail = $entity->get('emailAddresses');
            $mail = json_encode(object_to_array($mail));
            $entity->set('description', $mail);
        }
    } 
    
  • emillod
    Active Community Member
    • Apr 2017
    • 1405

    #2
    Scarecrow thanks for kind words!
    Did you thought about using emailAddressData param?

    Comment

    Working...