@redesupar

Вывод картинки со связанного свойства в битриксе?

Есть у меня свойство бренд, оно привязано к другому иб, в детальной странице вывод реализован так

/*brand item*/
    $arBrand = array();
    if(strlen($arResult["DISPLAY_PROPERTIES"]["BRAND"]["VALUE"]) && $arResult["PROPERTIES"]["BRAND"]["LINK_IBLOCK_ID"]){
       $arBrand = CNextCache::CIBLockElement_GetList(array('CACHE' => array("MULTI" =>"N", "TAG" => CNextCache::GetIBlockCacheTag($arResult["PROPERTIES"]["BRAND"]["LINK_IBLOCK_ID"]))), array("IBLOCK_ID" => $arResult["PROPERTIES"]["BRAND"]["LINK_IBLOCK_ID"], "ACTIVE"=>"Y", "ID" => $arResult["DISPLAY_PROPERTIES"]["BRAND"]["VALUE"]));
       if($arBrand){
          if($arParams["SHOW_BRAND_PICTURE"] == "Y" && ($arBrand["PREVIEW_PICTURE"] || $arBrand["DETAIL_PICTURE"])){
             $picture = ($arBrand["PREVIEW_PICTURE"] ? $arBrand["PREVIEW_PICTURE"] : $arBrand["DETAIL_PICTURE"]);
             $arBrand["IMAGE"] = CFile::ResizeImageGet($picture, array("width" => 120, "height" => 40), BX_RESIZE_IMAGE_PROPORTIONAL_ALT, true);
             $arBrand["IMAGE"]["ALT"] = $arBrand["IMAGE"]["TITLE"] = $arBrand["NAME"];
             if($arBrand["DETAIL_PICTURE"])
             {
                $arBrand["IMAGE"]["INFO"] = CFile::GetFileArray($arBrand["DETAIL_PICTURE"]);
    
                $ipropValues = new \Bitrix\Iblock\InheritedProperty\ElementValues($arBrand["IBLOCK_ID"], $arBrand["ID"]);
                $arBrand["IMAGE"]["IPROPERTY_VALUES"] = $ipropValues->getValues();
                if($arBrand["IMAGE"]["IPROPERTY_VALUES"]["ELEMENT_DETAIL_PICTURE_FILE_TITLE"])
                   $arBrand["IMAGE"]["TITLE"] = $arBrand["IMAGE"]["IPROPERTY_VALUES"]["ELEMENT_DETAIL_PICTURE_FILE_TITLE"];
                if($arBrand["IMAGE"]["IPROPERTY_VALUES"]["ELEMENT_DETAIL_PICTURE_FILE_ALT"])
                   $arBrand["IMAGE"]["ALT"] = $arBrand["IMAGE"]["IPROPERTY_VALUES"]["ELEMENT_DETAIL_PICTURE_FILE_ALT"];
    
                if($arBrand["IMAGE"]["INFO"]["DESCRIPTION"])
                   $arBrand["IMAGE"]["ALT"] = $arBrand["IMAGE"]["TITLE"] = $arBrand["IMAGE"]["INFO"]["DESCRIPTION"];
             }
          }
       }
    }
    
    $arResult["BRAND_ITEM"]=$arBrand;


и вывожу

<?if($arResult["BRAND_ITEM"]){?>
                 <div class="item_block col-<?=$col;?>">
                    <div class="brand">
                         <met a itemprop="brand" content="<?=$arResult["BRAND_ITEM"]["NAME"]?>" />
                       <?if(!$arResult["BRAND_ITEM"]["IMAGE"]):?>
                          <b class="block_title"><?=GetMessage("BRAND");?>:</b>
                          <a href="<?=$arResult["BRAND_ITEM"]["DETAIL_PAGE_URL"]?>"><?=$arResult["BRAND_ITEM"]["NAME"]?></a>
                       <?else:?>
                          <a class="brand_picture" href="<?=$arResult["BRAND_ITEM"]["DETAIL_PAGE_URL"]?>">
                             <img  src="<?=$arResult["BRAND_ITEM"]["IMAGE"]["src"]?>" alt="<?=$arResult["BRAND_ITEM"]["IMAGE"]["ALT"]?>" title="<?=$arResult["BRAND_ITEM"]["IMAGE"]["TITLE"]?>" />
                          </a>
                       <?endif;?>
                    </div>
                 </div>
              <?}?>


Но в catalo.sections так работать не хочет, менял всё на arItem всеравно неработает, помогите пожалуйста
UPD1

Пытался вывести так, без result_modifier.php сразу в шаблон, а то даже имя не выводило

