0
votes

With the vhs viewhelper "resource.record.fal" i select the image from the page ressources. This works very fine but i also want to inheritance the image as long as there is no other image uploaded. Is there a way to do that? The slide attribute is not available for this viewhelper. I know i can do all of this just with Typoscript but i want to find a solution based on Fluid.

Here is my code:

<v:resource.record.fal table="pages" field="media" uid="{data.uid}" as="resources" >
<f:for each="{resources}" as="resource">
    <v:resource.image identifier="{resource.id}" />
</f:for>

3

3 Answers

3
votes

Okay, here goes some Fluid free-styling inline syntax:

{v:page.rootLine()
    -> v:iterator.column(columnKey: 'media', indexKey: 'uid')
    -> v:iterator.filter(preserveKeys: 1)
    -> v:iterator.keys()
    -> v:iterator.last()
    -> f:variable(name: 'firstPageUidWithMedia')}

In steps:

  1. Extract the page root line
  2. Extract a sub-array of all media column values, use column uid as keys
  3. Filter this to remove any empty values but preserve the keys
  4. Extract a sub-array of only the keys
  5. Pick the last key, which is the real page UID we want
  6. Assign that to a variable

Then use the {firstPageUidWithMedia} instead of {data.uid}.

1
votes

I am working to upgrade Typo3 from 6.2 to 9.5 LTS, i founded that fluidcontent will be used anymore, now below code used for previewing image in Flux content.

I have replaced my old code:

<f:for each="{v:content.resources.fal(field: 'teaserImage')}" as="image">
    <img src="{f:uri.image(src:'{image.id}',maxWidth:'64',treatIdAsReference:'1')}" alt="{image.alternative}"/>
</f:for>

To Below New code:

 <v:content.resources.fal field="teaserImage" as="images" record="{record}">
        <f:for each="{images}" as="image">
            <f:if condition="{image}">
                <f:image src="{image.id}" treatIdAsReference="1" maxWidth="100"/>
            </f:if>
        </f:for>
 </v:content.resources.fal>
0
votes

I developed this VH which do what you need :

namespace Your\Vendor\ViewHelpers;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use TYPO3\CMS\Frontend\Page\PageRepository;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
 * View helper to get the first image of rootline.
 */
class RootlineFirstImageViewHelper extends AbstractViewHelper
{
    use CompileWithRenderStatic;

    /**
     * {@inheritdoc}
     */
    public function initializeArguments()
    {
        $this->registerArgument('pid', 'int', '', true);
    }

    /**
     * {@inheritdoc}
     */
    public static function renderStatic(
        array $arguments,
        \Closure $renderChildrenClosure,
        RenderingContextInterface $renderingContext
    ): ?File {
        $fileRepository = GeneralUtility::makeInstance(FileRepository::class);
        $pages = GeneralUtility::makeInstance(PageRepository::class)->getRootLine($arguments['pid']);
        $files = [];
        foreach ($pages as $page) {
            /** @var FileReference[] $files */
            $files = $fileRepository->findByRelation('pages', 'media', $page['uid']);
            if (!empty($files)) {
                break;
            }
        }

        // It would be nice to have an extra argument to get a random image of the array.
        return !empty($files) ? $files[0]->getOriginalFile() : null;
    }
}

Then you can call it like this in your Fluid template :

<f:variable name="rootlineFirstImage"><whatever:rootlineFirstImage pid="{data.uid}"/></f:variable>
<f:if condition="{rootlineFirstImage}">
    <f:image image="{rootlineFirstImage}" width="1280"/>
</f:if>