簡體   English   中英

TYPO3 流體資源.record.fal

[英]TYPO3 Fluid resource.record.fal

使用 vhs 視圖助手“resource.record.fal”,我從頁面資源中選擇圖像。 這工作得很好,但我也想繼承圖像,只要沒有上傳其他圖像。 有沒有辦法做到這一點? 該視圖助手的幻燈片屬性不可用。 我知道我可以使用 Typoscript 完成所有這些,但我想找到一個基於 Fluid 的解決方案。

這是我的代碼:

<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>

好的,這里有一些 Fluid 自由樣式的內聯語法:

{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')}

分步驟:

  1. 提取頁面根行
  2. 提取所有media列值的子數組,使用列uid作為鍵
  3. 過濾它以刪除任何空值但保留鍵
  4. 僅提取鍵的子數組
  5. 選擇最后一個key,就是我們想要的真實頁面UID
  6. 將其分配給變量

然后使用{firstPageUidWithMedia}而不是{data.uid}

我正在努力將 Typo3 從 6.2 LTS 升級到 9.5 LTS,我發現將不再使用流體內容,現在下面用於在 Flux 內容中預覽圖像的代碼。

我已經替換了我的舊代碼:

<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>

到下面的新代碼:

 <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>

我開發了這個 VH,它可以滿足您的需求:

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;
    }
}

然后你可以在你的 Fluid 模板中這樣稱呼它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM