Skip to content

Instantly share code, notes, and snippets.

@pepebe
Forked from christianseel/moregallery img to TV
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepebe/ad0217f0e8cb0ee870cb to your computer and use it in GitHub Desktop.
Save pepebe/ad0217f0e8cb0ee870cb to your computer and use it in GitHub Desktop.
Add this as a plugin (OnDocFormSave). Saves the first MoreGallery image or cropimage into a TV (could be a hidden one) for quicker access.
<?php
/*
mgThumb by pepebe
---------------------------------------------
saves the first image in a galley inside a tv
trigger OnDocFormSave
Initial idea: https://gist.github.com/christianseel/557a1e0f2a1f502ce1c5
Changelog:
2014-05-20 - Initial release by christianseel
2014-05-21 - Fork and a minor fix
2015-06-20 - Added option to include crop images
2015-06-21 - Corrected a bug that crept in while cleaning up the code.
$templates - array with ids of all templates this plugin should use
$crop - Name of your cropimage. Set to false if you want to use the image itself.
$basename - if you are using a media resource for your tv, set this to true
$tvId - Id of the TV where you want to store your image
*/
$templates = array(2);
$crop = '1 zu 1'; // Name of your crop. Set to false if you don't want to have a crop image.
$basename = true;
$tvId = 22; // Add the name/id of your Thumbnail TV here
// Only trigger this plugin for resources with templates included in the array
if( !in_array( $resource->get('template') , $templates ) ){
return;
}
$c = $modx->newQuery('mgImage');
$c->where(array(
'resource' => $resource->get('id')
));
$c->sortby('sortorder', 'ASC');
$c->limit('1');
$image = $modx->getObject('mgImage', $c);
$output = "";
if($image){
if(!$crop) {
$props = $image->toArray();
$output = $props['file_url'];
}
else {
$props = $image->getCropsAsArray();
if(isset($props[$crop])){
$output = $props[$crop]['thumbnail_url'];
}
else {
return;
}
}
}
else {
return;
}
if(!empty($output)){
if($basename){
$output = basename($output);
}
$resource->setTVValue($tvId, $output);
$resource->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment