Dmitri Pisarev
Dmitri Pisarev

Reputation: 1173

Typo3 RealURL news links with news id

I want my news article URLs to be of the following form: http://domain.com/news/news-title-for-seo-and-usability-324/

And I want only the news id to be used in decoding, and title to be added only for SEO purposes.

How can I acomplish it with either RealURL, CoolURI or anything else?

edit: so far I managed to do what I wanted with the following two userfuncs:

array(
'GETvar' => 'tx_news_pi1[news]',
'userFunc' => 'EXT:speciality/Classes/Hooks/RealUrlUserFunc.php:&Tx_Speciality_Hooks_RealUrlUserFunc->main',
                'lookUpTable_fake' => array(
                    'table' => 'tx_news_domain_model_news',
                    'id_field' => 'uid',
                    'alias_field' => 'title',
                    'addWhereClause' => ' AND NOT deleted AND NOT hidden',
                    'useUniqueCache' => 1,
                    'useUniqueCache_conf' => array(
                        'strtolower' => 1,
                        'spaceCharacter' => '-',
                        'encodeTitle_userProc' => 'EXT:speciality/Classes/Hooks/RealUrlUserFunc.php:&Tx_Speciality_Hooks_RealUrlUserFunc->user_newsid',
                    ),
                ),
            ),

And the userfuncs:

class Tx_Speciality_Hooks_RealUrlUserFunc {
public function main(array $params, $parent) {
    $this->pObj = $parent;

    if($params['decodeAlias']) {
        return  $this->alias2id($params); 
    } else {
        return  $this->id2alias($params); 
    } 
}

function alias2id($params){
    return array_pop(explode('-', $params['value']));
}
function id2alias($params){
    return $this->pObj->lookUpTranslation($params['setup']['lookUpTable_fake'], $params['value'], FALSE);
}

function user_newsid($params) {
    if($params['pObj']->orig_paramKeyValues['tx_news_pi1[news]'])
        return $params['processedTitle'] ."-". $params['pObj']->orig_paramKeyValues['tx_news_pi1[news]'];
    else
        return $params['processedTitle'];
}
}

The only problem so far is that lookUpTranslation is a protected function, so I had to temporary hack Realurl to make that fucntion public.

So how do I encode a title from my userfunc the right way?

Upvotes: 1

Views: 648

Answers (1)

fnagel
fnagel

Reputation: 702

Perhaps there is a easier way to do this but it's definitly possible with RealUrl by using a hook. Keyword is "encodeTitle_userProc", see here: http://docs.typo3.org/typo3cms/extensions/realurl/1.12.7/Realurl/ClasstxRealurlAdvancedphp/Configuration/Index.html?highlight=encodetitle_userproc

Here's a simple example to remove the registered sign from a URL:

RealUrlConfig:

'product' => array(
    array(
        'GETvar' => 'tx_myextension[product]',
        'lookUpTable' => array(
            ....
            'useUniqueCache_conf' => array(
                'strtolower' => 1,
                'spaceCharacter' => '-',
                'encodeTitle_userProc' => 'EXT:tx_myextension/Classes/Hooks/RealUrlUserFunc.php:&Tx_Myextension_Hooks_RealUrlUserFunc->user_productsTitle',
            ),
            ...
        ),
    ),
),

And the hook class:

class Tx_Myextension_Hooks_RealUrlUserFunc {
    function user_productsTitle($params) {
        return preg_replace('/[R]{1}/', '', $params['processedTitle']);
    }
}

Upvotes: 2

Related Questions