[FEATURE] Function for correct item label in BE
authorGeorg Ringer <mail@ringerge.org>
Fri, 27 Jul 2012 13:21:18 +0000 (15:21 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Sat, 18 Aug 2012 22:30:24 +0000 (00:30 +0200)
The function t3lib_befunc::getLabelFromItemList() gets you the label of
a tca field in the BE. This works nicely but ignores changes done with
TsConfig with code like
TCEFORM.<table>.<field>.addItems.<key> = New value or
TCEFORM.<table>.<field>.aldLabels.<ke> = Change me

The new function will handle it all

Change-Id: Ifd99d321943d72ecb4e16c4974aaa8cddf481b2f
Resolves: #39327
Releases: 6.0
Reviewed-on: http://review.typo3.org/13098
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
t3lib/class.t3lib_befunc.php
tests/Unit/t3lib/class.t3lib_befuncTest.php

index 1b91499..337e9a5 100644 (file)
@@ -1878,6 +1878,46 @@ class t3lib_BEfunc {
        }
 
        /**
+        * Return the label of a field by additionally checking TsConfig values
+        *
+        * @param integer $pageId Page id
+        * @param string $table Table name
+        * @param string $column Field Name
+        * @param string $key item value
+        * @return string Label for item entry
+        */
+       public static function getLabelFromItemListMerged($pageId, $table, $column, $key) {
+               $pageTsConfig = self::getPagesTSconfig($pageId);
+               $label = '';
+               if (
+                       is_array($pageTsConfig['TCEFORM.'])
+                       && is_array($pageTsConfig['TCEFORM.'][$table . '.'])
+                       && is_array($pageTsConfig['TCEFORM.'][$table . '.'][$column . '.'])
+               ) {
+                       if (
+                               is_array($pageTsConfig['TCEFORM.'][$table . '.'][$column . '.']['addItems.'])
+                               && isset($pageTsConfig['TCEFORM.'][$table . '.'][$column . '.']['addItems.'][$key])
+                       ) {
+                               $label = $pageTsConfig['TCEFORM.'][$table . '.'][$column . '.']['addItems.'][$key];
+                       } elseif (
+                               is_array($pageTsConfig['TCEFORM.'][$table . '.'][$column . '.']['altLabels.'])
+                               && isset($pageTsConfig['TCEFORM.'][$table . '.'][$column . '.']['altLabels.'][$key])
+                       ) {
+                               $label = $pageTsConfig['TCEFORM.'][$table . '.'][$column . '.']['altLabels.'][$key];
+                       }
+               }
+
+               if (empty($label)) {
+                       $tcaValue =  self::getLabelFromItemlist($table, $column, $key);
+                       if (!empty($tcaValue)) {
+                               $label = $tcaValue;
+                       }
+               }
+
+               return $label;
+       }
+
+       /**
         * Splits the given key with commas and returns the list of all the localized items labels, separated by a comma.
         * NOTE: this does not take itemsProcFunc into account
         *
index 0aebb17..26076fa 100644 (file)
@@ -184,5 +184,168 @@ class t3lib_befuncTest extends tx_phpunit_testcase {
 
                $this->assertEquals($selectFields, $expectedFields);
        }
+
+       ////////////////////////////////////////////
+       // Tests concerning getLabelFromItemlist
+       ////////////////////////////////////////////
+
+       /**
+        * Data provider for getLabelFromItemlistReturnsCorrectFields
+        *
+        * @return array The test data with $table, $col, $key, $expectedLabel
+        */
+       public function getLabelFromItemlistReturnsCorrectFieldsDataProvider() {
+               return array(
+                       'item set' => array(
+                               'table' => 'tt_content',
+                               'col' => 'menu_type',
+                               'key' => '1',
+                               'tca' => array(
+                                       'columns' => array(
+                                               'menu_type'=> array(
+                                                       'config' => array(
+                                                               'items' => array(
+                                                                       array('Item 1', '0'),
+                                                                       array('Item 2', '1'),
+                                                                       array('Item 3', '3'),
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               'expectedLabel' => 'Item 2',
+                       ),
+                       'item set twice' => array(
+                               'table' => 'tt_content',
+                               'col' => 'menu_type',
+                               'key' => '1',
+                               'tca' => array(
+                                       'columns' => array(
+                                               'menu_type'=> array(
+                                                       'config' => array(
+                                                               'items' => array(
+                                                                       array('Item 1', '0'),
+                                                                       array('Item 2a', '1'),
+                                                                       array('Item 2b', '1'),
+                                                                       array('Item 3', '3'),
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               'expectedLabel' => 'Item 2a',
+                       ),
+                       'item not found' => array(
+                               'table' => 'tt_content',
+                               'col' => 'menu_type',
+                               'key' => '5',
+                               'tca' => array(
+                                       'columns' => array(
+                                               'menu_type'=> array(
+                                                       'config' => array(
+                                                               'items' => array(
+                                                                       array('Item 1', '0'),
+                                                                       array('Item 2', '1'),
+                                                                       array('Item 3', '2'),
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               'expectedLabel' => NULL,
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider getLabelFromItemlistReturnsCorrectFieldsDataProvider
+        */
+       public function getLabelFromItemlistReturnsCorrectFields($table, $col = '', $key = '', array $tca, $expectedLabel = '') {
+               t3lib_div::loadTCA($table);
+               $tcaBackup = $GLOBALS['TCA'][$table];
+               unset($GLOBALS['TCA'][$table]);
+               $GLOBALS['TCA'][$table] = $tca;
+
+               $label = $this->fixture->getLabelFromItemlist($table, $col, $key);
+
+               unset($GLOBALS['TCA'][$table]);
+               $GLOBALS['TCA'][$table] = $tcaBackup;
+
+               $this->assertEquals($label, $expectedLabel);
+       }
+
+
+       ////////////////////////////////////////////
+       // Tests concerning getLabelFromItemListMerged
+       ////////////////////////////////////////////
+
+       /**
+        * Data provider for getLabelFromItemListMerged
+        *
+        * @return array The test data with $pageId, $table, $column, $key, $expectedLabel
+        */
+       public function getLabelFromItemListMergedReturnsCorrectFieldsDataProvider() {
+               return array(
+                       'no field found' => array(
+                               'pageId' => '123',
+                               'table' => 'tt_content',
+                               'col' => 'menu_type',
+                               'key' => '10',
+                               'tca' => array(
+                                       'columns' => array(
+                                               'menu_type'=> array(
+                                                       'config' => array(
+                                                               'items' => array(
+                                                                       array('Item 1', '0'),
+                                                                       array('Item 2', '1'),
+                                                                       array('Item 3', '3'),
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               'expectedLabel' => '',
+                       ),
+                       'no tsconfig set' => array(
+                               'pageId' => '123',
+                               'table' => 'tt_content',
+                               'col' => 'menu_type',
+                               'key' => '1',
+                               'tca' => array(
+                                       'columns' => array(
+                                               'menu_type'=> array(
+                                                       'config' => array(
+                                                               'items' => array(
+                                                                       array('Item 1', '0'),
+                                                                       array('Item 2', '1'),
+                                                                       array('Item 3', '3'),
+                                                               )
+                                                       )
+                                               )
+                                       )
+                               ),
+                               'expectedLabel' => 'Item 2',
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider getLabelFromItemListMergedReturnsCorrectFieldsDataProvider
+        */
+       public function getLabelFromItemListMergedReturnsCorrectFields($pageId, $table, $column = '', $key = '', array $tca, $expectedLabel = '') {
+               t3lib_div::loadTCA($table);
+               $tcaBackup = $GLOBALS['TCA'][$table];
+               unset($GLOBALS['TCA'][$table]);
+               $GLOBALS['TCA'][$table] = $tca;
+
+               $label = $this->fixture->getLabelFromItemListMerged($pageId, $table, $column, $key);
+
+               unset($GLOBALS['TCA'][$table]);
+               $GLOBALS['TCA'][$table] = $tcaBackup;
+
+               $this->assertEquals($label, $expectedLabel);
+       }
 }
 ?>
\ No newline at end of file