VariantView for TYPO3 8.7

时间:2017-06-15 13:06:17

标签: php typo3

模板变体有一个解决方案,它允许为extbase控制器使用的流体模板文件设置后缀。它由Peter Niederlag创建,并由Bastian Waidelich进行了改进。

解决方案在TYPO3 8.7中不再有效,因为代码已经重构,expandGenericPathPattern中的方法TemplateView不再存在。

我应该如何在TYPO3 8.7中实现这种变体视图?

2 个答案:

答案 0 :(得分:1)

$this->view->getRenderingContext()->setControllerAction('MyAction.Variant');应该做的(从任何initializeAction方法或在action方法中)。请注意,与您链接的视图覆盖类相反,此方法意味着必须在路径中包含原始操作模板。

答案 1 :(得分:0)

我在扩展程序中创建了以下类,它们为TYPO3 8.7实现了VariantView。

类\视图\ VariantView.php

<?php

namespace Vendor\Extkey\View;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\TemplateView;

/**
 * Extended Fluid Template View that supports different "variants"
 *
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
 */
class VariantView extends TemplateView
{
    /**
     * @param string $layoutVariant
     * @return void
     */
    public function setLayoutVariant($layoutVariant)
    {
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
        $this->baseRenderingContext->setTemplatePaths($objectManager->get(TemplatePaths::class));
        /** @var TemplatePaths $templatePaths */
        $templatePaths = $this->baseRenderingContext->getTemplatePaths();
        $templatePaths->setLayoutVariant($layoutVariant);
    }

    const DEFAULT_LAYOUT_VARIANT = '.default';
}

类\视图\ TemplatePaths.php

<?php

namespace Vendor\Extkey\View;

class TemplatePaths extends \TYPO3\CMS\Fluid\View\TemplatePaths
{
    /**
     * Layout variant to use for this view.
     *
     * @var string
     */
    protected $layoutVariant = VariantView::DEFAULT_LAYOUT_VARIANT;

    /**
     * @param string $layoutVariant
     * @return void
     */
    public function setLayoutVariant($layoutVariant)
    {
        $this->layoutVariant = $layoutVariant;
    }

    /**
     * Wrapper for parent class method which adds layout variant in action parameter
     *
     * @param string $controller
     * @param string $action
     * @param string $format
     * @return string|NULL
     * @api
     */
    public function resolveTemplateFileForControllerAndActionAndFormat($controller, $action, $format = self::DEFAULT_FORMAT)
    {
        $action = $action . $this->layoutVariant;
        return parent::resolveTemplateFileForControllerAndActionAndFormat($controller, $action, $format = self::DEFAULT_FORMAT);
    }
}

在您的控制器中添加以下行:

 protected function setViewConfiguration(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view) {
   parent::setViewConfiguration($view);
   $view->setLayoutVariant($this->settings['layoutVariant']);
 }