Advertisement
Guest User

Fluid RenderExternal ViewHelper

a guest
Mar 21st, 2013
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.85 KB | None | 0 0
  1. <?php
  2. /***************************************************************
  3.  *  Copyright notice
  4.  *
  5.  *  (c) 2011 Christian Kuhn <lolli@schwarzbu.ch>
  6.  *
  7.  *  All rights reserved
  8.  *
  9.  *  This script is part of the TYPO3 project. The TYPO3 project is
  10.  *  free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 3 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  The GNU General Public License can be found at
  16.  *  http://www.gnu.org/copyleft/gpl.html.
  17.  *
  18.  *  This script is distributed in the hope that it will be useful,
  19.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *  GNU General Public License for more details.
  22.  *
  23.  *  This copyright notice MUST APPEAR in all copies of the script!
  24.  ***************************************************************/
  25.  
  26.  
  27. /**
  28.  * Render a partial from another extension in own namespace.
  29.  *
  30.  * <fx:renderExternal
  31.  *      partial="Device/ProductImage"
  32.  *      extensionName="EnetOtherExtension"
  33.  *      arguments="{
  34.  *          product: entry.device.product,
  35.  *          clearing: entry.device.clearing,
  36.  *          maxWidth: 30,
  37.  *          maxHeight: 30
  38.  *      }"
  39.  *  />
  40.  *
  41.  * @author Christian Kuhn <lolli@schwarzbu.ch>
  42.  * @TODO: Implement sections
  43.  */
  44. class Tx_EnetFxLibrary_ViewHelpers_RenderExternalViewHelper extends Tx_Fluid_ViewHelpers_RenderViewHelper {
  45.  
  46.     /**
  47.      * @var Tx_Extbase_Object_ObjectManagerInterface
  48.      */
  49.     protected $objectManager;
  50.  
  51.     /**
  52.      * Injects the object manager
  53.      *
  54.      * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
  55.      * @return void
  56.      */
  57.     public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
  58.         $this->objectManager = $objectManager;
  59.     }
  60.  
  61.     /**
  62.      * Renders the content.
  63.      *
  64.  
  65.      * @param string $extensionName Render partial of this extension
  66.      * @param string $partial The partial to render
  67.      * @param array $arguments Arguments to pass to the partial
  68.      * @return string
  69.      */
  70.     public function render($extensionName, $partial = NULL, array $arguments = array()) {
  71.             // Overload arguments with own extension local settings (to pass own settings to external partial)
  72.         $arguments = $this->loadSettingsIntoArguments($arguments);
  73.  
  74.             /** @var $request Tx_Extbase_MVC_Request */
  75.         $request = clone $this->controllerContext->getRequest();
  76.         $request->setControllerExtensionName($extensionName);
  77.         $controllerContext = clone $this->controllerContext;
  78.         $controllerContext->setRequest($request);
  79.  
  80.         $this->setPartialRootPath($controllerContext);
  81.         $content = $this->viewHelperVariableContainer->getView()->renderPartial($partial, NULL, $arguments);
  82.         $this->resetPartialRootPath();
  83.  
  84.         return $content;
  85.     }
  86.  
  87.     /**
  88.      * Set partial root path by controller context
  89.      *
  90.      * @param Tx_Extbase_MVC_Controller_ControllerContext $controllerContext
  91.      * @return void
  92.      */
  93.     protected function setPartialRootPath(Tx_Extbase_MVC_Controller_ControllerContext $controllerContext) {
  94.         $this->viewHelperVariableContainer->getView()->setPartialRootPath(
  95.             $this->getPartialRootPath($controllerContext)
  96.         );
  97.     }
  98.  
  99.     /**
  100.      * Resets the partial root path to original controller context
  101.      *
  102.      * @return void
  103.      */
  104.     protected function resetPartialRootPath() {
  105.         $this->setPartialRootPath($this->controllerContext);
  106.     }
  107.  
  108.     /**
  109.      * @param Tx_Extbase_MVC_Controller_ControllerContext $controllerContext
  110.      * @return mixed
  111.      */
  112.     protected function getPartialRootPath(Tx_Extbase_MVC_Controller_ControllerContext $controllerContext) {
  113.         $partialRootPath = str_replace(
  114.             '@packageResourcesPath',
  115.             t3lib_extMgm::extPath($controllerContext->getRequest()->getControllerExtensionKey()) . 'Resources/',
  116.             '@packageResourcesPath/Private/Partials'
  117.         );
  118.         return $partialRootPath;
  119.     }
  120. }
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement