Search code examples
typo3tt-news

Automatically set author and email in tt_news


I know I can configure tt_news to automatically set author and email to a given value, like this:

TCAdefaults.tt_news.author = full name
TCAdefaults.tt_news.author_email = name@domain.tld

But could I retrieve the name an email from the info of the currently logged BE user instead?


Solution

  • To meake it possible to fill the values on every change (if empty), you need to register a hook somewhere ie, in your own extension

    typo3conf/ext/yourext/hooks/class.tx_ttnews_hooks.php

    class tx_ttnews_hooks {
    // hook for prefilling TCA values
    function getSingleField_preProcess($table, $field, &$row, $altName, $palette, $extra, $pal, $pObj) {
    
            switch($field) {
                case 'author_email':
                    if($row[$field] == '') {
                        $row[$field] = $GLOBALS['BE_USER']->user['email'];
                    }
                    break;
    
                case 'author':
                    if($row[$field] == '') {
                        $row[$field] = $GLOBALS['BE_USER']->user['realName'];
                    }
                    break;
             }
        }
    }
    

    and then add this into typo3conf/ext/yourext/ext_localconf.php:

    $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tceforms.php']['getSingleFieldClass'][] 
        = 'EXT:yourext/hooks/class.tx_ttnews_hooks.php:tx_ttnews_hooks';