ShowRowViewHelper
Eigener ViewHelper für Ausgabe einer ganzen Zeile
Um den Viewhelper zu erstellen zunächst das Verzeichnis Classes/ViewHelpers/Detail anlegen. Dort eine Datei anlegen ShowRowViewHelper.php. Dieser ViewHelper nimmt 4 Argumente: Label, Value, Type und Unit. Er gibt ein Feld mit Beschriftung für die Detailansicht aus. Der Html Code ist per TS konfigurierbar.
Eingesetzt habe ich den ViewHelper allerdings nirgends - aber der Vollständigkeit halber ist er hier mit aufgeführt.
Listing
<?php
/* *
* This script belongs to the FLOW3 package "Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* *
* This script is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
* General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with the script. *
* If not, see www.gnu.org/licenses/lgpl.html *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
*
* = Examples =
*
* <code title="default notation">
* {namespace rsys=Tx_Rsysproductbase_ViewHelpers}
* <rsys:Detail.ShowRow value="{product.title}" label="tx_rsysproductbase_domain_model_product.title"/>
* </code>
* <output>
* row with label and value wrapped by TS plugin.tx_rsysproductbase.Detail.label_wrap
* </output>
*
* @license www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
* @api
*/
class Tx_Rsysproductbase_ViewHelpers_Detail_ShowRowViewHelper extends Tx_Fluid_ViewHelpers_Format_AbstractEncodingViewHelper implements t3lib_Singleton {
/**
* shows a detail row wrapped with TS settings
*
* @param string $value string show
* @param string $label string for label
* @param string $type string for data type
* @param string $unit string for unit
* @return string the altered string
* @author Erwin Knoll <typo3coding@rootsystem.de>, Rootsystem
* @api
*/
public function render($value = NULL, $label=NULL, $type = 'string', $unit='') {
// init
$value_parsed = '';
// check if it is an object
if(is_object($value)) {
foreach ($value as $obj) {
$type = 'object';
}
}
if(!empty($value)) {
switch ($type) {
case 'currency':
$value_parsed = '€ ' . number_format($value,2,',','.');
break;
case 'float':
$value_parsed = number_format($value,2,',','.');
break;
case 'object':
foreach ($value as $obj) {
$value_parsed .= $obj->getTitle();
}
break;
default:
if(is_string($value)) {
$value_parsed = $value;
} else {
$value_parsed = '';
}
}
if(!empty($value_parsed)) {
// wrap
$settings = $this->templateVariableContainer->get('settings');
$wrap = $settings['Detail']['label_wrap'];
if($label && (strpos($label,'tx_rsysproductbase') === 0)) {
$label = Tx_Extbase_Utility_Localization::translate($label, 'Rsysproductbase');
}
$value_parsed .= ' ' . $unit;
$ret = preg_replace ('/\|/',$label, $wrap, 1);
$ret = preg_replace ('/\|/',$value_parsed, $ret, 1);
} else {
$ret = '';
}
} else {
$ret = '';
}
return $ret;
}
}
?>
Konfiguration des Html wraps mit TS:
plugin.tx_rsysproductbase {
Detail {
label_wrap (
<tr>
<td class="product_property_label">
|
</td>
<td class="product_property_value">
|
</td>
</tr>
)
}
Im Fluid Template:
<rsys:Detail.ShowRow value="{product.title}" label="tx_rsysproductbase_domain_model_product.title"/>
Das ganze ist zwar nicht besonders intelligent, vereinfacht aber die Erstellung der Templates - weiterhin wird hier die ganze Ausgabe unterdrückt, wenn das Feld keinen Wert hat (Leerstring oder 0).