R
R
redesupar2020-09-01 13:03:40
1C-Bitrix
redesupar, 2020-09-01 13:03:40

Displaying a picture from a related property in Bitrix?

I have a brand property, it is tied to another ib, in the detailed page the output is implemented like this

/*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;


and take out

<?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>
              <?}?>


But in catalo.sections it doesn’t want to work like that, changing everything to arItem still doesn’t work, please help
UPD1

I tried to display it like this, without result_modifier.php immediately into the template, otherwise even the name didn’t display

<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>


but the picture is not displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PetrPo, 2020-09-01
@redesupar

redesupar , keep the code for catalog.section
1. At the end of result_modifier.php add

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. In template.php, inside the element loop
<?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. In the catalog component template, in the section.php file where catalog.section is called, add
"SHOW_BRAND_PICTURE" => $arParams["SHOW_BRAND_PICTURE"],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question