We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    Hi there, I'm trying to use the value of a migx tv with multiple textfields as input options for another tv, a checkbox tv with a list of categories. The idea behind this is that I want a site admin to easily manage the input options for this tv without accessing the tv itself.

    I did this as follows (probably not the best way, I'm not a programmer):

    1) created the admin-input tv named categoryInputOptions:
    input type: migx
    Form type:
    [{
      "caption":"Categories",
      "fields": [{
        "field":"category",
        "caption":"Category"
      }]
    }]

    Grid columns:
    [
    {"header": "Category", "width": "30", "sortable": "false", "dataIndex": "category"}
    ]


    2) Created the category tv named category:

    input type: checkbox
    input option values:
    @EVAL return $modx->runSnippet('readCategoryOptions');


    3) Created the snippet to display the category options from the first TV named readCategoryOptions:
    <?php
    $resource = $modx->getObject('modResource',1);
    
    $migxEntries = $resource->getTVValue('categoryInputOptions');
    $migxEntries2 = json_decode($migxEntries);


    This is where I get stuck. The TV categoryInputOptions is managed on the resource with id 1. So I first get that resource and then I get the value of the TV categoryInputOptions on that resource. I understand this is a JSON string, so I try to decode it. This gives me an array.

    My question is: how do I turn that array ($migxEntries2) into a string that can be used by TV category? I think it should be a string in the form value1||value2||value3.

    I tried everything I could find on arrays, like implode, but nothing seems to work. Probably something small.. Anyone got a hint for me?
      • 19328
      • 433 Posts
      For anyone interested, this is how I got it to work:

      <?php
      $resource = $modx->getObject('modResource',1);
      $migxEntries = $resource->getTVValue('tagOptions');
      $migxEntries2 = json_decode($migxEntries, true);
      $numItems = count($migxEntries2);
      $i = 0;
      foreach ($migxEntries2 as $key => $jsons) { // This will search in the 2 jsons
           foreach($jsons as $key => $value) {
              if($i+1 == $numItems) {
              	$output .= $value."==".$value; 
      	} else {
      	  $output .= $value."==".$value."||";
      	}
             
              $i++;              
          }
      }
      
      
      return $output;
        • 23018
        • 353 Posts
        Nice! I Added your solution to my gists collection for future use. Thank you.

        "Flexible" TVs are a great tool. I did this in the past with a "fake" resources.

        @SELECT pagetitle, id FROM [+add_prefix+]site_content WHERE parent=[+add_parentid] ORDER BY menuindex

        [+add_prefix+]. The modx table prefix (usually: modx_).
        [+add_parentid+]. The id of the parent resource. ITs children will be available options in the TV. The value will be equals to the id of the selected child resource.

        A more complex example can be found at the forum: http://forums.modx.com/index.php?topic=30102.0

        Thanks your your contribution!

        pepebe
          Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe