<?php
namespace MyCustomSeo;
use Bitrix\Main\EventManager;
use Bitrix\Main\EventResult;
use Bitrix\Main\Event;
use Bitrix\Main\Loader;
use Bitrix\Iblock\Template\Functions\FunctionBase;
use Bitrix\Iblock\Template\Entity\Base;
use Bitrix\Iblock\Template\Entity\Element;
// Подключаем модуль инфоблоков и нужный системный класс Битрикс
Loader::includeModule('iblock');
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/iblock/lib/template/functions/fabric.php");
// Регистрируем обработчик события
EventManager::getInstance()->addEventHandler(
"iblock",
"OnTemplateGetFunctionClass",["\MyCustomSeo\ElementProps", "registerCustomFunction"]
);
class ElementProps extends FunctionBase
{
private $elementId = null;
private static $cache =[];
/**
* Метод, который говорит Битриксу, какой класс обрабатывает функцию {=prop ...}
*/
public static function registerCustomFunction(Event $event)
{
$parameters = $event->getParameters();
$functionName = strtolower($parameters[0]);
if ($functionName === "prop") {
return new EventResult(EventResult::SUCCESS, "\\MyCustomSeo\\ElementProps");
}
}
/**
* Получаем ID элемента, для которого генерируется SEO-шаблон
*/
public function onPrepareParameters(Base $entity, $parameters)
{
if ($entity instanceof Element) {
$this->elementId = $entity->getId();
}
$arguments =[];
foreach ($parameters as $parameter) {
$arguments[] = $parameter->process($entity);
}
return $arguments;
}
/**
* Непосредственно сама логика получения данных
*/
public function calculate(array $parameters)
{
if (!$this->elementId || empty($parameters[0])) {
return '';
}
$paramName = strtolower(trim($parameters[0]));
// Кэшируем данные в статическую переменную, чтобы не дергать БД на каждый тег
if (!isset(self::$cache[$this->elementId])) {
$rsElement = \CIBlockElement::GetList([],
["ID" => $this->elementId],
false,
false,["ID", "IBLOCK_ID", "DETAIL_PAGE_URL", "DATE_CREATE", "TIMESTAMP_X", "DETAIL_PICTURE"]
);
if ($arElement = $rsElement->GetNext()) {
self::$cache[$this->elementId] = $arElement;
} else {
self::$cache[$this->elementId] = false;
}
}
$arElement = self::$cache[$this->elementId];
if (!$arElement) return '';
// Возвращаем запрошенное свойство
switch ($paramName) {
case 'url':
return $arElement["DETAIL_PAGE_URL"];
case 'date_create':
return $arElement["DATE_CREATE"];
case 'timestamp_x':
return $arElement["TIMESTAMP_X"];
case 'detail_picture':
if (!empty($arElement["DETAIL_PICTURE"])) {
return \CFile::GetPath($arElement["DETAIL_PICTURE"]);
}
break;
}
return '';
}
}
[Error] Undefined constant "SITE_TEMPLATE_PATH" (0) /var/www/swblog.local.ru/bitrix/modules/main/include/epilog_before.php:93 #0: require /var/www/swblog.local.ru/bitrix/modules/main/include/epilog.php:2 #1: require_once(string) /var/www/swblog.local.ru/bitrix/footer.php:4 #2: require(string) /var/www/swblog.local.ru/bitrix/php_interface/include/seo_prop.php:108 #3: require_once(string) /var/www/swblog.local.ru/bitrix/php_interface/init.php:110 #4: include_once(string) /var/www/swblog.local.ru/bitrix/modules/main/include.php:140 #5: require_once(string) /var/www/swblog.local.ru/bitrix/modules/main/include/prolog_before.php:19 #6: require_once(string) /var/www/swblog.local.ru/bitrix/modules/main/include/prolog.php:10 #7: require_once(string) /var/www/swblog.local.ru/bitrix/header.php:1 #8: require(string) /var/www/swblog.local.ru/index.php:2 #9: include_once(string) /var/www/swblog.local.ru/bitrix/modules/main/include/urlrewrite.php:128 #10: include_once(string) /var/www/swblog.local.ru/bitrix/urlrewrite.php:2 ----------