- Модуль: landing
- Путь к файлу: ~/bitrix/modules/landing/lib/node/img.php
- Класс: BitrixLandingNodeImg
- Вызов: Img::saveNode
static function saveNode(BitrixLandingBlock $block, $selector, array $data)
{
$doc = $block->getDom();
$resultList = $doc->querySelectorAll($selector);
$valueBefore = static::getNode($block, $selector);
$files = null;
foreach ($data as $pos => $value)
{
// 2x - this for retina support
$src = (isset($value['src']) && is_string($value['src'])) ? trim($value['src']) : '';
$src2x = (isset($value['src2x']) && is_string($value['src2x'])) ? trim($value['src2x']) : '';
$alt = (isset($value['alt']) && is_string($value['alt'])) ? trim($value['alt']) : '';
$id = isset($value['id']) ? intval($value['id']) : 0;
$id2x = isset($value['id2x']) ? intval($value['id2x']) : 0;
$isLazy = isset($value['isLazy']) && $value['isLazy'] === 'Y';
if ($src)
{
$src = str_replace('http://', 'https://', $src);
}
if ($src2x)
{
$src2x = str_replace('http://', 'https://', $src2x);
}
if (isset($value['url']))
{
$url = is_array($value['url'])
? json_encode($value['url'])
: $value['url'];
}
else
{
$url = '';
}
if (isset($resultList[$pos]))
{
// check permissions to this file ids
if ($id || $id2x)
{
if ($files === null)
{
$files = File::getFilesFromBlock($block->getId());
}
if (!in_array($id, $files))
{
$id = 0;
}
if (!in_array($id2x, $files))
{
$id2x = 0;
}
}
// update in content
if ($resultList[$pos]->getTagName() !== 'IMG')
{
$styles = StyleInliner::getStyle($resultList[$pos]);
$oldStyles = [];
$newStyles = [];
// collect existing styles
foreach ($styles as $key => $styleValue)
{
if ($key !== 'background' && $key !== 'background-image')
{
$oldStyles[] = "{$key}: {$styleValue};";
}
}
// add images to bg
if ($src)
{
// and one two additional bg
$newStyles = [
"background-image: url('{$src}');",
];
if ($src2x)
{
$newStyles = array_merge(
$newStyles,
[
"background-image: -webkit-image-set(url('{$src}') 1x, url('{$src2x}') 2x);",
"background-image: image-set(url('{$src}') 1x, url('{$src2x}') 2x);",
]
);
}
}
// or remove exists
else
{
foreach (['fileid', 'fileid2x'] as $dataCode)
{
$oldId = $resultList[$pos]->getAttribute(
'data-' . $dataCode
);
if ($oldId > 0)
{
File::deleteFromBlock(
$block->getId(),
$oldId
);
}
}
}
$style = array_merge($oldStyles, $newStyles);
$style = implode(' ', $style);
$resultList[$pos]->setAttribute('style', $style);
// for lazyload
if ($isLazy)
{
$resultList[$pos]->setAttribute('data-lazy-bg', 'Y');
if ($lazyOrigSrc = $value['lazyOrigSrc'])
{
$resultList[$pos]->setAttribute('data-src', $lazyOrigSrc);
}
if ($lazyOrigSrc2x = $value['lazyOrigSrc2x'])
{
$resultList[$pos]->setAttribute('data-src2x', $lazyOrigSrc2x);
}
if ($lazyOrigStyle = $value['lazyOrigStyle'])
{
$resultList[$pos]->setAttribute('data-style', $lazyOrigStyle);
}
}
}
else
{
$resultList[$pos]->setAttribute('alt', $alt);
$resultList[$pos]->setAttribute('src', $src);
if ($src2x)
{
$resultList[$pos]->setAttribute('srcset', "{$src2x} 2x");
}
else
{
$resultList[$pos]->setAttribute('srcset', '');
}
// for lazyload
if ($isLazy)
{
$resultList[$pos]->setAttribute('data-lazy-img', 'Y');
$resultList[$pos]->setAttribute('loading', 'lazy');
if ($lazyOrigSrc = $value['lazyOrigSrc'])
{
$resultList[$pos]->setAttribute('data-src', $lazyOrigSrc);
}
if ($lazyOrigSrcset = $value['lazyOrigSrcset'])
{
$resultList[$pos]->setAttribute('data-srcset', $lazyOrigSrcset);
}
}
}
$id
? $resultList[$pos]->setAttribute('data-fileid', $id)
: $resultList[$pos]->removeAttribute('data-fileid')
;
$id2x
? $resultList[$pos]->setAttribute('data-fileid2x', $id2x)
: $resultList[$pos]->removeAttribute('data-fileid2x')
;
$url
? $resultList[$pos]->setAttribute('data-pseudo-url', $url)
: $resultList[$pos]->removeAttribute('data-pseudo-url')
;
if (History::isActive())
{
$history = new History($block->getLandingId(), History::ENTITY_TYPE_LANDING);
$history->push('EDIT_IMG', [
'block' => $block,
'selector' => $selector,
'position' => (int)$pos,
'valueBefore' => $valueBefore[$pos],
'valueAfter' => $value,
]);
}
}
}
}