Skip to content

Instantly share code, notes, and snippets.

@geff21st
Last active April 23, 2018 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geff21st/fbf0abeb5422678e4622ccc288a10dac to your computer and use it in GitHub Desktop.
Save geff21st/fbf0abeb5422678e4622ccc288a10dac to your computer and use it in GitHub Desktop.
<?
defined("B_PROLOG_INCLUDED") && B_PROLOG_INCLUDED === true || die();
if (!Bitrix\Main\Loader::includeModule("iblock")) return;
/**
* Когда нужно выводить элементы и получать из них разделы
* @source: https://gist.github.com/geff21st/fbf0abeb5422678e4622ccc288a10dac
* @global CMain $APPLICATION
* @var array $arParams
* @var array $arResult
*/
$arSections = [];
foreach ($arResult["ITEMS"] as &$arItem) {
if ($sectID = $arItem["IBLOCK_SECTION_ID"]) {
$arItem["SECTION"] = &$arSections[$sectID];
}
unset($arItem);
}
$arOrder = array("sort" => "asc", "date_active_from" => "desc");
$arFilter = array(
"ACTIVE" => "Y",
"GLOBAL_ACTIVE" => "Y",
"ID" => array_keys($arSections),
"IBLOCK_ID" => $arResult["ID"],
);
$arSelect = array("ID", "IBLOCK_ID", "NAME");
$res = CIBlockSection::GetList($arOrder, $arFilter, false, $arSelect);
while ($sect = $res->GetNext()) {
$arSections[$sect["ID"]] = $sect;
}
$arResult["SECTIONS"] = $arSections;
<?
defined("B_PROLOG_INCLUDED") && B_PROLOG_INCLUDED === true || die();
/**
* Когда нужно выводить разделы, в которые вложены элементы
* @source: https://gist.github.com/geff21st/fbf0abeb5422678e4622ccc288a10dac
* @global CMain $APPLICATION
* @var array $arParams
* @var array $arResult
*/
if (!Bitrix\Main\Loader::includeModule("iblock")) return;
$arSections = [];
foreach ($arResult["ITEMS"] as $key => $arItem) {
$sect = !empty($arItem["IBLOCK_SECTION_ID"]) ? $arItem["IBLOCK_SECTION_ID"] : "all";
$arSections[$sect]["ITEMS"][$arItem["ID"]] = &$arResult["ITEMS"][$key];
}
$arNewSections = array();
$arOrder = array("sort" => "asc", "date_active_from" => "desc");
$arFilter = array(
"ACTIVE" => "Y",
"ACTIVE_DATE" => "Y",
"IBLOCK_ID" => $arResult["ID"],
"ID" => array_keys($arSections),
);
$arSelect = array("ID", "IBLOCK_ID", "NAME");
$res = CIBlockSection::GetList($arOrder, $arFilter, false, $arSelect);
while ($sect = $res->GetNext()) {
$arNewSections[$sect["ID"]]["SECT"] = $sect;
$arNewSections[$sect["ID"]]["ITEMS"] = &$arSections[$sect["ID"]]["ITEMS"];
}
if (!empty($arSections["all"]["ITEMS"])) {
$arSections["all"]["SECT"]["NAME"] = $arResult["NAME"];
$arSections["all"]["SECT"]["ID"] = 0;
array_unshift($arNewSections, $arSections["all"]);
}
//d($arSections, "sections");
//d($arResult, "items");
$arResult["SECTIONS"] = $arNewSections;
unset($arSections);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment