Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get gallery's cover from Template Variable
<?php
/**
* getGalleryCoverByTV snippet
* Get gallery's cover from Template Variable
*
* @author goldsky <goldsky@virtudraft.com>
* @license GPLv3
*/
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties);
$modx->setPlaceholder($toPlaceholder, ''); // reset for snippet loop
$resourceId = $modx->getOption('resourceId', $scriptProperties);
$tv = $modx->getOption('tv', $scriptProperties);
if (empty($resourceId) || empty($tv)) {
return;
}
$resource = $modx->getObject('modResource', $resourceId);
if (!$resource) {
return;
}
$galleryId = $resource->getTVValue($tv);
if (empty($galleryId)) {
return;
}
$gallery = $modx->getService('gallery', 'Gallery', $modx->getOption('gallery.core_path', null, $modx->getOption('core_path') . 'components/gallery/') . 'model/gallery/', $scriptProperties);
if (!($gallery instanceof Gallery)) {
return '';
}
$modx->lexicon->load('gallery:web');
$galAlbum = $modx->getObject('galAlbum', $galleryId);
if (!$galAlbum) {
return;
}
$cover = $galAlbum->get('cover_filename');
if (empty($cover)) {
return;
}
$output = $modx->getOption('gallery.files_url') . $cover;
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