Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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