<div class="brand">
    	<? /*brand item*/
    $arBrand = array();
    if(strlen($arItem["DISPLAY_PROPERTIES"]["BRAND"]["VALUE"]) && $arItem["PROPERTIES"]["BRAND"]["LINK_IBLOCK_ID"]){
    	$arBrand = CNextCache::CIBLockElement_GetList(array('CACHE' => array("MULTI" =>"N", "TAG" => CNextCache::GetIBlockCacheTag($arItem["PROPERTIES"]["BRAND"]["LINK_IBLOCK_ID"]))), array("IBLOCK_ID" => $arItem["PROPERTIES"]["BRAND"]["LINK_IBLOCK_ID"], "ACTIVE"=>"Y", "ID" => $arItem["DISPLAY_PROPERTIES"]["BRAND"]["VALUE"]));
    	if($arBrand){
    		if($arParams["SHOW_BRAND_PICTURE"] == "Y" && ($arBrand["PREVIEW_PICTURE"] || $arBrand["DETAIL_PICTURE"])){
    			$picture = ($arBrand["PREVIEW_PICTURE"] ? $arBrand["PREVIEW_PICTURE"] : $arBrand["DETAIL_PICTURE"]);
    			$arBrand["IMAGE"] = CFile::ResizeImageGet($picture, array("width" => 120, "height" => 40), BX_RESIZE_IMAGE_PROPORTIONAL_ALT, true);
    			$arBrand["IMAGE"]["ALT"] = $arBrand["IMAGE"]["TITLE"] = $arBrand["NAME"];
    			if($arBrand["DETAIL_PICTURE"])
    			{
    				$arBrand["IMAGE"]["INFO"] = CFile::GetFileArray($arBrand["DETAIL_PICTURE"]);
    
    				$ipropValues = new \Bitrix\Iblock\InheritedProperty\ElementValues($arBrand["IBLOCK_ID"], $arBrand["ID"]);
    				$arBrand["IMAGE"]["IPROPERTY_VALUES"] = $ipropValues->getValues();
    				if($arBrand["IMAGE"]["IPROPERTY_VALUES"]["ELEMENT_DETAIL_PICTURE_FILE_TITLE"])
    					$arBrand["IMAGE"]["TITLE"] = $arBrand["IMAGE"]["IPROPERTY_VALUES"]["ELEMENT_DETAIL_PICTURE_FILE_TITLE"];
    				if($arBrand["IMAGE"]["IPROPERTY_VALUES"]["ELEMENT_DETAIL_PICTURE_FILE_ALT"])
    					$arBrand["IMAGE"]["ALT"] = $arBrand["IMAGE"]["IPROPERTY_VALUES"]["ELEMENT_DETAIL_PICTURE_FILE_ALT"];
    
    				if($arBrand["IMAGE"]["INFO"]["DESCRIPTION"])
    					$arBrand["IMAGE"]["ALT"] = $arBrand["IMAGE"]["TITLE"] = $arBrand["IMAGE"]["INFO"]["DESCRIPTION"];
    			}
    		}
    	}
    }
    
    $arItem["BRAND_ITEM"]=$arBrand;   ?>
    
    		<meta itemprop="brand" content="<?=$arItem["BRAND_ITEM"]["NAME"]?>" />
    									<?if(!$arItem["BRAND_ITEM"]["IMAGE"]):?>
    										<b class="block_title"><?=GetMessage("BRAND");?>:</b>
    										<a class="brand_picture" href="<?=$arItem["BRAND_ITEM"]["DETAIL_PAGE_URL"]?>">
    											<img  src="<?=$arItem["BRAND_ITEM"]["IMAGE"]["src"]?>" alt="<?=$arItem["BRAND_ITEM"]["IMAGE"]["ALT"]?>" title="<?=$arItem["BRAND_ITEM"]["IMAGE"]["TITLE"]?>" />
    										</a>
    									<?else:?>
    										<a class="brand_picture" href="<?=$arItem["BRAND_ITEM"]["DETAIL_PAGE_URL"]?>">
    											<img  src="<?=$arItem["BRAND_ITEM"]["IMAGE"]["src"]?>" alt="<?=$arItem["BRAND_ITEM"]["IMAGE"]["ALT"]?>" title="<?=$arItem["BRAND_ITEM"]["IMAGE"]["TITLE"]?>" />
    										</a>
    									<?endif;?>
    							</div>


но картинка не выводится
  • Вопрос задан
  • 638 просмотров
Решения вопроса 1
@PetrPo
redesupar, держи норм код для catalog.section

