image

TYPO3 How to create typolinks in a scheduler/cron script

To create typolinks in a schedular or CommandController, first we need to initialize the TSFE. Later we can use typoLink_URL() to generate the links.

Below are the steps.

1. Initialize the TSFE

use TYPO3\CMS\Core\Utility\GeneralUtility;

use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

use TYPO3\CMS\Backend\Utility\BackendUtility;

use TYPO3\CMS\Frontend\Utility\EidUtility;

/*

* Initialise tsfe

*/

function initTSFE($id = 1, $typeNum = 0) {

   EidUtility::initTCA();

   if (!is_object($GLOBALS['TT'])) {

       $GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker;

       $GLOBALS['TT']->start();

   }

   $GLOBALS['TSFE'] = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', $GLOBALS['TYPO3_CONF_VARS'], $id, $typeNum);

   $GLOBALS['TSFE']->connectToDB();

   $GLOBALS['TSFE']->initFEuser();

   $GLOBALS['TSFE']->determineId();

   $GLOBALS['TSFE']->initTemplate();

   $GLOBALS['TSFE']->getConfigArray();

   if (ExtensionManagementUtility::isLoaded('realurl')) {

       $rootline = BackendUtility::BEgetRootLine($id);

       $host = BackendUtility::firstDomainRecord($rootline);

       $_SERVER['HTTP_HOST'] = $host;

   }

}

2. Generate typolink

Below is the example function that generates typolink.

private function createLink($detailUid) {

   $this->initTSFE();

   /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj*/

   $cObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');

   $my_url = $cObj->typoLink_URL(array(

       'parameter' => intval($detailUid),

       'additionalParams' => '&kvt='.$my_additionalParameters,

       'additionalParams.insertData' => 1,

       'returnLast' => 'url',

       'useCacheHash' => 1

   ));

   //returns the url

   return $my_url;

}

That's it.