Skip to content

Instantly share code, notes, and snippets.

@govza
Created February 11, 2013 19:58
Show Gist options
  • Save govza/4757102 to your computer and use it in GitHub Desktop.
Save govza/4757102 to your computer and use it in GitHub Desktop.
Get first Image from modx content and add to firstImageTV
<?php
//OnDocFormSave
$img_path = $modx->getOption('imagesPath',$scriptProperties, MODX_ASSETS_URL."static/images/");
$imgName = $modx->getOption('ImageName',$scriptProperties,'default.png');
$TvValue = $modx->getOption('TV',$scriptProperties,'firstImage');
$def_img = $img_path.$imgName;
$img = $resource->GetTVValue($TvValue);
$text = $resource->content;
if(empty($img)) {
if (preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i',$text , $matches )!=0)
{
$img = $matches[1][0];
}
else{
$img = $def_img;
}
}
$resource->setTVValue($TvValue,$img);
//$modx->event->output('внимание: '.$output);
$resource->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment