‪TYPO3CMS  9.5
DatePickerViewHelper.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It originated from the Neos.Form package (www.neos.io)
9  *
10  * It is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License, either version 2
12  * of the License, or any later version.
13  *
14  * For the full copyright and license information, please read the
15  * LICENSE.txt file that was distributed with this source code.
16  *
17  * The TYPO3 project - inspiring people to share!
18  */
19 
24 
33 {
34 
38  protected ‪$tagName = 'input';
39 
43  protected ‪$propertyMapper;
44 
49  public function ‪injectPropertyMapper(\‪TYPO3\CMS\‪Extbase\Property\PropertyMapper ‪$propertyMapper)
50  {
51  $this->propertyMapper = ‪$propertyMapper;
52  }
53 
57  public function ‪initializeArguments()
58  {
59  parent::initializeArguments();
60  $this->registerTagAttribute('size', 'int', 'The size of the input field');
61  $this->registerTagAttribute('placeholder', 'string', 'Specifies a short hint that describes the expected value of an input element');
62  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
63  $this->registerArgument('initialDate', 'string', 'Initial date (@see http://www.php.net/manual/en/datetime.formats.php for supported formats)');
64  $this->registerArgument('enableDatePicker', 'bool', 'Enable the Datepicker', false, true);
65  $this->registerArgument('previewMode', 'bool', 'Preview mde flag', true, false);
66  $this->registerArgument('dateFormat', 'string', 'The date format', false, 'Y-m-d');
67  $this->registerUniversalTagAttributes();
68  }
69 
75  public function ‪render()
76  {
77  $enableDatePicker = $this->arguments['enableDatePicker'];
78  $dateFormat = $this->arguments['dateFormat'];
79  $previewMode = (bool)$this->arguments['previewMode'];
80  $placeholder = $this->arguments['additionalAttributes']['placeholder'] ?? $this->arguments['placeholder'];
81 
82  $name = $this->‪getName();
84 
85  $this->tag->addAttribute('type', 'text');
86  $this->tag->addAttribute('name', $name . '[date]');
87 
88  if ($this->hasArgument('id')) {
89  $id = $this->arguments['id'];
90  } else {
91  $id = 'field' . md5(uniqid());
92  }
93 
94  if (empty($placeholder)) {
95  $this->tag->addAttribute('placeholder', $dateFormat);
96  }
97 
98  if ($enableDatePicker) {
99  $this->tag->addAttribute('readonly', 'readonly');
100  if (!$previewMode) {
101  $datePickerDateFormat = $this->‪convertDateFormatToDatePickerFormat($dateFormat);
102  $this->‪renderInlineJavascript($id, $datePickerDateFormat);
103  }
104  }
105  $date = $this->‪getSelectedDate();
106  if ($date !== null) {
107  $this->tag->addAttribute('value', $date->format($dateFormat));
108  }
109 
110  $this->tag->addAttribute('id', $id);
111 
112  $this->‪setErrorClassAttribute();
113  $content = '';
114  $content .= $this->tag->render();
115  $content .= '<input type="hidden" name="' . htmlspecialchars($name) . '[dateFormat]" value="' . htmlspecialchars($dateFormat) . '" />';
116 
117  return $content;
118  }
119 
123  protected function ‪getSelectedDate()
124  {
126  $formRuntime = $this->renderingContext
127  ->getViewHelperVariableContainer()
128  ->get(RenderRenderableViewHelper::class, 'formRuntime');
129 
130  $formState = $formRuntime->getFormState();
131 
132  $date = $formRuntime[$this->arguments['property']];
133  if ($date instanceof \DateTime) {
134  return $date;
135  }
136  if ($date !== null) {
137  $date = $this->propertyMapper->convert($date, 'DateTime');
138  if (!$date instanceof \DateTime) {
139  return null;
140  }
141  return $date;
142  }
143  if ($this->hasArgument('initialDate')) {
144  return new \DateTime($this->arguments['initialDate']);
145  }
146  }
147 
152  protected function ‪convertDateFormatToDatePickerFormat(string $dateFormat): string
153  {
154  $replacements = [
155  'd' => 'dd',
156  'D' => 'D',
157  'j' => 'o',
158  'l' => 'DD',
159 
160  'F' => 'MM',
161  'm' => 'mm',
162  'M' => 'M',
163  'n' => 'm',
164 
165  'Y' => 'yy',
166  'y' => 'y'
167  ];
168  return strtr($dateFormat, $replacements);
169  }
170 
175  protected function ‪renderInlineJavascript(string $uniqueIdentifier, string $datePickerDateFormat)
176  {
178  'ext_form_datepicker-' . $uniqueIdentifier,
179  'if ("undefined" !== typeof $) {
180  $(function() {
181  $("#' . $uniqueIdentifier . '").datepicker({
182  dateFormat: "' . $datePickerDateFormat . '"
183  }).on("keydown", function(e) {
184  // By using "backspace" or "delete", you can clear the datepicker again.
185  if(e.keyCode == 8 || e.keyCode == 46) {
186  e.preventDefault();
187  $.datepicker._clearDate(this);
188  }
189  });
190  });
191  }
192  '
193  );
194  }
195 
199  protected function ‪getPageRenderer(): ‪PageRenderer
200  {
201  return GeneralUtility::makeInstance(PageRenderer::class);
202  }
203 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:324
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\initializeArguments
‪initializeArguments()
Definition: DatePickerViewHelper.php:55
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\$tagName
‪string $tagName
Definition: DatePickerViewHelper.php:37
‪TYPO3
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\injectPropertyMapper
‪injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
Definition: DatePickerViewHelper.php:47
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper
Definition: DatePickerViewHelper.php:33
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration($fieldName)
Definition: AbstractFormViewHelper.php:101
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:35
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\$propertyMapper
‪TYPO3 CMS Extbase Property PropertyMapper $propertyMapper
Definition: DatePickerViewHelper.php:41
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\convertDateFormatToDatePickerFormat
‪string convertDateFormatToDatePickerFormat(string $dateFormat)
Definition: DatePickerViewHelper.php:150
‪TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper
Definition: RenderRenderableViewHelper.php:37
‪TYPO3\CMS\Core\Page\PageRenderer\addJsFooterInlineCode
‪addJsFooterInlineCode($name, $block, $compress=true, $forceOnTop=false)
Definition: PageRenderer.php:1246
‪TYPO3\CMS\Form\ViewHelpers\Form
Definition: DatePickerViewHelper.php:3
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\getSelectedDate
‪DateTime null getSelectedDate()
Definition: DatePickerViewHelper.php:121
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\render
‪string render()
Definition: DatePickerViewHelper.php:73
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\renderInlineJavascript
‪renderInlineJavascript(string $uniqueIdentifier, string $datePickerDateFormat)
Definition: DatePickerViewHelper.php:173
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:86
‪TYPO3\CMS\Form\ViewHelpers\Form\DatePickerViewHelper\getPageRenderer
‪PageRenderer getPageRenderer()
Definition: DatePickerViewHelper.php:197