We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44866
    • 50 Posts
    Hi Guys,
    I'm trying to make a simple change to my web page and my MODX skills aren't that great. I can normally "code by example" but this time I'm stuck. I'm using Modx 2.3.6

    I didn't write the page originally but I have the job of maintaining it.

    I'm adding a new button to a page but first I need to find all the values the button will need to pass on. What I am trying to return next to the text Left= is the domain part of the URL. i.e. "http://test.com.au" I think I have the rest figured out.

    Here is what I have so far

    <div class="book-inspection">
    	Full=[[~[[+id]]? &scheme=`full`]]
    	uniqueID=[[+id]]
    	Right=[[~[[+id]]]]
    	Left=[[~ ? &scheme=`full`]]
    	imgURL=[[+tv.PropertyImageThumb:phpthumbof=`w=280&h=202&zc=1`]]
    </div>


    When I run this code through my web server and view the source in my web browser this is what I see.

    <div class="book-inspection">
    	Full=http://test.com.au/index.php?id=579
    	uniqueID=579
    	Right=index.php?id=579
    	Left=
    	imgURL=/assets/components/phpthumbof/cache/00799_01.04caac2880e1d09a4d65e6c091e1543417.jpg
    </div>


    Ultimately I need to build a full path to the image file but that should be easy once I have all the pieces.


    I have tried all sorts of combinations of Left=[[~ ? &scheme=`full`]]
    I think it is going to be part of this string [[~[[+id]]? &scheme=`full`]] but I'm only guessing now.

    Any Ideas what I should try next?
    Thanks in advance.
    David


    UPDATE:
    ==========
    I found the Tag named [[++site_url]] that holds the value I want (almost)
    However it has a trailing slash. the template variable that contains the imgURL has a leading slash.
    Obviously I can't change either of them without breaking other things on my site.
    So I'm now looking for a way to remove one or the other of those slashes.

    This question has been answered by multiple community members. See the first response.

    [ed. note: pollardd last edited this post 10 years, 3 months ago.]
      • 3749
      • 24,544 Posts
      You're missing the ampersand before the w property.

      imgURL=[[+tv.PropertyImageThumb:phpthumbof=`&w=280&h=202&zc=1`]]


      It's a MODX convention that all path and URL System Settings have a trailing slash, so if you're going to change one, remove the leading slash on the TV. As long as you don't have hard-coded paths or URLs, changing it shouldn't break anything.

      FYI, you can shorten your code a little and make it more flexible if you use this instead of site_url:

      [[++assets_url]]
        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
        • 44866
        • 50 Posts
        Hi BobRay,
        Thanks for spotting that missing ampersand smiley
        I did read on your "Bob's Guide" about the trailing slash being the standard and make complete sense.
        My problem is I'm not sure where else that tv is used.
        Currently I'm still trying to find where it is assigned its value. I'm on a test site so I can change it and see what happens.

        I think the original writer has hacked this code from previous web pages so many times it has become difficult for me to understand. All these Chunks, Snipits, and TV might make the code reusable it doesn't make it readable.
        David

          • 3749
          • 24,544 Posts
          It's always tough trying to find your way around someone else's code.

          This is a utility snippet (untested) that should show you where that TV is used -- don't worry about the prefix, you don't need it here:

          Tag for the page:

          [[FindTVs]]



          FindTVs snippet content:

          $tvName='PropertyImageThumb';
          $output = '';
          
          /* Look in Resources */
          $docs = $modx->getCollection('modResource');
          
          foreach ($docs as $doc) {
              $content = $doc->getContent();
              if (strpos($content, $tvName) !== false) {
                  $output .= "<br>Resource: " . $doc->get('pagetitle'); 
              }
          }
          
          unset($docs);
          
          /* Look in Chunks */
          $chunks = $modx->getCollection('modChunk');
          
          foreach ($chunks as $chunk) {
              $content = $chunk->getContent();
              if (strpos($content, $tvName) !== false) {
                  $output .= "<br>Chunk: " . $chunk->get('name'); 
              }
          }
          
          return $output;
          


          If the site is very large, replace the word getCollection in both places with getIterator.
            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
            • 44866
            • 50 Posts
            Hi Bobray,
            That looks like a great utility. Just so I understand what I need to do, I create a snipit called FindTVs.
            Then I create a html page with not much more than [[FindTVs]] in it.
            When I load the page the code will execute and list all the places where "PropertyImageThumb" is used.

            Is this correct?

            I'm away from my test environment for about a week so I"ll give it a try when I return.
            I'm not sure what constitutes a very large site. My guess is it would be medium sized so I'll try getCollection then getIterator.

            Thanks again for your assistance.

            David
              • 3749
              • 24,544 Posts
              Is this correct?

              Yes it is, unless I made a mistake in the code. wink
                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
                • 44866
                • 50 Posts
                For anyone else who may be reading this thread looking for a similar answer this is how I got over the hump.

                Well I couldn't get a simple hello world example to work so I didn't have much chance of getting Bob's code to work.
                Unfortunately I don't have time to become a modx expert. Therefore I decided to move back towards the problem at hand and just make it work.

                I found this in the manual about creating an output filter.
                https://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers)/custom-output-filter-examples

                I then created a snipit that I called “substring” with the following code in it, from an example found part way down the above link.

                <?php
                $options=explode(',',$options);
                return count($options)>1 ? substr($input,$options[0],$options[1]) : substr($input,$options[0]);
                ?>
                


                Then where I was appending the two variables to togeather I just did it like this.
                imgURL=[[++site_url]][[+tv.PropertyImageThumb:phpthumbof=`&w=280&h=202&zc=1`:substring=`1`]]


                Note: substring could accept one or two variables but just specifying one variable removed that number of characters from the front which is what I wanted. smiley
                  • 3749
                  • 24,544 Posts
                  That looks perfect. smiley
                    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
                  • discuss.answer
                    • 44866
                    • 50 Posts
                    Very close but no cigar yet.
                    When I load my page in a browser my button isn't clickable. (I don't get the little hyperlink hand icon)

                    	<!--DJP 23Jun16  Added new Book Inspection Button v16-->
                    	<div class="book-inspection">
                    		<a target="_blank" href="https://book.inspectrealestate.com.au/Register?agentID=IRE-VJRAY&uniqueID=IRE-VJRAY-[[+id]]&imgURL=[[++site_url]][[+tv.PropertyImageThumb:phpthumbof=`&w=280&h=202&zc=1`:substring=`1`]]&Type=Rental"><a/>
                    	</div>
                    	<!--DJP End of new Inspection Button Update -->


                    After resorting to a html validator I eventually found the problem.

                    See that last closing tag??? <a/> should be </a>
                    Funny how your eyes (and brain) just relax and skip over the easy stuff.
                      • 44866
                      • 50 Posts
                      Even closer but still no cigar.

                      I have my code fully working on my test server but it produces an incorrect result on my production server. sad
                      I was stumped for a while but then I noticed I have friendly URL's turned on for my production site.
                      The [[+id]] returns an incorrect number in production and I can only think it is related to friendly URL's.

                      It isn't the ID of the current page that I need. It is the ID of the page that another button on the current page points to.

                      I found a discussion here https://forums.modx.com/forums/thread/35426/can-i-get-id-number-from-friendly-urls-in-modx where Mr Sotwell talks about doing a database lookup based on the page alias. Not sure if this is the correct approach. It seems I'm making it harder than it needs to be. And then i'd have to find the page's alias anyway.

                      Any suggestions?

                      David