如何在TypoScript中创建条件以将不同的JavaScript文件加载到不同的后端布局? (TYPO3) [英] How can I make a Condition in TypoScript for loading different JavaScript-Files to different Backend-Layouts? (TYPO3)

查看:74
本文介绍了如何在TypoScript中创建条件以将不同的JavaScript文件加载到不同的后端布局? (TYPO3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与Google进行漫长的旅程之后,进行了多次搜索和尝试:

After a "long way" with Google, Searching and many tries:

我为当前页面创建了一个lib.variable:

I created a lib.variable for the current Page:

lib.currentPage = TEXT
lib.currentPage.data = page:uid

如果我在前端的FluidTemplate中使用以下命令调试它:

If I debug it in my FluidTemplate in the Frontend with:

Testing currentPage: <f:cObject typoscriptObjectPath="lib.currentPage" />

我得到了正确的值。

现在我想在 pageSetup.ts 的条件下使用该变量,如下所示:

Now I want to use that Variable in a Condition in my pageSetup.ts like follows:

[DB:pages:lib.currentPage:backend_layout = pagets__pagelayout_logoclaim_subpage]
    page.includeJSFooter.belayoutlogoclaim = EXT:rm_base/Resources/Public/JS/be_logoclaim.js
[end]

我在其他一些条件下进行了测试,但没有一个像

I testet this with some other Conditions, but nothing works like expected.

经测试的条件:


  • [page | backend_layout = pagelayout_logoclaim_subpage]

  • [globalVar = TSFE:page | backend_layout = pagelayout_logoclaim_subpage]

我还测试了 TypoScript对象浏览器,在这里看起来不错:

I also tested the Condition in the TypoScript Object Browser, and here it looks like good working:

TypoScript对象浏览器-如果我激活了条件

TypoScript Object Browser - If I activate the Condition

SourceCode

SourceCode in the Frontend on a Site with the correct PageLayout

我需要这个,因为我有两个不同的菜单,并且它们需要不同的JavaScript,以避免前端出现错误行为。

I need this, because I have two different Menus, and they need different JavaScripts, to avoid wrong behaviour in the Frontend.

更新
我插入了如下的pageLayouts:

Update: I inserted the pageLayouts like this:

page = PAGE
page {

    10 = FLUIDTEMPLATE
    10 {
        partialRootPath = EXT:rm_base/Resources/Private/Templates/Fluid/Partials/
        layoutRootPath = EXT:rm_base/Resources/Private/Templates/Fluid/Layouts/

        file.stdWrap.cObject = CASE
        file.stdWrap.cObject {
            key.data = pagelayout

            // Default-Template is LogoFull_Subpage (No Navigation Dependence)
            default = TEXT
            default.value = EXT:rm_base/Resources/Private/Templates/Fluid/LogoFull_Subpage.html

            // LogoClaim - Subpage
            pagets__pagelayout_logoclaim_subpage = TEXT
            pagets__pagelayout_logoclaim_subpage.value = EXT:rm_base/Resources/Private/Templates/Fluid/LogoClaim_Subpage.html
        }
    }

您会看到:后端布局位于我的文件中扩展,不在数据库表中:backend_layouts。

You see: The Backendlayouts are in Files of my Extension, not in the Database-Table: backend_layouts.

更新2

如果有人知道如何使用TypoScript-Way,最好使用外部BE-Layouts。谢谢。

I would prefer a TypoScript-Way, if someone knows how - with external BE-Layouts. Thank you.

推荐答案

我找到了另一个使用TypoScript自定义条件的解决方案:

I found another solution, that uses a TypoScript Custom Condition:

首先,我在这里的扩展程序中创建一个BackendlayoutCondition.php:

First I create a BackendlayoutCondition.php in my Extension here:

/Classes/TypoScript/BackendlayoutCondition.php

其内容是此(更多详细信息请参见评论):

Its content is this (See Comments for more Detail):

<?php
namespace RM\RmBase\TypoScript;

use \TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractCondition;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;;

class BackendlayoutCondition extends AbstractCondition
{
    /**
     * Evaluate condition
     *
     * @param array $conditionParameters
     * @return bool
     */
    public function matchCondition(array $conditionParameters)
    {
        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($conditionParameters,'cond Params');

        # Return false if in Backend (Condition for Frontend only)
        if (!$GLOBALS['TSFE']) return false;

        # QueryBuilder for Table: pages
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');

        # Check if current BackendLayout is inside the $conditionParameters
        if (!empty($conditionParameters) && substr($conditionParameters[0], 0, 1) === '=') {
            # Trim the Parameter to get the correct Value of the Parameter (behind '=')
            $conditionParameters[0] = trim(substr($conditionParameters[0], 1));

                # Get Backendlayout on this Page
                $backendLayoutOnThisPage = $queryBuilder
                    ->select('backend_layout')
                    ->from('pages')
                    ->where(
                        $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter(intval($GLOBALS['TSFE']->id), \PDO::PARAM_INT))
                    )
                    ->execute();

            # Store Backendlayout Value of the current Page in $backendLayoutOnThisPage
            $backendLayoutOnThisPage = $backendLayoutOnThisPage->fetch()['backend_layout'];
        } else {
            # If no ConditionParameter was set return false
            return false;
        }

        # Check if parent BackendLayout_NextLevel is inside the $conditionParameters
        if ($backendLayoutOnThisPage == '') {
            # Get pageRepository
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
            $pageRepository = $objectManager->get('TYPO3\\CMS\\Frontend\\Page\\PageRepository');

            # Get Rootline of the current Page
            $pageRootline = $pageRepository->getRootLine(intval($GLOBALS['TSFE']->id));

            # Set rootlineIndex to the Parent Page Index
            $rootlineIndex = count($pageRootline)-2;

            # Check Parent Page Backendlayout_NextLevel till 0 or Backendlayout found
            while ($rootlineIndex > 0) {
                if ($pageRootline[$rootlineIndex]['backend_layout_next_level'] == $conditionParameters[0]) {
                    return true;
                } else {
                    $rootlineIndex--;
                }
            }

            # No BackendLayout_NextLevel found till 0
            return false;
        } else {
            # If Condition Backendlayout found return true, otherwise return false
            if ($backendLayoutOnThisPage == $conditionParameters[0]) {
                return true;
            } else {
                return false;
            }
        }
    }
}

已编辑2

我只需要在 pageSetup.ts

[RM\RmBase\TypoScript\BackendlayoutCondition = pagelayout_logoclaim_subpage]
    page.includeJSFooter.belayoutlogoclaim = EXT:rm_base/Resources/Public/JS/be_logoclaim.js
[global]

现在,我在前端有此源代码:

Now I have this Sourcecode in the Frontend:

<script src="/typo3conf/ext/rm_base/Resources/Public/JS/be_logoclaim.js?1523005944" type="text/javascript"></script>

不是:

<script src="/typo3conf/ext/rm_base/Resources/Public/JS/be_logoclaim.js" type="text/javascript"></script>

如果我使用FooterAssets方法:

if I use the FooterAssets method:

<f:section name="FooterAssets">
    <script src="/typo3conf/ext/rm_base/Resources/Public/JS/be_logoclaim.js" type="text/javascript"></script>
</f:section>






编辑:我在我的答案中发现一些错误,我将其修复并编辑我的答案。


Edit: I found some errors in my Answer, i fix it and edit my answer then.

Edit2 :现在,条件检查了Backendlayouts和Backendlayouts_Nextlevel字段让Backendlayouts获得所有可能的BE布局,包括继承的。

Edit2: Now the Condition checks the Backendlayouts and the Backendlayouts_Nextlevel Fields for Backendlayouts to get all possible BE-Layouts including the inherited.

这篇关于如何在TypoScript中创建条件以将不同的JavaScript文件加载到不同的后端布局? (TYPO3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