I’m trying to get an image sitemap setup and using SeoSuite as a basis. Normally I’d use SeoSuite but there is currently a bug in their snippet when dealing with MIGX.
I had to convert some of the class stuff to be in a snippet style. I am not sure why the query works with modTemplateVar
but not with modMediaSourceElement
Snippet For Reference
<?php
$contextKey = $modx->getOption('context', $scriptProperties, 'web');
$outerTpl = $modx->getOption('outerTpl', $scriptProperties, 'images-sitemap-outerTpl');
$rowTpl = $modx->getOption('rowTpl', $scriptProperties, 'images-sitemap-rowTpl');
$itemTpl = $modx->getOption('itemTpl', $scriptProperties, 'images-sitemap-itemTpl');
$output = '';
$usedMediaSourceIds = [];
// Get all modResources and iterate through them
$resources = [];
foreach($modx->getIterator('modResource') as $modResource) {
// if published and not deleted and in right context
if ($modResource->get('published') == 1 && $modResource->get('deleted') == 0 && $modResource->get('context_key') == $contextKey) {
// double check the template is correct
if (!in_array($modResource->get('template'), [0,1])) {
// then save to array
$resources []= $modResource->get('id');
}
}
}
$query = $modx->newQuery('modTemplateVar');
$query->select('modTemplateVar.*', 'Value.*');
$query->leftJoin('modTemplateVarResource', 'Value', ['modTemplateVar.id = Value.tmplvarid']);
$query->where([
'Value.contentid:IN' => $resources,
'Value.value:!=' => '',
'modTemplateVar.type:IN' => ['image', 'migx', 'imagecropper']
]);
$images = [];
if ($imageTVs = $modx->getIterator('modTemplateVar', $query)) {
# This was modMediaSourceElement::class in SeoSuiteSitemap
$query = $modx->newQuery('modMediaSourceElement');
$query->where([
'object_class' => 'modTemplateVar',
'context_key:IN' => $contextKey
]);
$getTVSources = $modx->getIterator('modMediaSourceElement', $query);
$tvSources = [];
if ($getTVSources) {
foreach($getTVSources as $tvSource) {
$tvSources[$tvSource->get('object')] = $tvSource->get('source');
}
}
foreach($imageTVs as $tv) {
$tv = $tv->toArray();
$cid = $tv['contentid'];
if ($tv['type'] === 'image') {
$images[$cid][] = [
'id' => $tv['id'],
'value' => $tv['value'],
'source' => $tvSources[$tv['tmplvarid']]
];
}
if (!in_array($tvSources[$tv['tmplvarid']], $usedMediaSourceIds)) {
$usedMediaSourceIds[] = $tvsources[$tv['tmplvarid']];
}
}
}
if ($resources) {
$mediasources = [];
if (count($usedMediaSourceIds) > 0) {
foreach($usedMediaSourceIds as $mediaSourceId) {
$modx->loadClass('modMediaSource');
if ($source = modMediaSource::getDefaultSource($modx, $mediaSourceId, false)) {
$source->initialize();
$url = ($source->get('class_key') === 'source.modFileMediaSource') ? rtrim(MODX_SITE_URL, '/').'/'.ltrim($source->getBaseUrl(), '/') : '';
$mediasources[$mediaSourceId] = array_merge([ 'full_url' => $url ], $source->toArray());
}
}
}
foreach($resources as $resource) {
$imagesOutput = '';
if (isset($images[$resource])) {
foreach($images[$resource] as $image) {
if (array_key_exists($image['source'], $mediasources)) {
$image['value'] = rtrim($mediasources[$image['source']]['full_url'], '/').'/'.ltrim($image['value'], '/');
$imagesOutput .= $modx->getChunk($itemTpl, [
'url' => $image['value']
]);
}
}
$output .= $modx->getChunk($rowTpl, [
'url' => $modx->makeUrl($resource, '', '', 'full'),
'images' => $imagesOutput
]);
}
}
}
return $modx->getChunk($outerTpl, [
'wrapper' => $output
]);
If I can provide more info to help, lemme know!