Skip to content

Instantly share code, notes, and snippets.

@goldsky

goldsky/getTVText.php

Last active Dec 11, 2017
Embed
What would you like to do?
Dynamically Render MODX's Template Variable on front-end to get Text and Value
<?php
/**
* getTVText snippet
*
* Dynamically Render Template Variable on front-end to get not only the value,
* but also its input's "text"
*
* @author goldsky <goldsky@virtudraft.com>
*
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
* @copyright 2016 goldsky <goldsky@virtudraft.com>
*
* 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