Skip to content

Instantly share code, notes, and snippets.

@goldsky
Last active April 28, 2017 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldsky/569b593960a91047fda9cfc16ddfcb47 to your computer and use it in GitHub Desktop.
Save goldsky/569b593960a91047fda9cfc16ddfcb47 to your computer and use it in GitHub Desktop.
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