Viewhelper: File exsits.
Anwendnung
Angabe des Namesspaces im Fluid Template:
{namespace fhs=Fischhase\FhViewhelpers\ViewHelpers}
Dann kann derViewhelper aufgerufen werden:
{fhs:fileexists(path:path, file:filename)}
path (String): absoluter Pfad, in dem die Daten liegt (fileadmin/theme/light/images/)
file (String): Dateiname, der geprüft werden soll.
return: Gibt FALSE aus, wenn die Datei nicht vorhanden ist oder den geprüften Pfad+Dateinamen zur Weiterverwendung
Der Pfad kann Schrägstriche am Anfang enthalten oder nicht. Der Viewhelper rückt es gerade und gibt den korrekten Pfad zurück.
Beispiel:
<f:variable name="file" value="icon_{category.description}.svg" />
<f:variable name="path" value="/fileadmin/theme/light/images/" />
<f:variable name="verifiedPath"><fhs:fileexists file="{file}" path="{path}"/></f:variable>
<f:if condition="{verifiedPath}">
<f:then>
<img src="{verifiedPath}" alt="{category.title}" width="360c" height="360c" />
</f:then>
<f:else>
<div style="color:red; border:1px solid red; padding:0.5rem"> kein Icon zu Kategorie {category.uid} gefunden. Dateinamensegment muss in der Beschreibung stehen.<br />
</f:else>
</f:if>
<?php
namespace Fischhase\FhViewhelpers\ViewHelpers;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
/*
* {namespace fhs=Fischhase\FhViewhelpers\ViewHelpers}
*
* {fhs:fileexists(path:path, file:filename)}
* the viewhelpers checks, if the path and filenames have the correct slashes. It returns the checked path/filename as string or FALSE, if the file does not exist.
*/
class FileexistsViewHelper extends AbstractViewHelper {
/**
* @params string $path (path)
* @params string $file (filename)
* @return bool/string (FALSE or path/filename)
*/
public function initializeArguments() {
$this->registerArgument('path', 'string', 'path of file', TRUE);
$this->registerArgument('file', 'string', 'name of file', TRUE);
}
public function render() {
$path = $this->arguments['path'];
$file = $this->arguments['file'];
$return = FALSE;
/* checks slashes and adds missing*/
if ($path) {
if (mb_substr($path, 0, 1) != '/') {
$path = '/' . $path;
}
if (mb_substr($path, -1) != '/') {
$path = $path . '/';
}
};
if ($file) {
/* checks slashes and removes excess slashes */
if (mb_substr($file, 0, 1) == '/') {
$file = mb_substr($file, 1);
}
}
/* get web root path */
$root = Environment::getPublicPath();
/*assemble full path */
$fullpath = $root . $path . $file;
if (file_exists($fullpath)) {
/*returns path from web root */
$return = $path . $file;
}
/*retunrs false if file does not exist */
return $return;
}
}