• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/uf/fileusertype.php
  • Класс: BitrixDiskUfFileUserType
  • Вызов: FileUserType::onBeforeSave
static function onBeforeSave($userField, $value, $userId = false)
{
	[$type, $realValue] = self::detectType($value);

	if (empty($value))
	{
		$alreadyExistsValues = $userField['VALUE'];
		if (!is_array($alreadyExistsValues))
		{
			$alreadyExistsValues = array($userField['VALUE']);
		}
		AttachedObject::detachByFilter(array('ID' => $alreadyExistsValues));

		return $value;
	}

	if ($type === self::TYPE_NEW_OBJECT)
	{
		$fileModel = self::getFileById($realValue);
		if (!$fileModel || !$fileModel->getStorage())
		{
			return '';
		}

		if ($userId === false)
		{
			$securityContext = $fileModel->getStorage()->getCurrentUserSecurityContext();
		}
		else
		{
			$securityContext = $fileModel->getStorage()->getSecurityContext($userId);
		}

		$canUpdate = $fileModel->canUpdate($securityContext);
		$attachedModel = $fileModel->attachToEntity(
			[
				'id' => $userField['VALUE_ID'],
				'type' => $userField['ENTITY_ID'],
			],
			[
				'isEditable' => $canUpdate,
				'allowEdit' => ($canUpdate && (int)self::getValueForAllowEdit($userField, $value)),
				'createdBy' => $userId === false? self::getActivityUserId() : $userId,
			]
		);

		if (!$attachedModel)
		{
			return '';
		}

		return $attachedModel->getId();
	}
	else if ($type === self::TYPE_ALREADY_ATTACHED)
	{
		$allowEdit = self::getValueForAllowEdit($userField, $value);
		if ($allowEdit !== null)
		{
			self::changeAllowEdit($realValue, $allowEdit);
		}
	}

	return $realValue;
}