tl_page::generateAlias PHP Method

generateAlias() public method

Auto-generate a page alias if it has not been set yet
public generateAlias ( mixed $varValue, DataContainer $dc ) : string
$varValue mixed
$dc DataContainer
return string
    public function generateAlias($varValue, DataContainer $dc)
    {
        $autoAlias = false;
        // Generate an alias if there is none
        if ($varValue == '') {
            $autoAlias = true;
            $varValue = StringUtil::generateAlias($dc->activeRecord->title);
            // Generate folder URL aliases (see #4933)
            if (Config::get('folderUrl')) {
                $objPage = PageModel::findWithDetails($dc->activeRecord->id);
                if ($objPage->folderUrl != '') {
                    $varValue = $objPage->folderUrl . $varValue;
                }
            }
        }
        $objAlias = $this->Database->prepare("SELECT id FROM tl_page WHERE id=? OR alias=?")->execute($dc->id, $varValue);
        // Check whether the page alias exists
        if ($objAlias->numRows > ($autoAlias ? 0 : 1)) {
            $arrPages = array();
            $strDomain = '';
            $strLanguage = '';
            while ($objAlias->next()) {
                $objCurrentPage = PageModel::findWithDetails($objAlias->id);
                $domain = $objCurrentPage->domain ?: '*';
                $language = !$objCurrentPage->rootIsFallback ? $objCurrentPage->rootLanguage : '*';
                // Store the current page's data
                if ($objCurrentPage->id == $dc->id) {
                    // Get the DNS and language settings from the POST data (see #4610)
                    if ($objCurrentPage->type == 'root') {
                        $strDomain = Input::post('dns');
                        $strLanguage = Input::post('language');
                    } else {
                        $strDomain = $domain;
                        $strLanguage = $language;
                    }
                } else {
                    // Check the domain and language or the domain only
                    if (Config::get('addLanguageToUrl')) {
                        $arrPages[$domain][$language][] = $objAlias->id;
                    } else {
                        $arrPages[$domain][] = $objAlias->id;
                    }
                }
            }
            $arrCheck = Config::get('addLanguageToUrl') ? $arrPages[$strDomain][$strLanguage] : $arrPages[$strDomain];
            // Check if there are multiple results for the current domain
            if (!empty($arrCheck)) {
                if ($autoAlias) {
                    $varValue .= '-' . $dc->id;
                } else {
                    throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $varValue));
                }
            }
        }
        return $varValue;
    }

Usage Example

Exemplo n.º 1
0
 public function adjustAlias($value, $dc)
 {
     // Nothing to adjust if no folderUrls
     if (!$GLOBALS['TL_CONFIG']['folderUrl']) {
         return $value;
     }
     // If current page is of type folder, update children
     if ($dc->activeRecord && $dc->activeRecord->type == 'folder') {
         $childRecords = \Database::getInstance()->getChildRecords(array($dc->id), 'tl_page');
         $this->updateChildren($childRecords);
         return $value;
     }
     $tl_page = new \tl_page();
     // Clean the alias
     $value = $this->cleanAlias($value);
     try {
         $value = $tl_page->generateAlias($value, $dc);
     } catch (\Exception $e) {
         // The alias already exists so add ID just like the original method would
         $value = $value . '-' . $dc->id;
         // Validate the alias once again and throw an error if it exists
         $value = $tl_page->generateAlias($value, $dc);
     }
     return $value;
 }