Quote from: csauer at Mar 19, 2013, 08:14 AMQuote from: BobRay at Mar 18, 2013, 12:19 PMTry using getCollection() instead of getIterator(). I've had trouble with getIterator() and nesting, since getIterator() doesn't actually get the data until it's needed and if it's not needed in the snippet with the getIterator() call, it may not show up at all.
In general, though, I think I would try to redesign this so that you don't have to nest things so deeply. It appears that you have a resource field tag inside a link tag inside a chunk inside a snippet inside a chunk. I try not to ever get more than two, or occasionally three levels deep.
Thanks a lot Bob. Not nesting so deeply fixed the issue. In your opinion, what element is better suited to be exposed to the user? Should I put a snippet into the resource ( [[ArchivedNews]] )? Or should I put that behavior into a chunk ( [[$ArchivedNews]] )?
Scratch that. The problem has returned. I don't know what made it work for a few requests, but now I am back to the old buggy behavior again...
I removed all nesting completely. I have a snippet that fetches the objects and builds up all html itself; no chunk is ever called now. Yet this weird link behavior is still present.
I noticed that I am calling getCollection() on the $modx object. The documentation mentions that it is a member of $xpdo. Is this a problem?
Updated code:
<?php
//Search for modDocuments...
$c = $modx->newQuery('modDocument',null);
$c->where(array(
'published' => '1',
'isfolder' => '0',
'context_key' => 'news',
));
$c->sortby('publishedon', 'DESC');
//Limiting and offset parameters
$limit = $modx->getOption('archived_num_articles');
if($modx->resource->get('id') == $modx->getOption('site_start')) {
$offset = $modx->getOption('home_num_featured');
} else {
$offset = $modx->getOption('news_num_featured');
}
$c->limit($limit, $offset);
//Get docs
$docs = $modx->getCollection('modDocument', $c);
$output = '<div id="news">';
$output .= '<h4 class="title">News Resources</h4>';
$output .= $modx->runSnippet('Wayfinder', array(
"contexts" => "web",
"startId" => 47,
"outerTpl" => "NewsResourcesTpl",
));
$output .= '<ul class="dept-news-ul">';
foreach($docs as $doc) {
$properties = $doc->toArray();
//$output .= $modx->getChunk('DepartmentalNewsListItem', $properties);
$output .= '<li class="dept-news-li">';
$output .= '<a href="'.$modx->makeUrl($properties['id']).'">';
$output .= $properties['pagetitle'];
$output .= '</a></li>';
}
$output .= '</ul>';
$output .= '</div>';
return $output;
[---EDIT---]
Ok I've been playing with the code and think I have the problem pinpointed. It seems to be in this block:
//Search for modDocuments...
$c = $modx->newQuery('modDocument');
$c->where(array(
'published' => '1',
'isfolder' => '0',
//'context_key' => 'news',
));
$c->sortby('publishedon', 'DESC');
If I comment out 'context_key' => 'news' the snippet grabs links from the web context by default and all the links work perfectly. I am doing some context routing to make the news context act as a subdirectory (i.e. www.mysite.com/news). Here is the plugin that is doing the routing:
<?php
$pieces = explode('/', trim($_REQUEST[$modx->getOption('request_param_alias', null, 'q')], '/'), 2);
if (count($pieces) > 0) {
switch ($pieces[0]) {
case 'news':
if (isset($pieces[1])) {
$_REQUEST[$modx->getOption('request_param_alias', null, 'q')] = $pieces[1];
} else {
$_REQUEST[$modx->getOption('request_param_alias', null, 'q')] = '';
}
$modx->switchContext('news');
break;
default:
break;
}
}
Could there be any relation between the routing and the weird link problem occurring when I specify the context_key in the criteria?
[ed. note: csauer last edited this post 13 years, 7 months ago.]