/** * renderSelectWithSelected * * @test */ public function renderSelectWithSelected() { $htmlGenerated = tx_t3devapi_html::renderSelect('test', array(1 => 'test 1', 2 => 'test 2', 3 => 'test 3'), 3, array('class' => 'test')); $html = '<select name="test" id="test" class="test"><option value="1">test 1</option><option value="2">test 2</option><option value="3" selected="selected">test 3</option></select>'; $this->debug($htmlGenerated); $this->assertEquals(trim($htmlGenerated), $html); }
/** * Generate a field from a tca type "select" * * @param string $field * @param string|array $val * @return string */ public function getFieldFromTcaSelect($field, $val) { $config = $this->getFieldConfig($field); $aVal = explode(',', $val); $optionList = array(); $attributes = array(); if (!empty($config['items'])) { foreach ($config['items'] as $item) { $optionList[$item[1]] = $GLOBALS['TSFE']->sL($item[0]); } } if (!empty($config['foreign_table'])) { if (!empty($config['MM'])) { if ($val > 0) { if ($config['maxitems'] > 1) { $aVal = array(); $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $config['MM'], 'uid_local=' . $this->uid); while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $aVal[] = $row['uid_foreign']; } $GLOBALS['TYPO3_DB']->sql_free_result($res); } else { $res = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', $config['MM'], 'uid_local=' . $this->uid); $val = $res[0]['uid_foreign']; } } } $table = $config['foreign_table']; $where = strtolower(substr(trim($config['foreign_table_where']), 0, 3)); $where = trim($where == 'and' || $where == 'or ' || $where == 'gro' || $where == 'ord' || $where == 'lim' ? '1 ' . $config['foreign_table_where'] : $config['foreign_table_where']); $select = 'uid,' . $GLOBALS['TCA'][$table]['ctrl']['label']; $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($select, $table, $where); while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $optionList[$row['uid']] = $row[$GLOBALS['TCA'][$table]['ctrl']['label']]; } $GLOBALS['TYPO3_DB']->sql_free_result($res); } /*if (!empty($config['readOnly'])) { $attributes['disabled'] = 'disabled'; }*/ if (!empty($config['size'])) { $attributes['size'] = $config['size']; } if (count($aVal) > 1 || $config['maxitems'] > 1) { return tx_t3devapi_html::renderMultipleSelect($this->getPrefix($field) . '[]', $optionList, $aVal, $attributes); } else { return tx_t3devapi_html::renderSelect($this->getPrefix($field), $optionList, $val, $attributes); } }