31. Oktober 2012

cObj in Scheduler-Task verwenden

Scheduler-Tasks haben nicht den vollen Umfang aller TYPO3-Funktionen zur Verfügung, da diese ohnehin oftmals nicht benötigt werden. Manchmal braucht man aber spezielle Funktionen/Methoden, um z.B. Bilder rendern zu lassen oder mit HTML-Templates arbeiten zu können. Hierzu wird oft das cObj benötigt, welches im Scheduler-Task aber nicht vollständig vorliegt bzw. komplett fehlt.

Hierzu sorgen folgende Zeilen für Abhilfe:

chdir(PATH_site);
if(!$GLOBALS['TSFE'] instanceof tslib_fe){
    $GLOBALS['TSFE'] = t3lib_div::makeInstance('tslib_fe', $GLOBALS['TYPO3_CONF_VARS'], 0, 0);
    $GLOBALS['TSFE']->config['config']['language'] = null;
    $GLOBALS['TSFE']->initTemplate();
}
if(!isset($GLOBALS['TT'])){
    $GLOBALS['TT'] = t3lib_div::makeInstance('t3lib_TimeTrackNull');
}
$GLOBALS['TSFE']->tmpl->getFileName_backPath = PATH_site;
$cObj = t3lib_div::makeInstance('tslib_cObj');

Datum: Mittwoch, 31. Oktober 2012 13:02
Trackback: Trackback-URL
Themengebiet: Extensions, TYPO3
Feed zum Beitrag: RSS 2.0
Diesen Artikel kommentieren

4 Antworten zu “cObj in Scheduler-Task verwenden”

  1. Julian sagt:

    Danke für Euren Post. Das habe ich gesucht. Allerdings bekomme ich statt einem cObject einen Fehler:
    Fatal error: Call to a member function push() on a non-object in /(…)/typo3_src/typo3_src-4.5.20/typo3/sysext/cms/tslib/class.tslib_fe.php on line 587

    In welcher TYPO3-Version setzt ihr das ein?

  2. mobasoft sagt:

    Hallo Julian,

    hier mal die komplette Methode createCObj, welche bei uns die Lösung brachte:

    private function createCObj($pid = 1){
    	require_once(PATH_site.'typo3/sysext/cms/tslib/class.tslib_fe.php');
    	require_once(PATH_site.'t3lib/class.t3lib_userauth.php');
    	require_once(PATH_site.'typo3/sysext/cms/tslib/class.tslib_feuserauth.php');
    	require_once(PATH_site.'t3lib/class.t3lib_cs.php');
    	require_once(PATH_site.'typo3/sysext/cms/tslib/class.tslib_content.php') ;
    	require_once(PATH_site.'t3lib/class.t3lib_tstemplate.php');
    	require_once(PATH_site.'t3lib/class.t3lib_page.php');
    	require_once(PATH_site.'t3lib/class.t3lib_timetrack.php');
    
    		// Finds the TSFE classname
    	$TSFEclassName = t3lib_div::makeInstanceClassName('tslib_fe');
    
    		// Create the TSFE class.
    	$GLOBALS['TSFE'] = new $TSFEclassName($GLOBALS['TYPO3_CONF_VARS'], $pid, '0', 0, '','','','');
    
    	$temp_TTclassName = t3lib_div::makeInstanceClassName('t3lib_timeTrack');
    	$GLOBALS['TT'] = new $temp_TTclassName();
    	$GLOBALS['TT']->start();
    
    	$GLOBALS['TSFE']->config['config']['language']=$_GET['L'];
    
    		// Fire all the required function to get the typo3 FE all set up.
    	$GLOBALS['TSFE']->id = $pid;
    	$GLOBALS['TSFE']->connectToMySQL();
    
    		// Prevent mysql debug messages from messing up the output
    	$sqlDebug = $GLOBALS['TYPO3_DB']->debugOutput;
    	$GLOBALS['TYPO3_DB']->debugOutput = false;
    
    	$GLOBALS['TSFE']->initLLVars();
    	$GLOBALS['TSFE']->initFEuser();
    
    		// Look up the page
    	$GLOBALS['TSFE']->sys_page = t3lib_div::makeInstance('t3lib_pageSelect');
    	$GLOBALS['TSFE']->sys_page->init($GLOBALS['TSFE']->showHiddenPage);
    
    		// If the page is not found (if the page is a sysfolder, etc), then return no URL, preventing any further processing which would result in an error page.
    	$page = $GLOBALS['TSFE']->sys_page->getPage($pid);
    
    	if(count($page) == 0){
    		$GLOBALS['TYPO3_DB']->debugOutput = $sqlDebug;
    		return false;
    	}
    
    		// If the page is a shortcut, look up the page to which the shortcut references, and do the same check as above.
    	if($page['doktype']==4 && count($GLOBALS['TSFE']->getPageShortcut($page['shortcut'],$page['shortcut_mode'],$page['uid'])) == 0){
    		$GLOBALS['TYPO3_DB']->debugOutput = $sqlDebug;
    		return false;
    	}
    
    		// Spacer pages and sysfolders result in a page not found page too…
    	if($page['doktype'] == 199 || $page['doktype'] == 254){
    		$GLOBALS['TYPO3_DB']->debugOutput = $sqlDebug;
    		return false;
    	}
    
    	$GLOBALS['TSFE']->getPageAndRootline();
    	$GLOBALS['TSFE']->initTemplate();
    	$GLOBALS['TSFE']->forceTemplateParsing = 1;
    
    		// Find the root template
    	$GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine);
    
    		// Fill the pSetup from the same variables from the same location as where tslib_fe->getConfigArray will get them, so they can be checked before this function is called
    	$GLOBALS['TSFE']->sPre = $GLOBALS['TSFE']->tmpl->setup['types.'][$GLOBALS['TSFE']->type];        // toplevel – objArrayName
    	$GLOBALS['TSFE']->pSetup = $GLOBALS['TSFE']->tmpl->setup[$GLOBALS['TSFE']->sPre.'.'];
    
    		// If there is no root template found, there is no point in continuing which would result in a 'template not found' page and then call exit php. Then there would be no clickmenu at all.
    		// And the same applies if pSetup is empty, which would result in a "The page is not configured" message.
    	if(!$GLOBALS['TSFE']->tmpl->loaded || ($GLOBALS['TSFE']->tmpl->loaded && !$GLOBALS['TSFE']->pSetup)){
    		$GLOBALS['TYPO3_DB']->debugOutput = $sqlDebug;
    		return false;
    	}
    
    	$GLOBALS['TSFE']->getConfigArray();
    	$GLOBALS['TSFE']->getCompressedTCarray();
    
    	$GLOBALS['TSFE']->inituserGroups();
    	$GLOBALS['TSFE']->connectToDB();
    	$GLOBALS['TSFE']->determineId();
    
    	chdir(PATH_site);
    	if(!$GLOBALS['TSFE'] instanceof tslib_fe){
    		$GLOBALS['TSFE'] = t3lib_div::makeInstance('tslib_fe', $GLOBALS['TYPO3_CONF_VARS'], 0, 0);
    		$GLOBALS['TSFE']->config['config']['language'] = null;
    		$GLOBALS['TSFE']->initTemplate();
    	}
    	if(!isset($GLOBALS['TT'])){
    		$GLOBALS['TT'] = t3lib_div::makeInstance('t3lib_TimeTrackNull');
    	}
    	$GLOBALS['TSFE']->tmpl->getFileName_backPath = PATH_site;
    	$cObj = t3lib_div::makeInstance('tslib_cObj');
    
    	return $cObj;
    }
    

    Und dann einfach nur ein

    $this->cObj = $this->createCObj();
    

    und Du kannst auf Dein $this->cObj zugreifen. Bei uns funktioniert es so problemlos!

    P.S.: In der Methode besteht garantiert noch Säuberungsbedarf. Wer möchte, kann sich hier gern daran probieren und sein Ergebnis wieder hier posten. Danke!

  3. Frank sagt:

    Wenn ich diese Funktion im Scheduler einsetze und diesen per Backend starte wird mir (im Backend) eine Seite aus dem Frontent mit Error 404 angezeigt… echt merkwürdig.
    Hab schon Lösungen von anderen Seiten probiert, mit denen ich das TSFE laden und das SetupTS auslesen konnte, aber der aufruf von $this->cObj->enableFields($table) geht dann in der class.tslib_content.php in Zeile 7103 schief, weil dort $GLOBALS[‚TSFE‘]->sys_page->enableFields(…) den Methodenaufruf eines non-objects darstellt.

  4. mobasoft sagt:

    In der Extension sp-guestbook (archiviert) gibt es eine Methode „simulateFrontend“, die das $GLOBALS[‚TSFE‘] für das Backend erzeugt: https://github.com/TYPO3-svn-archive/sp_guestbook/blob/master/Classes/Utility/TypoScript.php

    Für den Scheduler also wie folgt benutzen:

    private function simulateFrontend($cObj = NULL){
    	// Make backup of current frontend
    	$this->frontend = (!empty($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL);
    	// Create new frontend instance
    	$GLOBALS['TSFE'] = new stdClass();
    	$GLOBALS['TSFE']->cObjectDepthCounter = 100;
    	$GLOBALS['TSFE']->cObj = (!empty($cObj) ? $cObj: t3lib_div::makeInstance('tslib_cObj'));
    	if (empty($GLOBALS['TSFE']->sys_page)) {
    		$GLOBALS['TSFE']->sys_page = t3lib_div::makeInstance('t3lib_pageSelect');
    	}
    	if (empty($GLOBALS['TSFE']->tmpl)) {
    		$GLOBALS['TSFE']->tmpl = t3lib_div::makeInstance('t3lib_TStemplate');
    		$GLOBALS['TSFE']->tmpl->getFileName_backPath = PATH_site;
    		$GLOBALS['TSFE']->tmpl->init();
    	}
    	if (empty($GLOBALS['TT'])) {
    		$GLOBALS['TT'] = t3lib_div::makeInstance('t3lib_TimeTrackNull');
    	}
    	/*if (empty($GLOBALS['TSFE']->config)) {
    		$GLOBALS['TSFE']->config = t3lib_div::removeDotsFromTS(self::getSetup());
    	}*/
    }
    

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

TYPO3 Internetagentur Dresden
Diese Webseite verwendet Cookies. Weitere Informationen.