Announcement

Collapse
No announcement yet.

Read current URL in formula

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

  • Read current URL in formula

    Hello,
    is it possible to read the current url to set into field by formula?
    I tried this, but it does not work.

    Code:
    url='http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    Any idea?

  • #2
    Hello, I think, it's not supported.
    Last edited by Maximus; 04-09-2021, 12:38 PM.

    Comment


    • #3
      Hello,
      strange for my understand :
      url are 99% so sample :



      $siteUrl = rtrim($this->getConfig()->get('siteUrl'), '/');
      $this->getSiteUrl() . '/#' . $entityType . '/view/' . $entityId;

      You have
      siteUrl : it's unique, you can write this by hand
      entityType : maybe by formula (not checked)
      view : hard coded
      entityId : in formula

      so just concat string.

      Comment


      • #4
        Hi, yes, this is exactly what I did so far, but with putting the URL itself into the formula means, that in every instance I install my app, I have to exchange the URL to the respective URL of that installation.
        For that I would like to read it from the installation itself. I know, kind of a "luxury problem", but would be nice to have this.

        Comment


        • #5
          Hello,

          just need a custom formula who return $this->getConfig()->get('siteUrl')
          i have checked config.php but nothing else data is relevant.

          https://docs.espocrm.com/development...on-in-formula/



          Not tested but something so :

          GetConfig('siteUrl')

          PHP Code:
          <?php

          namespace Espo\Custom\Core\Formula\Functions\StringGroup;

          use 
          Espo\Core\Exceptions\Error;

          class 
          GetConfigType extends \Espo\Core\Formula\Functions\Base
          {
          public function 
          process(\StdClass $item)
          {
          $args $this->fetchArguments($item);

          $value $args[0];

          return 
          $this->getConfig()->get($value);
          }
          }
          ?>

          Comment


          • #6
            item
            Have mercy with me , I am no programmer. Where would I put this to try? In the config.php? And do I understand right, that so it would extend the abilities of the formula to make it possible to have a formula like this:


            Code:
            url='http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];

            Comment


            • #7
              You can use this custom function.


              Create custom\Espo\Custom\Resources\metadata\app\formula. json file

              Code:
              {
              "functionList": [
              "__APPEND__",
              {
              "name": "string\\getSiteUrl",
              "insertText": "string\\getSiteUrl()"
              }
              ],
              "functionClassNameMap": {
              "string\\getSiteUrl": "Espo\\Custom\\Core\\Formula\\Functions\\StringGroup\\GetSiteUrl"
              }
              }

              Create custom\Espo\Custom\Core\Formula\Functions\StringGr oup\GetSiteUrl.php

              PHP Code:
              <?php

              namespace Espo\Custom\Core\Formula\Functions\StringGroup;

              use 
              Espo\Core\Utils\{
              Config,
              Config\ConfigFileManager
              };

              class 
              GetSiteUrl extends \Espo\Core\Formula\Functions\Base {

              public function 
              process(\StdClass $args null) {
              $fileManager = new ConfigFileManager();
              $config = new Config($fileManager);
              return 
              $config->getSiteUrl();
              }

              }


              And you can see and use in formula:
              Click image for larger version

Name:	getSiteUrl.jpg
Views:	628
Size:	21.6 KB
ID:	74847

              Comment


              • #8
                Thank you criffoh , I gonna test it in the next days.

                Comment


                • #9
                  It's not possible to know the current browser URL by design as EspoCRM is a single page application.

                  Comment


                  • #10
                    So that mean the code above won't work? All it do it get the siteURL only, getting /#entity/view/ is wishful thinking?

                    Comment


                    • #11
                      Today I succeeded in implementing the custom function from criffoh and it works in the following way:

                      1. Create a custom function (here is one small modification from the code example of criffoh). The function will be named string\getSiteUrl:

                      Create custom\Espo\Custom\Resources\metadata\app\formula. json


                      Code:
                      { "functionList": [ "__APPEND__", { "name": "string\\getSiteUrl", "insertText": "string\\getSiteUrl()" } ], "functionClassNameMap": { "string\\getSiteUrl": "Espo\\Custom\\Core\\Formula\\Functions\\StringGro up\\GetSiteUrl" } }
                      Create custom\Espo\Custom\Core\Formula\Functions\Base\StringGroup\GetSiteUrl.php (the modification is in the path, green folder name, add it also to the namespace!)

                      Code:
                      <?php
                      
                      namespace Espo\Custom\Core\Formula\Functions\Base\StringGroup;
                      
                      use Espo\Core\Utils\{
                      Config,
                      Config\ConfigFileManager
                      };
                      
                      class GetSiteUrl extends \Espo\Core\Formula\Functions\Base {
                      
                      public function process(\StdClass $args = null) {
                      $fileManager = new ConfigFileManager();
                      $config = new Config($fileManager);
                      return $config->getSiteUrl();
                      }
                      
                      }

                      Clear cache (probably also Browser cache). This will read https://yourHostname.domain and the function string\getSiteUrl() will appear in the function list of the formula.



                      To get the entire URL you will have to concatenate the necessary parts of the URL by formula and output in another field, like so (an example to get a complete link to an image embedded in your entity):

                      Field 1: current URL (Text), where you fetch the current hostname:

                      Code:
                      currentURL=string\getSiteUrl();
                      Field 2: concatenate currentURL with image link:

                      Code:
                      imageAddress=string\concatenate(
                      currentURL, '/','?entryPoint=DubasPublicAttachment&id='
                      );
                      What happens here:

                      -The formula takes the hostname from the previous created field, a slash is added and after that the entry point part. This part I get from the image link (copy any image link one time, to see, how it is formed). In my case I use for downloading images the free extension from emillod called DubasPublic Attachment, which allows downloads from outside of espoCRM (you can find it here: https://github.com/dubas-pro/ext-public-attachment). Mind the security isues in case of production sites.

                      Field 3: complete URL in an URL field. Concatenate the above string with the file ID (or image ID) of the current record:

                      Code:
                      imageDownloadLink=string\concatenate(imageAddress,imageId);
                      Hope it helps.




                      Comment


                      • esforim
                        esforim commented
                        Editing a comment
                        Wow, you getting good shalmax
                    Working...
                    X