Created
March 4, 2015 11:16
-
-
Save tboock/783628f2014329a7bfde to your computer and use it in GitHub Desktop.
TYPO3 Flow DatefieldViewHelper
This file contains hidden or 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 | |
| namespace Vendor\MyPackage\ViewHelpers\Form; | |
| use TYPO3\Fluid\Core\ViewHelper\Exception; | |
| use TYPO3\Fluid\ViewHelpers\Form\TextfieldViewHelper; | |
| class DatefieldViewHelper extends TextfieldViewHelper { | |
| /** | |
| * @return void | |
| */ | |
| public function initializeArguments() { | |
| parent::initializeArguments(); | |
| $this->registerArgument('dateFormat', 'string', 'The date format used for entering data in this field.', FALSE, 'Y-m-d\TH:i:sP'); | |
| } | |
| /** | |
| * @param bool $required | |
| * @param string $type | |
| * @return string | |
| * @throws \InvalidArgumentException | |
| */ | |
| public function render($required = FALSE, $type = 'text') { | |
| $name = $this->getName(); | |
| $value = $this->getValue(FALSE); | |
| $this->registerFieldNameForFormTokenGeneration($name); | |
| $this->tag->addAttribute('type', $type); | |
| $this->tag->addAttribute('name', $name . '[date]'); | |
| if ($this->hasMappingErrorOccurred()) { | |
| $this->tag->addAttribute('value', $value['date']); | |
| } else { | |
| if ($value instanceof \DateTime) { | |
| $this->tag->addAttribute('value', $value->format($this->arguments['dateFormat'])); | |
| } else { | |
| if ($value !== NULL) { | |
| throw new \InvalidArgumentException('Expected \DateTime object, but got ' . gettype($value) . ' for ' . $this->getName()); | |
| } | |
| } | |
| } | |
| if ($required === TRUE) { | |
| $this->tag->addAttribute('required', 'required'); | |
| } | |
| $this->setErrorClassAttribute(); | |
| return $this->tag->render() . $this->renderHiddenDateFormatField(); | |
| } | |
| /** | |
| * @return string | |
| */ | |
| protected function renderHiddenDateFormatField() { | |
| return '<input type="hidden" value="' . htmlspecialchars($this->arguments['dateFormat']) . '" name="' . $this->getName() . '[dateFormat]" />'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment