You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3自定义调度任务获取页面内容时出现filterAccessiblePageIds()错误求助

解决TYPO3调度任务中调用lib.dynamicContent出现的filterAccessiblePageIds()错误

错误原因

这个错误源于调度任务运行在CLI命令行环境,缺少前端TypoScript Frontend(TSFE)上下文,而filterAccessiblePageIds()是TSFE关联的权限检查方法;同时你的lib.dynamicContent TypoScript定义在非前端环境下可能存在类型处理问题。

解决方案

1. 初始化TSFE上下文(推荐)

在调度任务的PHP代码中手动初始化TSFE,确保权限检查和内容渲染流程正常执行:

首先在SendNewslatters.php类中注入依赖:

use TYPO3\CMS\Core\Site\SiteFinder;
use Psr\Container\ContainerInterface;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class SendNewslatters extends AbstractTask
{
    protected $siteFinder;
    protected $container;

    public function __construct(SiteFinder $siteFinder, ContainerInterface $container)
    {
        $this->siteFinder = $siteFinder;
        $this->container = $container;
        parent::__construct();
    }

    // 任务执行方法
    public function execute()
    {
        $this->initTask();
        $pageId = (int)$this->pageId;

        // 初始化TSFE上下文
        $site = $this->siteFinder->getSiteByPageId($pageId);
        $language = $site->getDefaultLanguage();
        $request = Bootstrap::createRequestFromEnvironment();
        $request = $request->withAttribute('site', $site)
                          ->withAttribute('language', $language)
                          ->withQueryParams(['id' => $pageId]);

        $tsfe = GeneralUtility::makeInstance(
            TypoScriptFrontendController::class,
            $this->siteFinder,
            $language,
            $site->getRootPageId(),
            $pageId,
            false
        );
        $tsfe->fe_user = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
        $tsfe->fe_user->initializeUserSession($request);
        $tsfe->initialize($request);

        // 将TSFE存入全局变量,确保ContentObjectRenderer能访问
        $GLOBALS['TSFE'] = $tsfe;

        // 后续渲染代码保持不变
        $renderer = $this->rendererUtility->getFluidRendererForTemplate(
            'typo3conf/ext/newslatters/Resources/Private/Templates/Subscribers/Mail/NewslaterMail.html',
            'html'
        );
        $renderer->assign('pageId', $pageId);
        $message = $renderer->render();
        
        // ... 其他业务逻辑
        return true;
    }
}

2. 检查并修正lib.dynamicContent的TypoScript定义

确保TS定义正确处理pageUid参数,标准配置示例:

lib.dynamicContent = CONTENT
lib.dynamicContent {
    table = tt_content
    select {
        orderBy = sorting
        where = colPos=###colPos###
        pidInList = ###pageUid###
        languageField = sys_language_uid
        // 调度任务若需忽略前端权限,可添加以下配置
        enableFields = deleted=0
        disableGroupCheck = 1
    }
}

3. 直接在PHP中获取内容元素(替代方案)

如果不想依赖TSFE,可以直接在PHP中查询内容元素,再传递给Fluid渲染:

在SendNewslatters.php中注入ContentRepository:

use TYPO3\CMS\Frontend\Repository\ContentRepository;

class SendNewslatters extends AbstractTask
{
    protected $contentRepository;

    public function __construct(ContentRepository $contentRepository)
    {
        $this->contentRepository = $contentRepository;
        parent::__construct();
    }

    public function execute()
    {
        $this->initTask();
        $pageId = (int)$this->pageId;

        // 获取指定页面和列的内容元素
        $contentElements = $this->contentRepository->findByPageAndColPos($pageId, 0);

        $renderer = $this->rendererUtility->getFluidRendererForTemplate(
            'typo3conf/ext/newslatters/Resources/Private/Templates/Subscribers/Mail/NewslaterMail.html',
            'html'
        );
        $renderer->assign('contentElements', $contentElements);
        $message = $renderer->render();
        
        // ... 其他业务逻辑
        return true;
    }
}

修改Fluid模板NewslaterMail.html:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">
    <f:for each="{contentElements}" as="content">
        <f:cObject typoscriptObjectPath="tt_content.{content.CType}" data="{content}" />
    </f:for>
</html>

内容的提问来源于stack exchange,提问作者Pravin Vavadiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:46:26