1
votes

Guess my problem is closely related to this one : Snippet duplicates content when used multiple times on page

The elements of my problem are the following ...

  1. $modx->loadedResources : an (empty) array registered in the main $modx object via a snippet on page load. The array holds resource id's of the resources fetched from the DB randomly, so the same resource isn't shown twice on the same page.

  2. loadRandomResource : a snippet using XPDO-style querying to load a random resource from the DB. It uses $modx->parseChunk() to fill the placeholders in the chunk with the resource data. With each call, it appends the id of the fetched resource being fetched to the $modx->loadResources array.

I used some debugging to check if the resource id's were properly being stored in my array, each time I fetch a new random resource, which happens to be the case. I then checked if the db returns different results, each time I call the loadRandomResource snippet, and it does. I can also confirm that it doesn't return duplicate results (I exclude the already loaded resource ID's in my XPDO query).

However, when calling the snippet at 3 various locations throughout my page template, all 3 snippet calls render the same resource, which is weird, since my debug shows that unique data is being loaded from the DB, and being sent to the chunk for rendering.

Please find below both the snippet code, as well as the chunk mark-up. Does anyone have any ideas? Any help is much appreciated!

loadRandomResource snippet

$criteria = $modx->newQuery('modResource');
$criteria->select(array('id','pagetitle'));
$criteria->sortby('RAND()');
$criteria->limit(1);
$whereOptions = array(
    'parent' => 2,
    'deleted' => false,
    'hidemenu' => false,
    'published' => true
);
if (!empty($modx->loadedResources)) {
    $whereOptions['id:NOT IN'] = $modx->loadedResources;
}
$criteria->where($whereOptions);
$resources = $modx->getCollection('modResource', $criteria);
$output = '';
foreach ($resources as $resource) {
    $fields = $resource->toArray();
    $fields['tv.tvPersonalPicture'] = $resource->getTVValue('tvPersonalPicture');
    $fields['tv.tvJobTitle'] = $resource->getTVValue('tvJobTitle');
    $output .= $modx->parseChunk('cnkTeamListItem', $fields);
    $modx->loadedResources[] = $fields['id'];
}
return $output;

cnkTeamListItem chunk

<div>
    <img src="[[+tv.tvPersonalPicture]]" alt="[[+pagetitle]]" />
    <h2>[[+pagetitle]]<br /><span>[[+tv.tvJobTitle]]</span></h2>
</div>
3

3 Answers

3
votes

I found the answer myself, solution is a bit odd though ...

I was calling my custom snippet 3 times in my template, uncached. Each call though exactly looked the same ...

[[!loadRandomResource? &type='teammember']]

Even though I had the exclamation mark in place, still ModX was caching the call, within the same page request.

So when I added a random unique value to each of the 3 calls, the issue was solved.

Call 1 : [[!loadRandomResource? &type='teammember' &unique='123465']]

Call 2 : [[!loadRandomResource? &type='teammember' &unique='987654']]

Call 1 : [[!loadRandomResource? &type='teammember' &unique='666666']]

Don't know if this is a bug or a feature, but I thought that the exclamation mark prevented caching, both across different pageviews, as well as within the same page view. Anyhow, thx for helping.

0
votes

I use this code for rendering chunks in snippents:

<?php

// get chunk or template
$tplRow = $modx->getOption('tplRow', $scriptProperties, '');

// get template
if (substr($tplRow, 0, 6) == "@CODE:") {
    $tplRow = substr($tplRow, 6);
} elseif ($chunk = $modx->getObject('modChunk', array('name' => $tplRow), true)) {
    $tplRow = $chunk->getContent();
} else {
    $tplRow = false;
}

// render template
$field = array(); // your fields
if ($tplRow) {
    $chunk = $modx->newObject('modChunk');
    $chunk->setCacheable(false);
    $chunk->setContent($tplRow);
    $output[]= $chunk->process($fields);
} else {
    $output[]= '<pre>' . print_r($fields, 1) . '</pre>';
}
0
votes

You do realize you could have done this with getResources, don't you?

http://rtfm.modx.com/display/ADDON/getResources

&sortby=`RAND()`&limit=`1`