We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36788
    • 75 Posts
    There is a wrong behaviour of @eval bindings in TVs (vs Evo). TVs values are recalculated depending on the document from which they are called.

    E.g. lets suppose that you have the following site structure:

    mainPage(id=1)
    catalogue (id=2)
    -doc1 (id=3)
    -doc2 (id=4)
    -doc3 (id=5)


    And for each doc in catalogue container you set the following simple code:
    @EVAL $id = $modx->resource->get('id'); return $id;
    as a default TV value for myTV
    You will expect than that it will put the id of the current resource in each document. E.g. for the document with id=5 it should be 5. But it won't.

    If you then will call this TV in the document with id=1 (mainPage in my example) by using getRecources call [[getResources? &includeTVs=`1`& parent=`2` &includeTVlist=`myTV` &tpl=`myTpl`]], myTpl = [[+tv.myTV]] it will return 111 (id will be changed to the id of the document from which it was called).

    And even if you put the code above in a snippet mySnippet

    <?php $id = $modx->resource->get('id'); return $id;


    and put it in default TV value as [[mySnippet]] and then call the getRecources as described above, you'll get the same output - 111 (id will be changed to the id of the document from which it was called) or you will get an error.


    corrected misprint: myTpl = [[+myTV]] to myTpl = [[+tv.myTV]] [ed. note: sladdals last edited this post 13 years, 5 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Try adding a property like &id=`[[+id]]` to the TV in your getResources tpl. I'm not sure if that's the correct property name to indicate which resource you want the TV value for, but something like that should work.
        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
        • 36788
        • 75 Posts
        Quote from: sottwell at Apr 30, 2013, 03:05 AM
        Try adding a property like &id=`[[+id]]` to the TV in your getResources tpl. I'm not sure if that's the correct property name to indicate which resource you want the TV value for, but something like that should work.

        Sorry, can't get it work))

        There is only a property &tvPrefix=``, but it does not work with [[+id]].
          • 36788
          • 75 Posts
          And anyway if you put in default values some code which is not id dependant e.g. return @EVAL $output ='/assets/image.jpg'; return $output;
          This will work without changes in getResource call so I don't think that the problem is with getResource call.
            • 36788
            • 75 Posts
            Can someone please do a simple test, so help me to understand that this is not my mistake?

            This is really simple test:)
            1) Create snippet [[Test]]

            <?php $id = $modx->resource->get('id'); return $id;
            


            2) Put the call [[Test]] in the default value field of TV lets say testTV (please be sure that TV is used for at least 1 document smiley)


            3) Call the getResources [[getResources? &includeTVs=`1`&includeTVlist=`testTV` &tpl=`myTpl`]], create chank myTpl = [[+tv.testTV]]
            (this should be differ page from where you used testTV)

              • 3749
              • 24,544 Posts
              That EVAL'd code will always return the ID of the resource that the user is currently visiting. That's what $modx->resource is. getResources and other snippets like it generally pay no attention to $modx->resource.

              If I'm understanding you, you should be able to forget all about the TV and just put [[+id]] in your getResources Tpl chunk. That will be replaced with the ID of the resource that's currently being processed rather than the one with the getResources tag.
                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
                • 36788
                • 75 Posts
                That EVAL'd code will always return the ID of the resource that the user is currently visiting. That's what $modx->resource is.
                Sorry for my English and thank you for your help, BobRay! smiley
                This mean that default value of TV parameter do not store the result of snippet calculation as it was in Evo.

                It stores the code of a snippet and every time you call the TV this code will be recalculated giving different results.

                The TV default value is not that valuable anymore as it was in Evo. As I could just make a check whether TV is empty and make snippet calculation right inside getResources call.

                And as for the general question actually I need to solve a bit more complicated task. Here is my site structure:

                mainPage(id=1)

                category_level_1 (myTV= ‘empty’)
                -category_level_2
                -doc1 (myTV=’value1’ )

                category2_level_1 (myTV= ‘empty’)
                -category2_level_2
                -doc21 (myTV=’value2’)

                I need to set the value for each level 1 category. This value should be taken from the TV value of the document within this category.

                For example for category_level_1 I need to get the value of myTV from doc1 (’value1’) and for category2_level_1 from doc21 (’value2’). And I want to output the values of categories myTVs (along with other TVs and categories fields) on the mainPage of the site by using getResources.

                That was an easy task in Evo. I put a simple snippet call in default TV value field for categories. It used category id as a referrer to travel down the tree to the descending document. In Evo all calculations were done right in the default TV value field and modx stored the result of calculation. So when I used ditto to output the TVs the values were not recalculated.

                This method became impossible in Revo as the value is stored as a code. So when I call getResources on main page the code which I put in default value field is recalculating and the referring id is not already the category id (but id of the main page).
                Hope this was not too complicated smiley as my English is very poor

                I've attached the picture of what I need to achieve as output on main page [ed. note: sladdals last edited this post 13 years, 4 months ago.]
                  • 3749
                  • 24,544 Posts
                  I think you might be able to do that with a MIGX TV, but I'm not sure.
                    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
                    • 36788
                    • 75 Posts
                    oo12 - developer of the setPlaceholders, helped me to solve the general task.

                    I used the snippet setPlaceholders call - [[setPlaceholders? &id=[[+id]]&ph=childR.childR.tv.image!!childR.tv.image &output=1]] (https://github.com/oo12/setPlaceholders) within wayfinder call to solve the task.

                    This does not solve the core problem with TV default value, though.
                    There should be any replacement of $modx->resource->get('id'). The link to id of the document where TV is set non dependable from where TV is called. Analogue of $docid in EVO, which could be used inside TV default value and returned id of the page containing TV no matter on which other page of your site you call this TV.

                      • 24732 ☆ A M B ☆
                      • 300 Posts
                      After four hours of fighting this exact issue, I'm both relieved and really unhappy.

                      I can't eval the current id of a resource, set it into a tv and expect it to stay that value. It will reset.

                      banging my head against a wall.
                        ________

                        Anne
                        Toad-in-Chief
                        Red Toad Media - Web Design, Louisville KY
                        Hear me tweet: http://www.twitter.com/redtoadmedia
                        "Bring on the imperialistic condiments." - Rory Gilmore