# Fix #8986:
# * Implement content_fallback for page records (menus etc.)
# * Implement content_fallback per content element instead of just the language (not used globally yet - needs more testing)
Index: t3lib/class.t3lib_page.php
===================================================================
--- t3lib/class.t3lib_page.php	(Revision 5350)
+++ t3lib/class.t3lib_page.php	(Arbeitskopie)
@@ -295,18 +295,35 @@
 					However you may argue that the showHiddenField flag should determine this. But that's not how it's done right now.
 				*/
 
-					// Selecting overlay record:
+				list($sys_language_mode,$language_list) = t3lib_div::trimExplode(';', $GLOBALS['TSFE']->config['config']['sys_language_mode']);
+				if ($sys_language_mode === 'content_fallback' && $GLOBALS['TSFE']->sys_language_contentOL !== 'hideNonTranslated') {
+					$lUidList = t3lib_div::intExplode(',', $lUid . ',' . $language_list);
+				}
+
+					// Fetch all language overlay records which are configured for content_fallback and filter out the right one (according to content_fallback order) afterwards
 				$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
-							implode(',',$fieldArr),
+								// Append virtual field which is removed again afterwards (use another name to avoid removal if the field was explicitely requested)
+								// Make sure that language ids ($lUidList) are integer values!
+							implode(',',$fieldArr) . ', sys_language_uid AS _sys_language_uid',
 							'pages_language_overlay',
 							'pid='.intval($page_id).'
-								AND sys_language_uid='.intval($lUid).
+								AND sys_language_uid IN (' . implode(',', $lUidList ) . ')' .
 								$this->enableFields('pages_language_overlay'),
-							'',
-							'',
-							'1'
+							'sys_language_uid'
 						);
-				$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+				$totalRows = array();
+				while ($tmpRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+					$totalRows[$tmpRow['_sys_language_uid']] = $tmpRow;
+				}
+
+					// Loop through the list of overlay languages and find the first one that matches (according to order specified in config.sys_language_mode)
+				foreach ($lUidList as $lUid) {
+					if (is_array($totalRows[$lUid])) {
+						$row = $totalRows[$lUid];
+						break;
+					}
+				}
+
 				$GLOBALS['TYPO3_DB']->sql_free_result($res);
 				$this->versionOL('pages_language_overlay',$row);
 
@@ -316,6 +333,7 @@
 						// Unset vital fields that are NOT allowed to be overlaid:
 					unset($row['uid']);
 					unset($row['pid']);
+					unset($row['_sys_language_uid']);
 				}
 			}
 		}
Index: typo3/sysext/cms/tslib/class.tslib_content.php
===================================================================
--- typo3/sysext/cms/tslib/class.tslib_content.php	(Revision 5350)
+++ typo3/sysext/cms/tslib/class.tslib_content.php	(Arbeitskopie)
@@ -1309,7 +1309,7 @@
 
 							// Language overlay:
 						if (is_array($row) && $GLOBALS['TSFE']->sys_language_contentOL) {
-							$row = $GLOBALS['TSFE']->sys_page->getRecordOverlay($conf['table'],$row,$GLOBALS['TSFE']->sys_language_content,$GLOBALS['TSFE']->sys_language_contentOL);
+							$this->languageOverlay($conf['table'], $row);
 						}
 
 						if (is_array($row)) { // Might be unset in the sys_language_contentOL
@@ -1352,6 +1352,32 @@
 	}
 
 	/**
+	 * Perform language overlay
+	 *
+	 * @param	string		Table name
+	 * @param	array		Record to be overlayed
+	 * @return	void
+	 */
+	function languageOverlay($table, &$row) {
+		list($sys_language_mode,$language_list) = t3lib_div::trimExplode(';', $GLOBALS['TSFE']->config['config']['sys_language_mode']);
+		if ($sys_language_mode === 'content_fallback' && $GLOBALS['TSFE']->sys_language_contentOL !== 'hideNonTranslated') {
+			$tmpRow = $GLOBALS['TSFE']->sys_page->getRecordOverlay($table,$row,$GLOBALS['TSFE']->sys_language_content,'hideNonTranslated');
+			if (empty($tmpRow)) {
+				foreach (t3lib_div::intExplode(',', $language_list) as $sys_language_content) {
+					$tmpRow = $GLOBALS['TSFE']->sys_page->getRecordOverlay($table,$row,$sys_language_content,'hideNonTranslated');
+					if ($tmpRow) {
+						break;
+					}
+				}
+			}
+			$row = $tmpRow;
+			unset ($tmpRow);
+		} else {
+			$row = $GLOBALS['TSFE']->sys_page->getRecordOverlay($table,$row,$GLOBALS['TSFE']->sys_language_content,$GLOBALS['TSFE']->sys_language_contentOL);
+		}
+	}
+
+	/**
 	 * Rendering the cObject, RECORDS
 	 *
 	 * @param	array		Array of TypoScript properties
@@ -1402,7 +1428,7 @@
 
 					// Language overlay:
 				if (is_array($row) && $GLOBALS['TSFE']->sys_language_contentOL)	{
-					$row = $GLOBALS['TSFE']->sys_page->getRecordOverlay($val['table'],$row,$GLOBALS['TSFE']->sys_language_content,$GLOBALS['TSFE']->sys_language_contentOL);
+					$this->languageOverlay($val['table'], $row);
 				}
 
 				if (is_array($row))	{	// Might be unset in the content overlay things...
