1

I have made an own viewhelper for getting external images (thanks to robert pflamm see Typo3 fluid image from external resource). if i use only ImageViewHelper everything is working finde.

But if i use \TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper the image is only rendered once. Same problem as here TYPO3 ver. 7.6.2 - Condition ViewHelpers evaluated only once. but i do not know how to solve it

Can anyone please help? martin



    namespace Mwxxx\Mwxxx\ViewHelpers;

    use TYPO3\CMS\Core\Utility\GeneralUtility;
    use TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper;
    use TYPO3\CMS\Core\Resource\FileInterface;
    use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;

    class ExternalImageViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper
  {

    const UPLOAD_DIRECTORY = 'upload';
    const TEMP_PREFIX = 'Mwxx';

    /**
    * ResourceFactory
    *
    * @var \TYPO3\CMS\Core\Resource\ResourceFactory
    * @inject
    */
    protected $resourceFactory = null;

    /**
    * Resizes a given image (if required) and renders the respective img tag
    *
     * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/
    *
    * @param string                           $src                a path to a file, a combined FAL identifier or an uid (integer). If $treatIdAsReference is set, the integer is considered the uid of the sys_file_reference record. If you already got a FAL object, consider using the $image parameter instead
    * @param string                           $width              width of the image. This can be a numeric value representing the fixed width of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.
    * @param string                           $height             height of the image. This can be a numeric value representing the fixed height of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.
    * @param integer                          $minWidth           minimum width of the image
    * @param integer                          $minHeight          minimum height of the image
    * @param integer                          $maxWidth           maximum width of the image
    * @param integer                          $maxHeight          maximum height of the image
    * @param boolean                          $treatIdAsReference given src argument is a sys_file_reference record
    * @param FileInterface|AbstractFileFolder $image              a FAL object
    *
    * @return string
    * @throws \Exception
   * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
    * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException
    * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
    */
    public function render($src = null, $image = null, $width = null, $height = null, $minWidth = null, $minHeight = null, $maxWidth = null, $maxHeight = null, $treatIdAsReference = false)
    {
    if (filter_var($src, FILTER_VALIDATE_URL)) {
      $storage = $this->resourceFactory->getDefaultStorage();
      if (!$storage->hasFolder(self::UPLOAD_DIRECTORY)) {
        $storage->createFolder(self::UPLOAD_DIRECTORY);
      }
      if (file_exists('fileadmin/upload/'.basename(basename($src)))) {
          #echo "Die Datei $filename existiert";
           $src         = 'fileadmin/upload/'.basename(basename($src));
      }
      else {
          $externalFile = GeneralUtility::getUrl($src);
          if ($externalFile) {
            $tempFileName = tempnam(sys_get_temp_dir(), self::TEMP_PREFIX);
            $handle       = fopen($tempFileName, "w");
            fwrite($handle, $externalFile);
            fclose($handle);

            $uploadFolder = $storage->getFolder(self::UPLOAD_DIRECTORY);
                #echo "Die Datei $filename existiert nicht";
                $file         = $uploadFolder->addFile($tempFileName, basename(basename($src)), 'changeName');
                $src          = $file->getPublicUrl();
                #unlink($tempFileName);

          } else {
            throw new \Exception(sprintf('External URL % cannot accessed.', $src), 1473233519);
          }
      }

    }
    return parent::render($src, $image, $width, $height, $minWidth, $minHeight, $maxWidth, $maxHeight, $treatIdAsReference);
    }


    }



Community
  • 1
  • 1
matin
  • 303
  • 2
  • 13

1 Answers1

1

The viewhelpers are not really a public API which are meant to be extensible by custom ViewHelpers!

The problem you are facing is that after the 1st call, the ViewHelpers are compiled and the method renderStatic is used which you don't override.

The best solution would be that you copy the code of the parent class and only extend from AbstractViewHelper or AbstractTagBasedViewHelper

Georg Ringer
  • 7,779
  • 1
  • 16
  • 34
  • thanks. where do i have to copy the code? to same viewhelpver-class-file? – matin Sep 07 '16 at 14:30
  • Yes, to your viewhelper. Please mark the answer then as solution if it works for you – Georg Ringer Sep 07 '16 at 16:14
  • The *right* solution is to only subclass the abstract classes shipped with Fluid, exactly as Georg says. I add this comment to encourage you to pursue that method instead of copying the code, which it seems you are intending to do. – Claus Due Sep 07 '16 at 20:55