Save-and-view button for records of TYPO3 extensions
When editing a page in the TYPO3 backend, there's a whole bunch of save buttons available. The "save document and view page" button allows to save the content and open a preview popup with one click. Unfortunately this does only work for regular pages and content elements, but not for database records of extensions which are stored inside sysfolders. The following tutorial describes a way to get that button also running for those records.
The solution is to hook into the post-process of saving records in TCEmain and set the GET parameters needed for the preview page. The idea is taken from the tt_news extension. Thanks to the author Rupert Germann!
Hooks in TYPO3
Using hooks is a common way to add functionality or change behaviour of the TYPO3 core. Although it sometimes looks like hardcore coding stuff, it's not that hard to understand. The TYPO3 Core API documentation on typo3.org gives you an overview about hooks in TYPO3. To get a list of hooks which exist in TYPO3, you can use the extension dmc HookList (dmc_hooklist).
Preassumptions
This tutorial assumes you have an extension called extExample. extExample is used as the extension key. The extension stores its records in the DB table tx_extExample_data. The records are kept in a sysfolder in the BE. There's a frontend plugin tx_extExample_pi1 which displays a single record on a page (page id=123) and using the GET-parameter tx_extExample_pi1[showUid] for the uid of the record. A very common type of frontend extension, isn't it? Just replace the emphasized keywords to use the snippets in your extension.
What do you need to do in your own extension to get this button running?
There's only three things to do:
- add a class to your extension with a method to set the proper preview parameters,
- register the hook in your extension to tell TYPO3 to call this method,
- set the page ID of the single view in pageTS.
Writing a method to set the preview parameters
The method adds the needed information to a global variable which itself is processed by TCE when openening the popup window for the preview.
typo3conf/ext/extExample/classes/class.tx_extExample_tcemain.php
<?php
class tx_extExample_tcemain {
public function processDatamap_preProcessFieldArray(&$fieldArray, $table, $uid, &$pObj) {
if ($table == 'tx_extExample_data' && isset($GLOBALS['_POST']['_savedokview_x']) && !$GLOBALS['BE_USER']->workspace) {
// The GETparam which is used in the single view of the plugin
$getParam = 'tx_extExample_pi1[showUid]';
// get page TSconfig
$pageTS = t3lib_BEfunc::getPagesTSconfig($GLOBALS['_POST']['popViewId']);
// get pid of single view
$singlePid = $pageTS['tx_extExample.']['singlePid'];
if ($singlePid) {
// set page id to be shown
$GLOBALS['_POST']['popViewId'] = $singlePid;
// don't cache the page
$GLOBALS['_POST']['popViewId_addParams'] = '&no_cache=1';
// set language parameter
if ($fieldArray['sys_language_uid'] > 0) {
$GLOBALS['_POST']['popViewId_addParams'] .= '&L=' . $fieldArray['sys_language_uid'];
}
// set plugin GETparams
$GLOBALS['_POST']['popViewId_addParams'] .= '&' . $getParam . '=' . $uid;
}
}
}
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/extExample/classes/class.tx_extExample_tcemain.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/extExample/classes/class.tx_extExample_tcemain.php']);
}
?>
Registering the hook
Hooks are registered in your extension by adding a simple oneliner to ext_localconf.php:
ext_localconf.php
$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = 'EXT:exampleExt/classes/class.tx_exampleExt_tcemain.php:tx_exampleExt_tcemain';
Set the singlePid in pageTS
Finaly, you need to set the page ID of the frontend single view in pageTS. It's the pid of the page which should be opened in the preview.
pageTS (TSconfig)
tx_extExample.singlePid = 123
That's it! Now clear the configuration cache in the TYPO3 backend, open a record and try to "save and preview".
This solution works only in LIVE workspace. I didn't test it within otther workspaces.
Comments
Hey,
this is exactly this, what i was searching for.
Also it is described very well.
Thak you for your help.
Thanks Steffen,
works charming!
Happy X-Mas
Since 7.2 there is a nice feature implemented;
See:
https://docs.typo3.org/typo3cms/extensions/core/Changelog/7.2/Feature-66370-AddFlexiblePreviewUrlConfiguration.html