We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    I'm trying to create a listbox tv which only shows resources that have a certain tv value.
    My setup is:

    On my categorypage I have a listbox tv called 'highlight'. It has to show all resources which have the current category id set in a tv called 'category'. This way the user can select a highlighted article, but it has to be an article that belongs to that category.

    The tv 'category' is also a listbox tv, it contains the id's of the different categorypages. A resource can have multiple categories set. They are saved like 9||18||23 for example (this resource would belong to 3 categories).

    In the listbox TV for input option values I have:

    @EVAL return $modx->runSnippet('getResources',array('parents'=>6,'context'=>'web','tvFilters'=>'category==%|$modx->resource->get('id')|%||category==$modx->resource->get('id')|%||category==%|$modx->resource->get('id')||category==$modx->resource->get('id')','showHidden'=>1,'includeTVs'=>1,'sortby'=>'{"publishedon":"DESC"}','tpl'=>'manager.getresources.input.rowtpl','tplFirst'=>'manager.getresources.input.rowtplFirst'));


    the tpl's are:
    manager.getresources.input.rowtpl

    ||[[+pagetitle]]==[[+id]]​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


    manager.getresources.input.rowtplFirst

    [[+pagetitle]]==[[+id]]​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​



    However with this code the listbox for the tv 'highlight' shows no values at all.

    What am I doing wrong? Can I access the id of the current resource with $modx->resource->get('id') or would this not work because we're in the manager context? If so, what should I use to get the current resource id in the tvFilter param? I tried a hundred different things and I'm running out of ideas...

    Thanks for any help!
      • 3749
      • 24,544 Posts
      I'm not sure what you're trying to do is possible, given the parsing order and the many pieces of information needed for the operation (and the fact that you're in the Manager).

      I haven't really thought it through, but offhand it seems like a good use case for extending modResource with ClassExtender. The default install already has the concept of categories built in (as an example) and adds them to the Create/Edit User form with a plugin. The possible categories come from a chunk, but they could easily be computed by the plugin based on other custom fields.

      It would all be a lot faster once you got it working and I think you'd have more control over the UI.

      Having the data in a custom table rather than TVs would also make searching and sorting *way* faster.

        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
        • 40045
        • 534 Posts
        I hope I understand your question / scenario right, but this could work:

        create a listbox TV (in your case the one that should show the resources) and put in:

        @EVAL return $modx->runSnippet('setTaggedResourcesList', array('valuetv'=>23, 'searchtv' => 22));
        


        then create a snippet with the following code and call it the same name you set above in the @EVAL statement (e.g. "setTaggedResourcesList")

        // this is the array of input values that we will return at the end (imploded)
        $inputvalues = array(
        	'(Please choose)==',
        );
        
        // now we get all TemplateVariableResource Objects that exist for the categories TV
        // (the one where multiple categories can be specified, e.g. result in something like 9||18||23)
        // this is the TV ID you have to specify in the @EVAL snippet call for the property "searchtv"
        $tvrobjs = $modx->getCollection('modTemplateVarResource', array('tmplvarid' => $searchtv));
        
        
        // now we loop over all these Objects and filter out the ones we want
        foreach ($tvrobjs as $tvrobj) {
        	// create an array from a TV Value like 9||18||23 for easier searching
        	$tags = explode('||', $tvrobj->get('value'));
        	// if the value of the category tv (which ID you have to specify for the property "valuetv" in the @EVAL call
        	// is found in a template variable resource object then create a new item for the input values with pagetitle of that
        	// resource and it's id as value
        	if (in_array($modx->resource->getTVValue($valuetv), $tags)) {
        		// fetch the pagetitle for a matching resource
        		$resobj = $modx->getObject('modResource', array('id' => $tvrobj->get('contentid')));
        		// build the input value item and add it to the $inputvalues array
        		$inputvalues[] = $resobj->get('pagetitle') . '==' . $resobj->get('id');
        	}
        }
        
        return implode('||', $inputvalues); // finally implode the whole array to a correct input value options string for the listbox
        


        this is clearly not as elegant as Bobs classExtender, but that's the nice thing about MODX, there are always many ways to accomplish something =)!

        this then results in something like what you can see below (not that you have to reload the resource edit form after changing the "valuetv", e.g. switch from Category1 to Category2 (you'll see what I mean when It gets white =D)

        In this example there are 3 resources, "Published Child 1 (categories 1,2)", "Published Child 2 (categories 1,3)", "Published Child 3 (categories 1,4)" so when the current resource has selected Category 1, all three resources will be available in the listbox, if you then switch to Category 2, only a single resource is shown as no other has assigned category 2...



        PS: sry for the 2 attachments, still can't remove attachments...forum bug... [ed. note: exside last edited this post 12 years, 4 months ago.]
          • 4172
          • 5,888 Posts
          this should work:

          @EVAL $id = $modx->resource->get('id'); return 'select one==||' . $modx->runSnippet('migxLoopCollection',array('classname'=>'modTemplateVarResource','joins'=>'[{"alias":"Resource"}]','tpl'=>'@CODE:[[+Resource_pagetitle]]==[[+Resource_id]]','outputSeparator'=>'||','where'=>'{"tmplvarid":"93","1":[{"value":"'.$id.'"},{"OR:value:LIKE":"%||'.$id.'||%"},{"OR:value:LIKE":"'.$id.'||%"},{"OR:value:LIKE":"%||'.$id.'"}]}'));
          


          see also this post:
          https://forums.modx.com/thread/82476/why-tagging-doesn-039-t-work-in-revo-and-how-to-solve-this?page=2#dis-post-494448

          don't forget to change the templateVar - ID here:
          "tmplvarid":"93"
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 19328
            • 433 Posts
            Wow, you guys are great! Thanks so much for all the help!

            The first thing I tried was Bruno's suggestion (because it looked so easy ;-). I saw in the thread you mentioned that you helped me before with this question ;-). migxLoopCollection works great, but it also shows unpublished and deleted resources. What should I change in the code below to make this work? (code below doesn't work, it shows all resources now)

            @EVAL $id = $modx->resource->get('id'); return 'kies een artikel==||' . $modx->runSnippet('migxLoopCollection',array('classname'=>'modTemplateVarResource','joins'=>'[{"alias":"Resource"}]','tpl'=>'@CODE:[[+Resource_pagetitle]]==[[+Resource_id]]','outputSeparator'=>'||','where'=>'{{"Resource.published":"1"},{"Resource.deleted":"0"},{"tmplvarid":"12","1":[{"value":"'.$id.'"},{"OR:value:LIKE":"%||'.$id.'||%"},{"OR:value:LIKE":"'.$id.'||%"},{"OR:value:LIKE":"%||'.$id.'"}]}}'));
              • 4172
              • 5,888 Posts
              try this:

              @EVAL $id = $modx->resource->get('id'); return 'select one==||' . $modx->runSnippet('migxLoopCollection',array('classname'=>'modTemplateVarResource','joins'=>'[{"alias":"Resource"}]','tpl'=>'@CODE:[[+Resource_pagetitle]]==[[+Resource_id]]','outputSeparator'=>'||','where'=>'{"Resource.published":"1","Resource.deleted":"0","tmplvarid":"93","1":[{"value":"'.$id.'"},{"OR:value:LIKE":"%||'.$id.'||%"},{"OR:value:LIKE":"'.$id.'||%"},{"OR:value:LIKE":"%||'.$id.'"}]}'));
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 19328
                • 433 Posts
                Thanks Bruno, that was it! I guess I had a little too much curly brackets going on there ;-)