1. В result_modifier.php в конце добавь
if($arResult['ITEMS']) {
	$currentElement = current($arResult['ITEMS']);
	
	$bPropertyBrandDisplay = isset($currentElement['DISPLAY_PROPERTIES']['BRAND']) ? true : false;
	$bPropertyBrandLink = $bPropertyBrandDisplay && $currentElement['DISPLAY_PROPERTIES']['BRAND']['LINK_IBLOCK_ID'] ? true : false;
	
	$brandsIblockId = $bPropertyBrandLink ? $currentElement['DISPLAY_PROPERTIES']['BRAND']['LINK_IBLOCK_ID'] : 0;

	$brandsId = $brands = [];

	if($bPropertyBrandDisplay && $bPropertyBrandLink) {
		foreach($arResult['ITEMS'] as $arItem) {
			$brandId = (int)$arItem['DISPLAY_PROPERTIES']['BRAND']['VALUE'];
			if($brandId) {
				$brandsId[] = $brandId;
			}
		}

		if($brandsId) {
			$brandsId = array_unique($brandsId);
			$bShowBrandPicture = isset($arParams['SHOW_BRAND_PICTURE']) && $arParams['SHOW_BRAND_PICTURE'] == 'Y' ? true : false;

			$order = ['ID' => 'ASC'];
			$filter = ['IBLOCK_ID' => $brandsIblockId, 'ID' => $brandsId]; // Если надо добавляешь в фильтр активность элементов 'ACTIVE' => 'Y', 'GLOBAL_ACTIVE' => 'Y', 'ACTIVE_DATE' => 'Y'
			$select = ['IBLOCK_ID', 'ID', 'NAME', 'DETAIL_PAGE_URL'];
			if($bShowBrandPicture) {
				$select = array_merge($select, ['PREVIEW_PICTURE', 'DETAIL_PICTURE']);
			}
			
			$result = CIBlockElement::GetList($order, $filter, false, false, $select);
			while($row = $result->GetNext()) {
				$row['PICTURE'] = [];
				
				if($bShowBrandPicture) {
					$pictureResize = ['width' => 120, 'height' => 40];
					$pictureId = $row['PREVIEW_PICTURE'] ? $row['PREVIEW_PICTURE'] : $row['DETAIL_PICTURE'];
					
					if($pictureId) {
						$row['PICTURE'] = CFile::ResizeImageGet($pictureId, $pictureResize, BX_RESIZE_IMAGE_PROPORTIONAL_ALT, true);
						$row['PICTURE']['ALT'] = $row['NAME'];
						$row['PICTURE']['TITLE'] = $row['NAME'];
						
						if($row['DETAIL_PICTURE']) {
							$iProperty = new \Bitrix\Iblock\InheritedProperty\ElementValues($row['IBLOCK_ID'], $row['ID']);
							$iPropertyValues = $iProperty->getValues();
							
							if($iPropertyValues['ELEMENT_DETAIL_PICTURE_FILE_TITLE'])
							   $row['PICTURE']['TITLE'] = $iPropertyValues['ELEMENT_DETAIL_PICTURE_FILE_TITLE'];
						   
							if($iPropertyValues['ELEMENT_DETAIL_PICTURE_FILE_ALT'])
							   $row['PICTURE']['ALT'] = $iPropertyValues['ELEMENT_DETAIL_PICTURE_FILE_ALT'];
						}
					}
				}
				
				$brands[$row['ID']] = $row;
			}
		}
	}
	
	if($brands) {
		foreach($arResult['ITEMS'] as &$arItem) {
			$brandId = (int)$arItem['DISPLAY_PROPERTIES']['BRAND']['VALUE'];
			if($brandId) {
				$arItem['BRAND_ITEM'] = $brands[$brandId];
			}
		}
		
		unset($arItem, $brands, $brandsId, $currentElement);
	}
}


2. В template.php, внутри цикла по элементам
<?if($arItem['BRAND_ITEM']):?>
	<div class="brand">
		<meta itemprop="brand" content="<?=$arItem['BRAND_ITEM']['NAME']?>" />
		<?if($arItem['BRAND_ITEM']['PICTURE']):?>
			<a class="brand_picture" href="<?=$arItem['BRAND_ITEM']['DETAIL_PAGE_URL']?>">
				<img src="<?=$arItem['BRAND_ITEM']['PICTURE']['src'];?>" alt="<?=$arItem['BRAND_ITEM']['PICTURE']['ALT']?>" title="<?=$arItem['BRAND_ITEM']['PICTURE']['TITLE'];?>" />
			</a>
		<?else:?>
			<b class="block_title"><?=GetMessage('BRAND');?>:</b>
			<a href="<?=$arItem['BRAND_ITEM']['DETAIL_PAGE_URL']?>"><?=$arItem['BRAND_ITEM']['NAME']?></a>
		<?endif;?>
	</div>
<?endif;?>


UPD
3. В шаблоне компонента catalog в файле section.php где вызывается catalog.section добавь
"SHOW_BRAND_PICTURE" => $arParams["SHOW_BRAND_PICTURE"],
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы