Last active
June 23, 2023 12:53
-
-
Save goldsky/4d7ef01232beca69aec1 to your computer and use it in GitHub Desktop.
Dynamically Render MODX's Template Variable on front-end to get Text and Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* getTVText snippet | |
* | |
* Dynamically Render Template Variable on front-end to get not only the value, | |
* but also its input's "text" | |
* | |
* @author goldsky <[email protected]> | |
* | |
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | |
* @copyright 2016 goldsky <[email protected]> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
* | |
* @example snippet's call: | |
* | |
* [[!getTVText? | |
* &resId=`[[*id]]` | |
* &tvName=`tags` | |
* &tpl=`tag` | |
* &tvDelimiter=`||` | |
* &outputDelimiter=`<br>` | |
* ]] | |
* | |
* @example tpl chunk: | |
* <a href="[[~[[+parent]]? &tag=`[[+value]]`]]">[[+text]]</a> | |
* | |
* @return placeholders: | |
* - text | |
* - value | |
* - id // resource's ID | |
* - parent // parent resource's ID | |
*/ | |
/** | |
* @var &resId | |
* @type comma seperated integers | |
* @description resource's ID | |
* @default current resource's ID | |
*/ | |
$resId = $modx->getOption('resId', $scriptProperties, $modx->resource->get('id')); | |
/** | |
* @var &parents | |
* @type comma seperated integers | |
* @description parents' IDs | |
* @default null | |
*/ | |
$parents = $modx->getOption('parents', $scriptProperties); | |
/** | |
* @var &tvName | |
* @type string/int | |
* @description tv's Name or ID | |
* @default null | |
*/ | |
$tvName = $modx->getOption('tvName', $scriptProperties); | |
/** | |
* @var &tpl | |
* @type string | |
* @description chunk's Name to render the output. If not provided, raw values will return. | |
* @default null | |
*/ | |
$tpl = $modx->getOption('tpl', $scriptProperties); | |
/** | |
* @var &tvDelimiter | |
* @type string | |
* @description delimiter of multiple values | |
* @default || | |
*/ | |
$tvDelimiter = $modx->getOption('tvDelimiter', $scriptProperties, '||'); | |
/** | |
* @var &outputDelimiter | |
* @type string | |
* @description delimiter of multiple outputs | |
* @default \n | |
*/ | |
$outputDelimiter = $modx->getOption('outputDelimiter', $scriptProperties, "\n"); | |
/** | |
* @var &toPlaceholder | |
* @type string | |
* @description returns the value to the given placeholder instead | |
* @default null | |
*/ | |
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties); | |
if ((empty($resId) && empty($parents)) || empty($tvName)) { | |
return; | |
} | |
$resources = array(); | |
if ($resId) { | |
$resId = @explode(',', $resId); | |
$resources = $modx->getCollection('modResource', array( | |
'id:IN' => $resId | |
)); | |
} | |
if ($parents) { | |
$parents = @explode(',', $parents); | |
$parentResources = $modx->getCollection('modResource', array( | |
'id:IN' => $parents | |
)); | |
foreach ($parentResources as $parentResource) { | |
$children = $parentResource->getMany('Children'); | |
if ($children) { | |
foreach ($children as $child) { | |
$resources[] = $child; | |
} | |
} | |
} | |
} | |
if (empty($resources)) { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[getTVText] : $resources is empty'); | |
return; | |
} | |
$tv = $modx->getObject('modTemplateVar', array( | |
'name' => $tvName | |
)); | |
if (empty($tv)) { | |
return; | |
} | |
$outputArray = array(); | |
foreach ($resources as $resource) { | |
$tvValue = $resource->getTVValue($tvName); | |
$inputOptions = $tv->parseInputOptions($tv->processBindings($tv->get('elements'), $resource->get('id'))); | |
$tvOptions = array(); | |
foreach ($inputOptions as $v) { | |
$exp = @explode('==', $v); | |
$text = isset($exp[1]) ? $exp[0] : $exp[0]; | |
$val = isset($exp[1]) ? $exp[1] : $exp[0]; | |
$tvOptions[$val] = $text; | |
} | |
$values = array_map('trim', @explode($tvDelimiter, $tvValue)); | |
foreach ($values as $value) { | |
if (empty($value)) { | |
continue; | |
} | |
if (isset($outputArray[$value])) { | |
continue; | |
} | |
if (!empty($tpl)) { | |
$outputArray[$value] = $modx->getChunk($tpl, array( | |
'text' => (isset($tvOptions[$value]) ? $tvOptions[$value] : $value), | |
'value' => $value, | |
'id' => $resId, // resource's ID | |
'parent' => $resource->get('parent') // resource's ID | |
)); | |
} else { | |
$outputArray[$value] = $value; | |
} | |
} | |
} | |
$output = @implode($outputDelimiter, $outputArray); | |
if (!empty($toPlaceholder)) { | |
$modx->setPlaceholder($toPlaceholder, $output); | |
return; | |
} | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment