TYPO3 CMS  TYPO3_6-2
SwitchViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
40 
45  private $childNodes = array();
46 
50  protected $backupSwitchExpression = NULL;
51 
55  protected $backupBreakState = FALSE;
56 
63  public function setChildNodes(array $childNodes) {
64  $this->childNodes = $childNodes;
65  }
66 
72  public function render($expression) {
73  $content = '';
74  $this->backupSwitchState();
75  $templateVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
76 
77  $templateVariableContainer->addOrUpdate('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression', $expression);
78  $templateVariableContainer->addOrUpdate('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', FALSE);
79 
80  foreach ($this->childNodes as $childNode) {
81  if (
82  !$childNode instanceof \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode
83  || $childNode->getViewHelperClassName() !== 'TYPO3\CMS\Fluid\ViewHelpers\CaseViewHelper'
84  ) {
85  continue;
86  }
87  $content = $childNode->evaluate($this->renderingContext);
88  if ($templateVariableContainer->get('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break') === TRUE) {
89  break;
90  }
91  }
92 
93  $templateVariableContainer->remove('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression');
94  $templateVariableContainer->remove('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break');
95 
96  $this->restoreSwitchState();
97  return $content;
98  }
99 
105  protected function backupSwitchState() {
106  if ($this->renderingContext->getViewHelperVariableContainer()->exists('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')) {
107  $this->backupSwitchExpression = $this->renderingContext->getViewHelperVariableContainer()->get('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression');
108  }
109  if ($this->renderingContext->getViewHelperVariableContainer()->exists('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break')) {
110  $this->backupBreakState = $this->renderingContext->getViewHelperVariableContainer()->get('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break');
111  }
112  }
113 
119  protected function restoreSwitchState() {
120  if ($this->backupSwitchExpression !== NULL) {
121  $this->renderingContext->getViewHelperVariableContainer()->addOrUpdate(
122  'TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper',
123  'switchExpression',
124  $this->backupSwitchExpression
125  );
126  }
127  if ($this->backupBreakState !== FALSE) {
128  $this->renderingContext->getViewHelperVariableContainer()->addOrUpdate('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', TRUE);
129  }
130  }
131 }