I’ve gotten the snippet to parse everything and all looks good. But it’s not setting the placeholders. I know Im missing something simple, could anyone look at this and give me an idea?
My snippet call: [!sidebarParser? &chunks=`[*getSidebar*]` !] (sidebar value is 1,2,3)
In my page the placeholder would be:
[+sidebar.left+] or [+rnStrName+]
<?php
//File assets/snippets/testing/sidebarParser.inc.php
global $modx;
//You can use these values as example
$ar_sidebar = '1,2,3';
//$ar_sidebar = split(',',$chunks);
//$ar_sidebar = array_unique($ar_sidebar);
$ar_sb_exclude = split(',',$excludeDocs);
$ar_sb_exclude = array_unique($ar_sb_exclude);
setPlaceholders($ar_sidebar);
function setPlaceholders($doc_ids) {
global $modx;
$placeholders_array = getContentX($doc_ids);
foreach ($placeholders_array as $key => $value) {
//echo "<br/>Value: $value<br/><br/>";
$placeholder_name = 'sidebar.'.$key;
$modx->setPlaceholder($placeholder_name,$value);
echo "<br/>Key: $key<br/>";
echo "Placeholder Name: $placeholder_name<br/><br/>";
}
}
/* run mergeDocumentX, merge [content] with styling, and position. Create array for setPlaceholders */
function getContentX($array)
{
foreach ($array as $doc_id)
{
$my_wrapper = "\n<div class='{STYLING}'>\n\t<div><div><div>\n\t\t{CONTENT}\n\t</div></div></div>\n</div>\n";
$output = mergeDocumentContentX($doc_id);
$template = $output[template];
//These are two TVs, I've setup
//You can use
$sidebar_placement = 'left';
$sidebar_styling = 'left_side';
//$sidebar_placement = $output[my_doc][sidebar_placement];
//$sidebar_styling = $output[my_doc][sidebar_styling];
$my_wrapper = str_replace('{STYLING}', $sidebar_styling, $my_wrapper);
$template = str_replace('{CONTENT}', $template, $my_wrapper);
$my_placements[$sidebar_placement] .= $template;
//print_d($template);
}
return $my_placements;
}
/* Parse all TVs in the [content] of a document. $doc_id is the id of the document */
function mergeDocumentContentX($doc_id) {
global $modx;
$my_doc = $modx->getTemplateVarOutput('*',$doc_id);
$template = $my_doc['content'];
$replace= array ();
preg_match_all('~\[\*(.*?)\*\]~', $template, $matches);
$variableCount= count($matches[1]);
$basepath= $modx->config["base_path"] . "manager/includes";
for ($i= 0; $i < $variableCount; $i++) {
$key= $matches[1][$i];
$key= substr($key, 0, 1) == '#' ? substr($key, 1) : $key; // remove # for QuickEdit format
$value = $my_doc[$key];
if (is_array($value)) {
include_once $basepath . "/tmplvars.format.inc.php";
include_once $basepath . "/tmplvars.commands.inc.php";
$w= "100%";
$h= "300";
$value= $modx->getTVDisplayFormat($value[0], $value[1], $value[2], $value[3], $value[4]);
}
$replace[$i]= $value;
}
$template= str_replace($matches[0], $replace, $template);
//return $template;
$return_vals = array("my_doc" => $my_doc, "template" => $template);
return $return_vals;
}
function print_d($array){
echo "<pre>PRE<br/>\n";
print_r($array);
echo "</pre>\n";
}
?>
<?php
//Snippet
//tried this but its not setting either
$modx->setPlaceholder('rnStrName', 'T E S T I N G');
$snipPath = $modx->config['base_path'] . "assets/snippets/testing/";
include_once $snipPath."sidebarParser.inc.php";
?>
Edited: Commented out TV’s with static replacements to make debugging easier. Any help would be sooooo appreciated- rackin’ my brain!