Skip to content

Instantly share code, notes, and snippets.

@Beagon
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save Beagon/0c6e28b61ba5305c1b85 to your computer and use it in GitHub Desktop.

Select an option

Save Beagon/0c6e28b61ba5305c1b85 to your computer and use it in GitHub Desktop.
[MODX] Converts an array of TV options to a JSON &where string, for use in snippets such as Rowboat or getResources.
<?php
/**
* TV2JSON
*
* Generates a JSON sting to be used in snippets like Rowboat and getResources
*
* Author: Robin Rijkeboer <Robin@qaraqter.nl>
*
* PROPERTIES:
*
* &templateVariable required
* &delimiter optional. Default: ,
*
* USAGE:
*
* [[!TV2JSON? &templateVariable=`[[*SuperAwesomeTemplateVariable]]`]]
*
*/
$TV = $modx->getOption('templateVariable', $scriptProperties);
$delimiter = $modx->getOption('delimiter', $scriptProperties, ",");
if (empty($TV))
{
return "";
}
$splitTV = explode($delimiter, $TV);
$count = 0;
$string = "";
foreach ($splitTV as $value) {
if($count == 0) {
$string .= '{"id":"' . $value;
$count++;
} else {
$string .= ',"OR:id":"' . $value . '"';
}
}
$string .= "}";
return $string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment