knuklus
12/19/2017 - 4:30 AM

getFirstImg

Компонет tickets и pdo page

//Вывод картинки
<img src="[[+properties.first_img_src.0]]" alt="[[+pagetitle]]">


//Плагин для вывода getFirstImg
<?php
if ($modx->event->name == 'OnBeforeDocFormSave') {

    //get current content for resource
    $content = $resource->getContent();
    if (!empty($content)){
    //get first image in content
    $doc = new DOMDocument();
    $doc->loadHTML($content);
    $xml = simplexml_import_dom($doc);
    $images = $xml->xpath('//img');
    foreach ($images as $img) {
        //also avaliable in array  $img['alt'] и $img['title]
        $file_headers = @get_headers($img['src']);
        if ($file_headers[0] != 'HTTP/1.1 404 Not Found') {
            $first_img_src = $img['src'];
            break;
        }
    }

    //get properties
    $properties = $resource->get('properties');

    //here you can check for empty $first_img_src and set default noImage photo


    //save or override first_img_src
    $properties['first_img_src'] = $first_img_src;
    $resource->set('properties', $properties);


}
}