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