Last active
April 28, 2017 09:23
-
-
Save goldsky/569b593960a91047fda9cfc16ddfcb47 to your computer and use it in GitHub Desktop.
Get gallery's cover from Template Variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* getGalleryCoverByTV snippet | |
* Get gallery's cover from Template Variable | |
* | |
* @author goldsky <[email protected]> | |
* @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