DownloadViewHelper

Eigener ViewHelper für variablen Linktext

Dieser ViewHelper zeigt einen Download Link an dessen Linktext mit einem Text aus einem anderen Feld ersetzt werden kann. Also z.B. statt "Download: project_file_x9873.pdf" "Download: Projektinformationen im PDF Format".

Im Feld download stehen in diesem Beispiel mehrere Dateien zum Download und im Feld downloadCaption steht pro Zeile eine Caption aus welcher der Text des Links gesetzt wird.

<rsys:Download uri="{project.download}" label="{project.downloadCaption}" prefix="uploads/tx_rsyssocialprojects/" setLabel="1" />

Die Konfiguration geschieht per Typoscript (siehe weiter unten).

Hier der ViewHelper:

<?php

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
*                                                                        *
* TYPO3 is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License version 2 as published by  *
* the Free Software Foundation.                                          *
*                                                                        *
* 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 General      *
* Public License for more details.                                       *
*                                                                        */


/**
 * A view helper for creating URIs to resources.
 *
 * = Examples =
 *
 * <code title="Defaults">
 * <link href="{f:uri.resource(path:'css/stylesheet.css')}" rel="stylesheet" />
 * </code>
 * <output>
 * <link href="Resources/Packages/MyPackage/stylesheet.css" rel="stylesheet" />
 * (depending on current package)
 * </output>
 *
 * @license www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
 */

class Tx_Rsyssocialprojects_ViewHelpers_DownloadViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {

    /**
     * @param string $uri the URI that will be put in the href attribute of the rendered link tag
     * @param string $label
     * @param string $prefix
     * @param bool $setLabel
     * @return string Rendered link
     */
    public function render($uri, $label='', $prefix, $setLabel=1) {

        $output = '';
        $uliList = explode(',',$uri);
        $labelList = explode("\n",$label);

        $settings = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_rsyssocialprojects.']['settings.']['download.'];
        $filelinkconf = $settings['filelink.'];


        if (!is_array($filelinkconf)){
            // default values
            $filelinkconf = array();
            $filelinkconf['jumpurl'] = 1;
            $filelinkconf['jumpurl.']['secure'] = 1;
            $filelinkconf['icon'] = 1;
            $filelinkconf['icon_link'] = 1;
            $filelinkconf['size'] = 1;
            $filelinkconf['size.']['wrap'] = ' |';
            $filelinkconf['size.']['bytes'] = 1;
            $filelinkconf['size.']['bytes.']['labels'] = ' B| KByte| MB| GB';
        }

        for ($i = 0; $i < sizeof($uliList); $i++) {
               
            if(!empty($labelList[$i]) && $setLabel) {
                //replace the link label with the param $label
                $filelinkconf['labelStdWrap.']['cObject'] = 'TEXT';
                $filelinkconf['labelStdWrap.']['cObject.']['value'] = ' '.$labelList[$i];
            } else  {
                $filelinkconf['labelStdWrap.']['cObject'] = 'TEXT';
                $filelinkconf['labelStdWrap.']['cObject.']['value'] = ' '.$uliList[$i];
                $output .=  str_replace('|', $labelList[$i], $settings['label.']['wrap']);
            }
               
            // render link
            $output .= $GLOBALS['TSFE']->cObj->filelink($prefix . $uliList[$i], $filelinkconf);
            $output = str_replace('|', $output, $settings['itemwrap']);
        }

       
        $output = str_replace('|', $output, $settings['wrap']);
       
        // return
        return $output;
    }

}
?>

 Das zugehörige Typoscript:

plugin.tx_rsyssocialprojects {
    settings {
        # download
        download {
            wrap = <table class="download">|</table>
            itemwrap = <tr>|</tr>
            label.wrap = <td>|</td><td>
            filelink {               
                    wrap = |</td>
                    jumpurl = 1
                    jumpurl.secure = 1
                    icon = 0
                    icon_link = 1
                    size = 1
                    size.wrap = <td>|</td>
                    size.bytes = 1
                    size.bytes.labels =  B| KB| MB| GB
            }
        }
    }
}

Erstellt: 09/2012| Geändert: 10/2015