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 may be wrong about that; it certainly wouldn't be the first time.
      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
      • 39932
      • 483 Posts
      So, if I want to add a TV that originates on Page A and also call it up across other pages, I would need to go through a snippet or plugin?

      Yes. That is correct. There are ways to "make" exceptions, but they aren't built in. Exceptions would be using $_SESSION or transferring to a System Setting, using AJAX/JS, etc.

      In the case of today's project, the TVs are originating from the home page template (adding the info through the home page backend) but also need them to appear in the other page.

      Just to make sure terminology and concept is correct, a TV's definition only is on a Template. The value is actually on the Resource (or page) itself.

      Can someone suggest how to make those TVs call up in the other page as well? Would think this is a basic thing but I've been pounding my head on this since yesterday.

      There are two ways to do this (without making the exceptions):

      The first is to have a Snippet call that gets the TV from the originating page. This would be done similarly to the following code.

          $resource = $modx->getObejct('modResource', X); // <-- Where X is the ID or variable containing the ID
          $value = $resource->getTVValue('nameOfTV');
          $modx->setPlaceholder('nameOfPlaceholder', $value);
      


      The second would be to have set a persistent value from the originating Resource. This will mostly likely be done with a Plugin that fires OnDocFormSave (I believe). You have many options from within the Plugin, but the most common will probably be:

          $value = $resource->getTVValue('nameOfTV');
          $toResource = $modx->getObject('modResource', X); // <-- Same as above
          $toResource->setTVValue('newNameOfTV', $value);
      



        Website: Extended Dialog Development Blog: on Extended Dialog
        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
        • 31902
        • 342 Posts
        Cool, I think this is what I need. Just some questions, if you don't mind.

        1. The part that says $value, is that literally what the word should remain or is that to be replaced by something in my code?
        2. Do I do a set of each of those for each TV I want with values I want to transfer over?
        3. Once the snippet is done, how do I go about calling it up on the page? Sorry, but I think I need some basic hand-holding here. Should I just put it somewhere above the code I outlined earlier?

        Man, just when I think I've got a good handle on MODx, something like this takes me down a couple of pegs to newbie again.
          • 4172
          • 5,888 Posts
          If you want to show one or some single TVs from another page you can use getResourceField - snippet

          If you want to show a set of TVs from another page you can also use getResources with the property

          &resources=`99` where 99 is the id of your other page
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 31902
            • 342 Posts
            Thanks, Bruno.

            Actually, I don't believe it, but I actually made it work, using @Fuzzical Logic's advice. Hopefully, this is the correct method. If not, it worked anyway. Please correct anything I got wrong. What I did was:

            1. Created a snippet called tv_placement. In it, I added this:

            <?php
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_1_intro');
            $modx->setPlaceholder('home_featured_event_1_intro', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_2_intro');
            $modx->setPlaceholder('home_featured_event_2_intro', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_date_1');
            $modx->setPlaceholder('home_featured_event_date_1', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_date_2');
            $modx->setPlaceholder('home_featured_event_date_2', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_image_1');
            $modx->setPlaceholder('home_featured_event_image_1', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_image_2');
            $modx->setPlaceholder('home_featured_event_image_2', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_link_1');
            $modx->setPlaceholder('home_featured_event_link_1', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_link_2');
            $modx->setPlaceholder('home_featured_event_link_2', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_title_1');
            $modx->setPlaceholder('home_featured_event_title_1', $value);
            
            $resource = $modx->getObject('modResource', 1); // <-- Where X is the ID or variable containing the ID
            $value = $resource->getTVValue('home_featured_event_title_2');
            $modx->setPlaceholder('home_featured_event_title_2', $value);


            The 1 is representing the ID of the home page, where the TVs originated from.

            2. In the spot where I wanted to call up the TV's (another resource), I added the snippet call

            [[tv_placement]]


            ...and below that, I created the rest of the code I needed, calling up each TV as a placeholder, for instance, in this format:

            [[+home_featured_event_1_intro]]


            Now, I assume that's the way to do it. In any case, it worked here and I'm happy. Is that the way it was supposed to be done?

            Thanks to all!
              • 4172
              • 5,888 Posts
              you can also try something like that:

              <?php
              
              $resource = $modx->getObject('modResource', 1);
              if ($collection = $resource->getTemplateVarCollection($resource))
              {
                  foreach ($collection as $object)
                  {
                      $modx->setPlaceholder($object->get('name'), $object->get('value'));    
                  }
              }
              
              
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 39932
                • 483 Posts
                @Willi:

                Kudos!!

                As a note: You don't need to get the resource multiple if you are setting multiple placeholders. You can get the resource once, then set all of your placeholders. If you need many fields from a resource, an implementation of Bruno's suggestion is optimal.

                Basically, you want the code to work this way:

                You get a Resource. ($modx->getObject() or getCollection or $modx->resource)

                Take ALL the information that you need from that Resource. (get->('fieldname') or getTVValue('tvname'))

                If you need another Resource, Start over with 1 again.

                Bruno's method is extremely similar. The real difference is his code works like the example above. Both are valid. Also, getResourceField is an alternative and works in much the same way, except that you don't have to code it. You just use a tag.

                (P.S. Simple Snippets like the one you wrote are a great way to get comfortable with other apsects of the MODx system.)
                  Website: Extended Dialog Development Blog: on Extended Dialog
                  Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                  Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                  Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                  • 3109 ☆ A M B ☆
                  • 894 Posts
                  All you want to do can be accomplished with getResources or getResourceField extras, creating a custom snippet and hard coding it for a specific page ID is a bad practice IMHO.

                  [[*tvName]]
                  


                  Will only be accessible if the TV exists on the resource it is called on, the scope of this is just for that resource.

                  [[+tv.tvName]]
                  


                  Is to be used with an extra like getResources be advised every extra works differently so make sure to check how they work so you know what prefix you need to use when trying to access TV from other resources.

                  Hope that helps clear it up a bit.
                    Benjamin Marte
                    Interactive Media Developer
                    Follow Me on Twitter | Visit my site | Learn MODX
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Sometimes if only one field of one resource is wanted, and you know exactly which field and which resource, a custom snippet can be lighter and quicker than rolling out the whole army of getField or getResources. It usually doesn't make that much difference, though, especially if the page and the getField or getResources snippet are all cached.
                      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
                      • 31902
                      • 342 Posts
                      Willi Waizenegger Reply #20, 14 years ago
                      Thank you to all. I love this community.

                      What blows my mind is that I've built many sites using MODx and this is the first time I've run into this problem. All this time I thought I could get away with just adding a TV here or there with just *name or something. I guess I got away with it up till now because, thinking back, I usually ran it through another snippet or something, for instance, getResources.

                      Up to now, I would add a TV with *name and, if it did not work, I would change it to tv.name. One or the other would usually work and I would move on. With this thread (and the problem I was having on this project and the fact that neither *name or tv.name worked this time) I thought I would finally find out why one or the other, and hopefully diagnose my problem. It worked.

                      @Bruno: I'll definitely look into your method. [ed. note: waizen last edited this post 14 years ago.]