We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    I'm trying to create a custom output modifier to automatically convert incoming GET values to int. I had presumed it would be quite simple:
    <?php
    return intval($input);

    However, this (and other variations on the theme) doesn't appear to have any impact whatsoever on the output, which in the case of a string like pagetitle should be returning 0.
    [[*pagetitle:castInt]]

    This question has been answered by Bruno17. See the first response.

      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
    • discuss.answer
      • 4172
      • 5,888 Posts
      seems you will need to return a string since 2.3

      return (string) intval($input);
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Interesting. Is this documented anywhere officially?
          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
          • 22427
          • 793 Posts
          Fiddling around a bit I found that with
          return $input . ' ' . intval($input);

          the second part would be output correctly.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Yes, since you are appending a string to the value PHP will automatically convert it to a string. Likewise something like '42' + 0 will automatically convert the string 42 to an integer so the addition operation can be performed.

            However, in your example the space at the beginning of the numeric string may or may not be acceptable to the function using the resulting string. Better to specifically cast it to a string.
              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
              • 3749
              • 24,544 Posts
              FYI, this would be faster (if it works -- not sure if two casts in a row will work). IIRC, casts are about twice as fast as a conversion function.

              return (string) (int) $input;
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting