Index: t3lib/class.t3lib_tcemain.php
===================================================================
--- t3lib/class.t3lib_tcemain.php	(Revision 2163)
+++ t3lib/class.t3lib_tcemain.php	(Arbeitskopie)
@@ -330,6 +330,7 @@
 	var $registerDBList=array();				// Used for tracking references that might need correction after operations
 	var $copyMappingArray = Array();			// Used by the copy action to track the ids of new pages so subpages are correctly inserted! THIS is internally cleared for each executed copy operation! DO NOT USE THIS FROM OUTSIDE! Read from copyMappingArray_merged instead which is accumulating this information.
 	var $remapStack = array();					// array used for remapping uids and values at the end of process_datamap
+	var $remapStackRecords = array();			// array used for remapping uids and values at the end of process_datamap (e.g. $remapStackRecords[<table>][<uid>] = <index in $remapStack>)
 	var $updateRefIndexStack = array();			// array used for additional calls to $this->updateRefIndex
 	var $callFromImpExp = false;				// tells, that this TCEmain was called from tx_impext - this variable is set by tx_impexp
 
@@ -810,10 +811,18 @@
 								 * Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
 								 *		 but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
 								 */
-							foreach($hookObjectsArr as $hookObj)	{
-								if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
-									$hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);
+							if (!isset($this->remapStackRecords[$table][$id])) {
+								foreach($hookObjectsArr as $hookObj)	{
+									if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
+										$hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);
+									}
 								}
+								// If this record is in remapStack, it will be updated later on. So the hook will also be called later:
+							} else {
+								$this->remapStackRecords[$table][$id]['processDatamap_afterDatabaseOperations'] = array(
+									'status' => $status, 
+									'fieldArray' => $fieldArray,
+								);
 							}
 						}	// if ($recordAccess)	{
 					}	// if (is_array($incomingFieldArray))	{
@@ -821,8 +830,6 @@
 			}
 		}
 
-		// call_user_func_array
-
 			// Process the stack of relations to remap/correct
 		if(is_array($this->remapStack)) {
 			foreach($this->remapStack as $remapAction) {
@@ -832,6 +839,7 @@
 					// load values from the argument array in remapAction
 				$field = $remapAction['field'];
 				$id = $remapAction['args'][$remapAction['pos']['id']];
+				$rawId = $id;
 				$table = $remapAction['args'][$remapAction['pos']['table']];
 				$valueArray = $remapAction['args'][$remapAction['pos']['valueArray']];
 				$tcaFieldConf = $remapAction['args'][$remapAction['pos']['tcaFieldConf']];
@@ -864,7 +872,21 @@
 					// @TODO: Add option to disable count-field
 				$newVal = $this->checkValue_checkMax($tcaFieldConf, $remapAction['args'][$remapAction['pos']['valueArray']]);
 				$this->updateDB($table,$id,array($field => implode(',', $newVal)));
+
+					// Process waiting Hook: processDatamap_afterDatabaseOperations:
+				if (isset($this->remapStackRecords[$table][$rawId]['processDatamap_afterDatabaseOperations'])) {
+					$hookArgs = $this->remapStackRecords[$table][$rawId]['processDatamap_afterDatabaseOperations'];
+					$hookArgs['fieldArray'][$field] = implode(',', $newVal);
+					foreach($hookObjectsArr as $hookObj)	{
+						if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
+							$hookObj->processDatamap_afterDatabaseOperations($hookArgs['status'], $table, $rawId, $hookArgs['fieldArray'], $this);
+						}
+					}
+				}
 			}
+				// Reset remapStack:
+			$this->remapStack = array();
+			$this->remapStackRecords = array();
 		}
 
 		$this->dbAnalysisStoreExec();
@@ -1414,6 +1436,7 @@
 		if ($tcaFieldConf['type']=='select' && $tcaFieldConf['foreign_table'])	{
 				// check, if there is a NEW... id in the value, that should be substituded later
 			if (strpos($value, 'NEW') !== false) {
+				$this->remapStackRecords[$table][$id] = array('remapStackIndex' => count($this->remapStack));
 				$this->remapStack[] = array(
 					'func' => 'checkValue_group_select_processDBdata',
 					'args' => array($valueArray,$tcaFieldConf,$id,$status,'select',$table),
@@ -1792,6 +1815,7 @@
 			// $value = 45,NEW4555fdf59d154,12,123
 			// We need to decide whether we use the stack or can save the relation directly.
 		if(strpos($value, 'NEW') !== false || !t3lib_div::testInt($id)) {
+			$this->remapStackRecords[$table][$id] = array('remapStackIndex' => count($this->remapStack));
 			$this->remapStack[] = array(
 				'func' => 'checkValue_group_select_processDBdata',
 				'args' => array($valueArray,$tcaFieldConf,$id,$status,'inline',$table),
