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

    I'm wondering if there is any quick and efficient way to check if a resource is set to a template that has access to a particular TV. This could be used in a Snippet to make sure to filter out resources that are part of templates that don't have access to a particular TV granted.

    In this coded example, the TV in question is 'createPDF' which is the TV used by two popular modx addons (for generating PDF's from resources). The idea is to check the resource in question to see if the template that is set for it, has or doesn't have this TV(createPDF) accessible to it.

    I've managed to pull this off with this code, however, I'm thinking there are probably more efficient ways of doing this, so please if you know of any share the wealth smiley

    $resource = $modx->getObject('modResource', $id);  
                $templateId = $resource->get('template');
                $c = $modx->newQuery('modTemplateVar');
                $c->innerJoin('modTemplateVarTemplate', 'TemplateVarTemplates');
                $c->where(array(
                    'TemplateVarTemplates.templateid' => $templateId
                ));
                $c->sortby('modTemplateVar.rank');
                $tvs   = $modx->getCollection('modTemplateVar', $c);
                $names = array();
                foreach ($tvs as $tv) {
                    $names[] = $tv->get('name');
                }
                $names = array_unique($names);
                
                if (!in_array("createPDF", $names)) {
    		return false; //exit plugin, resource template has no access to TV
                }
    


    Regards,
    T
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Why not just have a TV that is hidden set to some default value, then filter on that TV value? http://rtfm.modx.com/display/revolution20/TV+Visible
        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
        • 30491
        • 44 Posts
        Yeah, that's what these plugins were using but, that didn't work out properly with me, as I have a plugin that (HTMLtoPDF) that is triggered OnDocFormSave, and it basically creates a pdf for resources that have a 'createPDF' tv set to 1. I need most, but not all, my resources to have PDF's so, I set the default value of the 'createPDF' tv to 1. This seemed fine in theory, but in practice, it resulted in resources whose templates don't have 'createPDF' access triggering the HTMLtoPDF plugin.

        Which is why I thought of adding filtering by template, as opposed to TV--since for some reason, all resources were gaining access to 'createPDF' tv even though they may not explicitly have access to it.

        I am using version 2.2.4pl of modx -- I will try it on latest version as well, perhaps its some sort of bug, or my databases templates and tv's have errors. I'll have to check that out.

        That's the whole story behind why had to put together piece of code that checks if a resource's template has access to a particular tv.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          If the template doesn't have the TV, then any resources using that template won't have the TV value. Are you checking for the value of the TV, or for the value of the TV for the current resource?
            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
            • 30491
            • 44 Posts
            Hey sottwell,

            you are totally right, just double checked that, it was as you described--I guess I must have been confused as to how the TV works. I setup the template variable with a default value of 1 and the plugin would trigger for some reason even with resources that don't necessarily have access to that TV, probably it had something to do with the way I set up the tv values, now its working fine, lol. I still haven't determined exactly what caused the fluke.

            Thanks for the insight anyway
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Depending on how you were actually fetching the TV, you could end up with the default value of the TV itself, the processed value of the TV for a resource (i.e. if it's not set you'd get the default) or the raw value of the TV for the current resource. Sometimes the API functions can do more than you expect.

              For example, somebody wanted to get the 'content' - the actual resource ID - of a weblink. The usual API for getting content for a modResource object will process it, see that it's a modWebLink and helpfully processes the ID in the content, returning the URL. We had to use a bare-bones $modx->query to fetch the content of the weblink resource from the site_content table.
                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
                • 10208 ☆ A M B ☆
                • 1,780 Posts
                The only time I know of that a resource whose template is NOT attached to a TV will render the default of a TV, is when that TV is not attached to the template but has a default. In this case, any template with the call to said TV will process and display the default value.

                See: http://tracker.modx.com/issues/1852 (Ronin TV).

                  Frogabog- MODX Websites in Portland Oregon
                  "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
                  Having server issues? These guys have MODX Hosting perfected - SkyToaster
                  • 30491
                  • 44 Posts
                  @sottwell and @Frogabog

                  Thanks for the input, I finally got around to testing it in a clean, fresh install of modx 2.2.8 and the problem persists.
                  Basically, the TV is processed despite not being attached to the template (and hence the resource in question)!

                  You are free to recreate the issue, following these steps:

                  Create a TV
                  -name: createPDF
                  -type: checkbox
                  -default value: 1
                  -options: yes==1

                  Create a Template
                  -assign 'createPDF' template variable to it.

                  **So that 'createPDF' tv is assigned to one template and unassigned in the other**

                  Finally, in a resource that belongs to the unassigned template (that is, 'createPDF' is not assigned to it), call the TV in that resource (by inserting [[*createPDF]] in the content of that resource.

                  Result:

                  -the tv is parsed, despite belonging to a template that does not have access to it!

                  This has been frustrating, particularly when the rendering of certain chunks is conditional to the value (or even access) of a TV.


                  Anyone know of any workarounds?
                    • 30491
                    • 44 Posts
                    It's really confusing to be honest, it's counter-intuitive if you ask me. Modx documentation specifically says "TVs are Template-specific, meaning they can only be used in Templates that they are assigned to." Yet, if you give a TV a default value, then that TV is accessible by any template, you are able to parse it anywhere.

                    In my case its frustrating my effort to achieve something pretty straightforward. I have a tv called 'createPDF', its default value is 1, and what I am trying to do, is for templates that are not SUPPOSED to have access to that TV, I want it to simply not output anything.

                    For example:
                    
                    [[[[*createPDF:is=`1`:then=`$pdfButton`:else=``]]]]
                    
                    or
                    
                    [[[[*createPDF:notempty=`$pdfButton`]]]]
                    
                    


                    but, for the life of me, the default value is parsed even for templates that explicitly do not have access to that TV

                    :S

                    I can't think of a way around this hurdle.

                    Your input is greatly appreciated.
                      • 3109 ☆ A M B ☆
                      • 894 Posts
                      What is $pdfButton? if it's a chunk it should be encapsulated by double brackets, try this and post back what happens.

                      [[*createPDF:notempty=`[[$pdfButton]]`]]
                      
                        Benjamin Marte
                        Interactive Media Developer
                        Follow Me on Twitter | Visit my site | Learn MODX