core-bundle/src/Resources/contao/dca/tl_page.php
public function generateAlias($varValue, Contao\DataContainer $dc)
	{
		$objPage = Contao\PageModel::findWithDetails($dc->id);

		$aliasExists = function (string $alias) use ($dc, $objPage): bool
		{
			$objAliasIds = $this->Database->prepare("SELECT id FROM tl_page WHERE alias=? AND id!=?")
										  ->execute($alias, $dc->id);

			if (!$objAliasIds->numRows)
			{
				return false;
			}

			$strCurrentDomain = $objPage->domain;
			$strCurrentLanguage = $objPage->rootLanguage;

			if ($objPage->type == 'root')
			{
				$strCurrentDomain = Contao\Input::post('dns');
				$strCurrentLanguage = Contao\Input::post('language');
			}

			while ($objAliasIds->next())
			{
				$objAliasPage = Contao\PageModel::findWithDetails($objAliasIds->id);

				if ($objAliasPage->domain != $strCurrentDomain)
				{
					continue;
				}

				if (Contao\Config::get('addLanguageToUrl') && $objAliasPage->rootLanguage != $strCurrentLanguage)
				{
					continue;
				}

				// Duplicate alias found
				return true;
			}

			return false;
		};

		// Generate an alias if there is none
		if ($varValue == '')
		{
			$varValue = Contao\System::getContainer()->get('contao.slug')->generate
			(
				$dc->activeRecord->title,
				$dc->activeRecord->id,
				function ($alias) use ($objPage, $aliasExists)
				{
					return $aliasExists((Contao\Config::get('folderUrl') ? $objPage->folderUrl : '') . $alias);
				}
			);

			// Generate folder URL aliases (see #4933)
			if (Contao\Config::get('folderUrl') && $objPage->folderUrl != '')
			{
				$varValue = $objPage->folderUrl . $varValue;
			}
		}
		elseif ($aliasExists($varValue))
		{
			throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $varValue));
		}

		return $varValue;
	}
public function generateAlias($varValue, Contao\DataContainer $dc)
	{
		$objPage = Contao\PageModel::findWithDetails($dc->id);

		$aliasExists = function (string $alias) use ($dc, $objPage): bool
		{
			$objAliasIds = $this->Database->prepare("SELECT id FROM tl_page WHERE alias=? AND id!=?")
										  ->execute($alias, $dc->id);

			if (!$objAliasIds->numRows)
			{
				return false;
			}

			$strCurrentDomain = $objPage->domain;
			$strCurrentLanguage = $objPage->rootLanguage;

			if ($objPage->type == 'root')
			{
				$strCurrentDomain = Contao\Input::post('dns');
				$strCurrentLanguage = Contao\Input::post('language');
			}

			while ($objAliasIds->next())
			{
				$objAliasPage = Contao\PageModel::findWithDetails($objAliasIds->id);

				if ($objAliasPage->domain != $strCurrentDomain)
				{
					continue;
				}

				if (Contao\Config::get('addLanguageToUrl') && $objAliasPage->rootLanguage != $strCurrentLanguage)
				{
					continue;
				}

				// Duplicate alias found
				return true;
			}

			return false;
		};

		// Generate an alias if there is none
		if ($varValue == '')
		{
			$varValue = Contao\System::getContainer()->get('contao.slug')->generate
			(
				$dc->activeRecord->title,
				$dc->activeRecord->id,
				function ($alias) use ($objPage, $aliasExists)
				{
					return $aliasExists((Contao\Config::get('folderUrl') ? $objPage->folderUrl : '') . $alias);
				}
			);

			// Generate folder URL aliases (see #4933)
			if (Contao\Config::get('folderUrl') && $objPage->folderUrl != '')
			{
				$varValue = $objPage->folderUrl . $varValue;
			}
		}
		elseif ($aliasExists($varValue))
		{
			throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $varValue));
		}

		return $varValue;
	}
Auto-generate a page alias if it has not been set yet

@param mixed                $varValue
@param Contao\DataContainer $dc

@return string

@throws Exception