Sep. 16., 2009

"Save and view" button for records of TYPO3 extensions

By Steffen Müller. Licensed under the Creative Commons License

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.

--> Back to the list of articles

License

Licensed under creative commonsThis article is licensed under the Creative Commons License CC BY-SA 3.0. You are free to share (copy, distribute and transmit) and to remix (to adapt) the work under the following conditions:

  • You must attribute the work by mentioning the name of the author (Steffen Müller) and setting a link back to the original article using its URL.
  • If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

Comments

  1. Andreas Becker wrote on December 2, 2010 at 14:11

    Hey,
    this is exactly this, what i was searching for.
    Also it is described very well.

    Thak you for your help.

  2. Christian Grlica wrote on December 19, 2011 at 18:43

    Thanks Steffen,

    works charming!

    Happy X-Mas


Leave a comment:

This page uses static caches. Make sure you reload the page in your browser after posting a comment.

(will not be published)

CAPTCHA image for SPAM prevention Click here for audio version of the word to enter.

If you can't read the captcha word, please click to load a new image.
(You need Javascript turned on. Otherwise press the submit button and wait until the page has reloaded.)