Skip to content

Instantly share code, notes, and snippets.

@christianseel
Last active August 29, 2015 14:01
Show Gist options
  • Save christianseel/557a1e0f2a1f502ce1c5 to your computer and use it in GitHub Desktop.
Save christianseel/557a1e0f2a1f502ce1c5 to your computer and use it in GitHub Desktop.
Add this as a plugin (OnDocFormSave). Saves the first MoreGallery image into a TV (could be a hidden one) for quicker access.
// Get the images
$corePath = $modx->getOption('moregallery.core_path', null, $modx->getOption('core_path') . 'components/moregallery/');
$moreGallery = $modx->getService('moregallery', 'moreGallery', $corePath . 'model/moregallery/');
$c = $modx->newQuery('mgImage');
$c->where(array(
'resource' => $resource->get('id'),
));
$c->sortby('sortorder', 'ASC');
$c->limit('1');
$image = $modx->getObject('mgImage', $c);
if($image){
$image = $image->toArray();
$resource->setTVValue('ref-image', $image['file_url']);
$resource->save();
}
@pepebe
Copy link

pepebe commented May 29, 2014

Nice plugin Very helpful.

I think you should check if getObject in line 11 returns something.

<?php
// Get the images
$corePath = $modx->getOption('moregallery.core_path', null, $modx->getOption('core_path') . 'components/moregallery/');
$moreGallery = $modx->getService('moregallery', 'moreGallery', $corePath . 'model/moregallery/');
$c = $modx->newQuery('mgImage');
$c->where(array(
    'resource' => $resource->get('id'),
));
$c->sortby('sortorder', 'ASC');
$c->limit('1');

$image = $modx->getObject('mgImage', $c);

if($image){
    $image = $image->toArray();

    $resource->setTVValue('Galerie Thumbnail', $image['file_url']);
    $resource->save();
}

@christianseel
Copy link
Author

Good point! Added it!

@Mark-H
Copy link

Mark-H commented May 29, 2014

As MoreGallery is loaded as an extension package, you shouldn't need those first two lines ($corePath = .. and $moreGallery = ...)... very cool tho!

@pepebe
Copy link

pepebe commented Jun 21, 2015

I've added optional output of crop images to this plugin: https://gist.github.com/pepebe/ad0217f0e8cb0ee870cb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment