TYPO3 FLUID STYLED CONTENT with Lazy Load Javascript Plugin - typo3

Unfortunately I'm still using TYPO3 7.6, but it's the same in principle like the latest TYPO3 CMS, so I also use Fluid_styled_content .. uff!
I've included the lazy load javascript plugin into my TYPO3-Website and now I try to change the FLUID-partial typo3conf/ext/myext/Resources/Private/Extensions/Fsc/Partials/MediaGallery.html - but I've a probem with this .. because my changing in line 'file' throws an error. I try it with or without " or \" ..
<f:section name="media">
<f:media
file="{f:uri.image(src: \"EXT:/myext/Resources/Public/img/icons/blank.gif\", treatIdAsReference:1}"
class="lazyload"
data="{src: '{f:uri.image(image: column.media)}'}"
width="{column.dimensions.width}"
height="{column.dimensions.height}"
alt="{column.media.alternative}"
title="{column.media.title}"
/>
</f:section>
I've changed file="{column.media}" , because I want to load my 'blank.gif' instead of the image himself.
Argument 1 passed to TYPO3\CMS\Core\Resource\Rendering\RendererRegistry::getRenderer() must implement interface TYPO3\CMS\Core\Resource\FileInterface, string given, called in C:\www\typo3_src-7.6.31\typo3\sysext\fluid\Classes\ViewHelpers\MediaViewHelper.php on line 90
The output is like this:
<img data-src="/fileadmin/user_upload/admins.jpg" class="lazyload" src="/fileadmin/user_upload/admins.jpg" width="280" height="176" alt="">
Lazy load is working perfect, but I have to load the blank.gif.
Is there a solution to manipulate the FSC-partials or do I need a new ViewHelper, like this?

Related

Display full name of logged in TYPO3 frontend user in Fluid

The task is pretty simple: I want to display the full name (first- and lastname) of the current TYPO3 frontend user in Fluid. But somehow, TYPO3 (version 9.5) or Fluid seems to cache data, so a logged in frontend user sometimes sees the name of other another logged user.
Current implementation:
TypoScript:
lib.username = USER_INT
lib.username.userFunc = Vendor\Extension\UserFunc\Username->getUsername
This is a USER_INT, so the output should always be uncached.
Fluid Layout - Default.html:
<f:render partial="Header" section="Top" />
Fluid Partial - Header.html:
<f:section name="Top">
<img src="logo.png">
<f:render partial="Navigation" />
</f:section>
Fluid Partial - Navigation.html
<f:security.ifAuthenticated>
<f:then>
<p>Logged in as: <f:cObject typoscriptObjectPath="lib.username" /></p>
</f:then>
<f:else>
<p>Not logged in</p>
<f:else>
</f:security.ifAuthenticated>
Why does the result of the cObject get cached? Should'nt this always be calculated per request, because lib.username is a USER_INT? I also tried to add a f:cache.disable ViewHelper to the template with no success.
In order to resolve the problem I refactored it to fetch the full name of the fe_user using a JavaScript XHR request to a simple PSR-15 middleware. Is this really the only suitable solution or am I missing something?
Update 17.12.2020
This all works fine. I just spotted a typo in my userFunc, which resulted in the unexpected behavior.
This happens because TYPO3 cache works with frontend user groups, not frontend users. So you will see results cached for the list of user's groups rather than the current user.
Use <v:render.uncache> ... </v:render.uncache> from EXT:vhs to render that part of code uncached. Alternatively you can modify TYPO3 caching to use the current user but this may decrease cache performance and seriously increase amount of cached items.
Beware, that there is a caching problem even with USER_INT and COA_Int see https://forge.typo3.org/issues/37988
You could use only TypoScript for that like:
lib.username = COA_INT
lib.username {
10 = TEXT
10.data = TSFE:fe_user|user|first_name
10.wrap = |
20 = TEXT
20.data = TSFE:fe_user|user|lastname_name
}
Why use TypoScript? It is very limited on what it can bring back. I just finished a tutorial where you can use a ViewHelper to add an ExtBase variable which assigns the whole user object in the FrontEnd and you can use it wherever you want with all it's relations, even the image, which is difficult to get via TypoScript.
TYPO3 tutorial
Best regards
share edit delete flag

fluid template: render image only if it exists

How can I use f:if to see if an image exists on the server before trying to render it?
I have tried the following without success:
<f:if condition="fileadmin/bilder/Header/{data.title}.jpg">
<f:then>
<f:variable name="imageUri" value="fileadmin/bilder/Header/{data.title}.jpg" />
</f:then>
<f:else>
<f:variable name="imageUri" value="fileadmin/bilder/Header/default.jpg" />
</f:else>
</f:if>
{data.title} works correkt. The Problem is that my condition is wrong. It never goes to the else. Probably my condition is interpret as a string und not as an source.
Description here doesn´t help because my Image is on the server.
Render image in fluid template if it exists?
First of all, why would you have a hardcoded path to a file in fileadmin in a template? That's very uncommon, most of the time you have some kind of relation through TYPO3 (e.g. through the media field in the pages table or something like that).
If this is still something you need, you'd have to write a FileExistsViewHelper that checks if the file exists before using it. Here's the documentation on how to write a custom view helper: https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/8-Fluid/8-developing-a-custom-viewhelper.html
You'd need to register your filename as an argument with registerArgument and then prepend the path to the TYPO3 installation (to make it an absolute path) and check for this file with file_exists.
Extend from \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper and implement the verdict method.
Your template above just creates a string (as you already guessed).

Override a single partial of TYPO3 fluid_styled_content

I want to add class="img-responsive" to the markup of each media element of text/media content elements.
I found a corresponding section in fluid_styled_content/Resources/Private/Partials/MediaGallery.html and added the class for test: Bingo.
<f:section name="media">
<f:media
file="{column.media}"
width="{column.dimensions.width}"
height="{column.dimensions.height}"
alt="{column.media.alternative}"
title="{column.media.title}"
additionalConfig="{settings.media.additionalConfig}"
class="img-responsive"
/>
</f:section>
I guess adding a custom partialRootPath with lib.fluidContent.partialRootPaths.200 and copying all the partials would work as documented.
However, I'd like to avoid copying 20 partials from fluid_styled_content just to change one line in one file.
Can I override a single partial of TYPO3 fluid_styled_content, or even better a single section of the partial?
Using TYPO3 7.6
Set lib.contentElement.partialRootPaths.200 = EXT:your_extension_key/Resources/Private/Partials/ in TypoScript as described in the question.
Copy a single partial file MediaGallery.html from fluid_styled_content to EXT:your_extension_key/Resources/Private/Partials/
Have enough breaks during work to avoid stackoverflow.

Is it possible to set by fluid a argument that the typo3 backend uses as input

In the typo3 template I'm using the following line to get the last 5 added items from the backend and this works.
<f:cObject typoscriptObjectPath="lib.lastaddeditmes" />
Only the number of item I want to have it flexible and set in the fluid template. So for example like below where the qty is set to three, is this possible?
<f:cObject typoscriptObjectPath="lib.lastaddeditmes(3)" />
You can't do that in the view, instead you can make a copy of lib.lastaddeditmes in TypoScript and then use it in other place like:
lib.lastaddeditmesOnlyThree < lib.lastaddeditmes
lib.lastaddeditmesOnlyThree.someSetting.items = 3
and in view:
<f:cObject typoscriptObjectPath="lib.lastaddeditmesOnlyThree" />
Remember that TypoScript is just a configuration syntax (not programming language) so it shouldn't give you any huge performance drop.

Output nested tags with one single ViewHelper

I have a TYPO3 project with an extension using Extbase and to implement lazy loading and retina images while maintaining compatibility if javascript is disabled, I need to generate some output like this:
<noscript data-src-small="small.jpg" data-src-large="large.jpg" data-width="300" data-height="200">
<img src="small.jpg" width="300" height="200">
</noscript>
So I wrote a custom ViewHelper (or rather I copied and modified an existing one) that does exactly this and it works, but I'm not sure I'm doing it right, because I'm manually creating the <img> like this:
$content = '<img class="'.$class.'" alt=" " src="'.$imageSource.'" width="'.$imageInfo[0].'" height="'.$imageInfo[1].'">';
$this->tag->setContent($content);
I wonder if there isn't a better way to do this, ideally I'd like to call the standard ImageViewHelper::render() function inside my custom ViewHelper.
Is there any way to do that? Should that even be done at all?
I am aware, I could adjust my template like this:
<x:noscript src="filename.jpg" width="300">
<f:image src="filename.jpg" width="300" />
</x:noscript>
(x:noscriptis supposed to be my custom ViewHelper)
and call renderChildren() inside my ViewHelper.
But then I would have to repeat src="filename.jpg" width="300" and I generally don't like to repeat input if there's a better way.
The easiest way to achieve what you want is to use a partial. It would look like this:
{namespace x=Yourcompany\Yourproduct\ViewHelpers}
<f:comment>
Parameters:
* src: src of the image
* width: Width of the image
</f:comment>
<x:noscript src="{src}" width="{width}">
<f:image src="{src}" width="{width}" />
</x:noscript>
You can use it like this:
<f:render
partial="Path/To/Partial"
arguments="{width: 300, src: 'filename.jpg'}
/>