Last active
September 26, 2017 19:22
-
-
Save benjaminrau/4674180 to your computer and use it in GitHub Desktop.
An ViewHelper for extbase/fluid equivalent to php´s in_array() function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*************************************************************** | |
* Copyright notice | |
* | |
* (c) 2012 Benjamin Rau <rau@codearts.at>, codearts | |
* | |
* All rights reserved | |
* | |
* This script is part of the TYPO3 project. The TYPO3 project is | |
* free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* The GNU General Public License can be found at | |
* http://www.gnu.org/copyleft/gpl.html. | |
* | |
* This script is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* This copyright notice MUST APPEAR in all copies of the script! | |
***************************************************************/ | |
/** | |
* | |
* | |
* @package hcbookings | |
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later | |
* | |
*/ | |
class Tx_Hcbookings_ViewHelpers_InArrayViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractConditionViewHelper { | |
public function initializeArguments() { | |
parent::initializeArguments(); | |
$this->registerArgument('haystack', 'mixed', 'View helper haystack ', TRUE); | |
$this->registerArgument('needle', 'string', 'View helper needle', TRUE); | |
} | |
/** | |
* Check if value is in array | |
* | |
* | |
* @param string $needle | |
* @param array $haystack | |
* | |
* @return boolean | |
* @api | |
*/ | |
public function render() { | |
$needle = $this->arguments['needle']; | |
$haystack = $this->arguments['haystack']; | |
if(is_string($haystack)) { | |
$haystack = strpos($haystack, ',') ? explode(',',$haystack) : $haystack; | |
if(!is_array($haystack)) return $this->renderElseChild(); | |
} | |
if(in_array($needle, $haystack)) { | |
return $this->renderThenChild(); | |
} else { | |
return $this->renderElseChild(); | |
} | |
} | |
} | |
?> | |
<!-- | |
For Example you could use it with multiple checkboxes to check the selected one: | |
<f:for each="{availabilityRange.possibleWeekdays}" key="key" as="weekday"> | |
<label class="checkbox inline"> | |
<f:form.checkbox name="item[availabilityRanges][{availabilityRange.uid}][weekday][]" id="inlineWeekday{key}" value="{weekday}" checked="{hcb:inArray(haystack:availabilityRange.weekday,needle:weekday,then:'checked',else:'')}" /> {weekday} | |
</label> | |
</f:for> | |
--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment