We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5143
    • 116 Posts
    Hi,

    In Revolution, is it possible to have a regex output modifier, or something that's similarly flexible?

    The use-case I have in mind is when using a URL TV; obviously the href is generated, but there is a lack of customisation for the actual text to be displayed. It'd be nice to have an option to remove all mailto:, http://, https:// etc...

    The other thing I'm having trouble with is getting Modx to hide a URL TV if it's not been filled in. The issue seems to be that there's always a "--" present, even when the user doesn't select a protocol, or add an address. The code I believe ought to work is this:

    [[+artisturl:isnt=`--`:or:notempty=`<a href="[[+url]]" target="_blank" class="small artist-url">[[+artisturl:striphttp`]]</a>`]]


    ...but if left unfilled, it will always return "--".

    Any thoughts?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I have never used the optional output widgets that come with TVs. It's much better to use simple custom snippets to format the TV's content the way you want it.

      That said, a plain URL-type TV by itself just returns the URL from the text field if I don't select anything (or select the --), no -- added. What version of MODX are you using?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Ran some test, and this works just fine - although there's no need for a URL type TV, just a text field works just as well.
        [[*url:notempty=`<a href="http://[[*url]]">[[*url]]</a>`]]
        


        Of course, if you prefer, you can certainly write a custom snippet using a regular expression to use as an output modifier.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 5143
          • 116 Posts
          I have never used the optional output widgets that come with TVs. It's much better to use simple custom snippets to format the TV's content the way you want it.
          I think that's possibly part of the problem - there aren't any output widgets for the URL TV. Granted they may take a little more processing, but they'd be much quicker to set up.

          Anyway, I'm using version 2.2.14. The protocol select box can be left blank, but then the full URL wouldn't be available for the href attribute.

          Your second approach looks like a reasonable work-around, except it doesn't account for addresses that begin "https://" or anything else. Granted it solves part of the issue, but I can't help feel that the separation of the URL TV into two parts (1: the dropdown, 2: the address) could be put to better use.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Well, it's very odd, because I never get an output of --.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 5143
              • 116 Posts
              Yeah it's really bugging me - I'm sure it's not happened on any of my other Modx sites.

              I decided to go for the custom option, but I'm having a bit of trouble with it. I'm using PHPs parse_url function http://uk3.php.net/manual/en/function.parse-url.php to try and extract the "host" portion, but the input stays unaffected.

              The snippet:
              $url = $input;
              parse_url($url);
              $result = $url["host"];
              return $result;


              Called like so:
              [[+artisturl:urlCleaner`]]


              What am I doing wrong?
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                I think you need to have a variable to hold the return value of parse_url().
                $url = $input;
                $rs = parse_url($url);
                $result = $rs["host"];
                return $result;
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 5143
                  • 116 Posts
                  Thanks, but I'm getting the same result.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Ok, I created this snippet, named getHost:
                    <?php
                    $url = parse_url($input);
                    return $url['host'];

                    And I use it like this:
                    [[*url:getHost]]

                    And no matter what I do with the scheme drop-down, I just get the host.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 5143
                      • 116 Posts
                      That's very strange indeed; I'm using the exact same code, but it's still not behaving as it should.
                      I guess that means I've got some bugs to try and track down.

                      Edit: looks like I had a rogue back-tick in there. It's working just fine now. Thanks for the help.