Announcement

Collapse
No announcement yet.

Read current URL to set in a field

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

  • Read current URL to set in a field

    Hello, I use the QR-Code field to print it on labels. The QR-Code contains the URL of the current record (means it is a QR-Code of its own record). I use it to print it on labels, by which you may access the record, when scanning the Code.
    The URL I have to copy from the browser`s address field and out manually into the QR-Code field.

    Is there a way to automatically read the current URL and fill in the respected field?

  • #2
    I think you could create hook which will fill this field after you create record. In hook you have to get config of EspoCRM and copy domain. It's not didifficult.
    Hook: https://docs.espocrm.com/development/hooks/#hooks

    Here you have adjusted hook from our extension. You need to adjust this hook, but after change of namespace, name of hook etc it should work.
    https://dubas.pro/c/?0a305c4dd706da9...61rCa5vzoFJDLU

    Comment


    • #3
      Hi, I tried to create a hook, following the documentation and the script of emillod helped to understand hooks. It seems not very difficult, though I did not succeed with my code. This is what I wrote:

      Code:
      <?php
      
      namespace Espo\Custom\Hooks\MyNameSpace;
      
      use Espo\ORM\Entity;
      
      class qrCodeUrl extends \Espo\Core\Hooks\Base
      {
      public function beforeSave(Entity $entity, array $options = [])
      {
      if ($entity->isNew() && !$entity->get('qrCodeUrl')) {
      $entity->set('qrCodeUrl', 'https://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
      }
      }
      }
      It does not work, nothing happens.
      What is my intention:

      I want to output the current site URL of a record in a custom entity. The site URL should fill in the field qrCodeUrl.
      The entity is equal to myNameSpace.
      The field should be filled in either it is a new record or it is not present in a record yet.
      I set the entity Attribute of qrCodeUrl to concatenated URL from prepend protocol (https;//), domain and rest of the address after the domain.
      The script I saved at custom/Espo/Custom/Hooks/MyNameSpace/qrCodeUrl.php

      I rebuilt, cleared cache, but it does not do anything.

      Is this approach correct or do I miss something?

      Comment


      • #4
        shalmaxb have you tried afterSave instead beforeSave?
        Anyway i think you shouldn't use $_SERVER. My way is pretty simple.

        Comment


        • #5
          emillod , you mean this line of your code:

          Code:
          $domain = $config->get('siteUrl');
          In this part I did not understand $config. I think I would not need that. Right? But how would I set the ->get('siteUrL')?

          I have never done this before, so it takes time for me to understand.I will try afterSave.

          Thank you for your help.

          Comment


          • #6
            shalmaxb you need everything which i put there, because first you have to construct config to fetch all parameters from data/config.php file to $config, and if you'll check config.php, then you'll see that you have there siteUrl parameter where you have domain.
            And thanks to $config->get('siteUrl'); you can take address of you EspoCRM to variable $domain

            Comment


            • #7
              I tried everything, but did not get it to work. So I made another effort with formula in the following way:

              To create a complete link of the record I need:
              1. The base address of the record (which is equal for any record), without the record ID
              2. The record ID

              I created a varchar field recordID, which I populate by formula

              Code:
              recordID=entity\attribute('id');
              Then I created another varchar field to put together the whole link this way:

              Code:
              webAddress=string\concatenate('web-address',recordID);
              The output is the complete address like in the browser address bar.

              I see, that this is not the most elegant solution, but for whom does not succed with coding (even trying hours to solve), this might be an acceptable approach.

              Comment


              • #8
                It's great if it's working It's important that's working

                Comment


                • shalmaxb
                  shalmaxb commented
                  Editing a comment
                  nontheless, I learned to understand a lot mor because of your help. I only did not succeed in finding out, why it did not work. Maybe later I will understand enough to solve even this. We will see. Thank you again for your help.

              • #9
                Meanwhile I succeded in solving the mentioned question in a more elegant way by creating a custom function to read the current hostname and concatenate with URL.
                You can find the solution here: https://forum.espocrm.com/forum/gene...url-in-formula

                Comment

                Working...
                X