• Модуль: iblock
  • Путь к файлу: ~/bitrix/modules/iblock/classes/general/iblockproptools.php
  • Класс: CIBlockPropertyTools
  • Вызов: CIBlockPropertyTools::createProperty
static function createProperty(int $iblockId, string $propertyCode, array $propertyParams = [])
{
	static::clearErrors();

	if ($iblockId <= 0 || $propertyCode === '')
	{
		return false;
	}

	$iblock = IblockIblockTable::getList([
		'select' => ['ID'],
		'filter' => ['=ID' => $iblockId],
	])->fetch();
	if (empty($iblock))
	{
		return false;
	}

	$propertyParams['IBLOCK_ID'] = $iblockId;
	$propertyDescription = static::getPropertyDescription($propertyCode, $propertyParams);
	if ($propertyDescription === false)
	{
		return false;
	}

	$propertyDescription['IBLOCK_ID'] = $iblockId;
	if (!static::validatePropertyDescription($propertyDescription))
	{
		return false;
	}

	$propertyId = 0;
	$getListParams = [
		'select' => ['ID'],
		'filter' => [
			'=IBLOCK_ID' => $iblockId,
			'=CODE' => $propertyCode,
			'=ACTIVE' => 'Y'
		],
	];
	static::modifyGetListParams($getListParams, $propertyCode, $propertyDescription);
	$property = IblockPropertyTable::getList($getListParams)->fetch();
	if (!empty($property))
	{
		if (static::validateExistProperty($propertyCode, $property))
		{
			$propertyId = (int)$property['ID'];
		}
	}
	unset($property);
	if (!empty(self::$errors))
	{
		return false;
	}
	if ($propertyId > 0)
	{
		return $propertyId;
	}

	$propertyObject = new CIBlockProperty();
	$propertyId = (int)$propertyObject->Add($propertyDescription);
	if ($propertyId > 0)
	{
		return $propertyId;
	}
	else
	{
		self::$errors[] = $propertyObject->LAST_ERROR;

		return false;
	}
}