Access the settings of the field in the Admin panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dangerdodis
    Junior Member
    • May 2018
    • 26

    Access the settings of the field in the Admin panel

    Hi,
    I'm using a hook for phone control. But I want to check this control by looking at the checkbox in the phoneNumber field in the entity.
    Is it possible to access the field settings within the Hook parameters?

    for example;
    Code:
    Entity $entity, array $options = array()
    [I]$options->phoneNumber->checkbox == true ? like this[/I]
    I hope you understand me.
    Attached Files
  • dangerdodis
    Junior Member
    • May 2018
    • 26

    #2
    like this JS
    Think of it as php --> this.model.getFieldParam(this.name, 'max');

    Comment

    • tanya
      Senior Member
      • Jun 2014
      • 4308

      #3
      Hi,
      $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $fieldName, $parameterName])

      Comment

      • dangerdodis
        Junior Member
        • May 2018
        • 26

        #4
        Originally posted by tanya
        Hi,
        $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $fieldName, $parameterName])
        Thank you for your answer. If you have time, I have one more question.
        The $entityType variable does not exist in my parameters. I've defined a global hook. Can I access the Scope name from the $entity variable?

        Comment

        • tanya
          Senior Member
          • Jun 2014
          • 4308

          #5
          $this->getMetadata()->get('scopes');

          but don't forget to check if it is entity and not disabled
          open data/cache/application/metadata.php

          Comment

          • dangerdodis
            Junior Member
            • May 2018
            • 26

            #6
            I completed it like this. Is it correct ?
            Code:
            $entityType = $this->getMetadata()->get('scopes');
            Because I will use it like this.
            if($entityType == "Lead" || $entityType == "Account")

            Comment

            • tanya
              Senior Member
              • Jun 2014
              • 4308

              #7
              No, it will return the list of all scopes.
              If you need only these to, you can use a variable

              $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $fieldName, $parameterName])

              $this->getMetadata()->get(['entityDefs', 'Lead', 'fields', 'phoneNumber', 'required'])

              Comment

              • dangerdodis
                Junior Member
                • May 2018
                • 26

                #8
                Yes I know that. But if I use certain entity, I want to check. So I will check my entity. I hope I can tell.

                Is this the right way? example;

                Code:
                <?php
                
                namespace Espo\Custom\Hooks\Common;
                
                use Espo\ORM\Entity;
                use \Espo\Core\Exceptions\Error;
                
                class PhoneControl extends \Espo\Core\Hooks\Base
                {    
                    public function beforeSave(Entity $entity, array $options = array())
                    {
                        $entityType = $entity->getEntityType();
                        $fieldName = $entity->get('phoneNumber');
                
                        $requiredCheck = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $fieldName, 'required']);
                
                        if($entityType == "Lead" || $entityType == "Account" || $entityType == "User" || $entityType == "Contact") {
                            if($entity->get('phoneNumber')) {
                                if($requiredCheck === true) {
                                    $this->checkPhoneNumber($entity->get('phoneNumber'));
                                }
                            }
                        }
                    }
                }

                Comment

                • tanya
                  Senior Member
                  • Jun 2014
                  • 4308

                  #9

                  $fieldName = 'phoneNumber';
                  $requiredCheck= ... after if and... reading your code... if phoneNumber is not empty (is set) and field is required, then call checkPhoneNumber method? if the field is required, phoneNumber can't be empty, if the field is on the form

                  Comment

                Working...