TYPO3 CMS  TYPO3_6-2
AbstractFormFieldViewHelper.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  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
32 
38 
45  public function initializeArguments() {
46  parent::initializeArguments();
47  $this->registerArgument('name', 'string', 'Name of input tag');
48  $this->registerArgument('value', 'mixed', 'Value of input tag');
49  $this->registerArgument('property', 'string', 'Name of Object Property. If used in conjunction with <f:form object="...">, "name" and "value" properties will be ignored.');
50  }
51 
60  protected function getName() {
61  $name = $this->getNameWithoutPrefix();
62  return $this->prefixFieldName($name);
63  }
64 
70  protected function getNameWithoutPrefix() {
71  if ($this->isObjectAccessorMode()) {
72  $formObjectName = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
73  if (!empty($formObjectName)) {
74  $propertySegments = explode('.', $this->arguments['property']);
75  $propertyPath = '';
76  foreach ($propertySegments as $segment) {
77  $propertyPath .= '[' . $segment . ']';
78  }
79  $name = $formObjectName . $propertyPath;
80  } else {
81  $name = $this->arguments['property'];
82  }
83  } else {
84  $name = $this->arguments['name'];
85  }
86  if ($this->hasArgument('value') && is_object($this->arguments['value'])) {
87  // TODO: Use $this->persistenceManager->isNewObject() once it is implemented
88  if (NULL !== $this->persistenceManager->getIdentifierByObject($this->arguments['value'])) {
89  $name .= '[__identity]';
90  }
91  }
92  return $name;
93  }
94 
102  protected function getValue($convertObjects = TRUE) {
103  $value = NULL;
104  if ($this->hasArgument('value')) {
105  $value = $this->arguments['value'];
106  } elseif ($this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper') && $this->hasMappingErrorOccurred()) {
108  $value = $this->getLastSubmittedFormData();
109  } elseif ($this->isObjectAccessorMode() && $this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject')) {
111  $value = $this->getPropertyValue();
112  }
113  if ($convertObjects) {
114  $value = $this->convertToPlainValue($value);
115  }
116  return $value;
117  }
118 
125  protected function convertToPlainValue($value) {
126  if (is_object($value)) {
127  $identifier = $this->persistenceManager->getIdentifierByObject($value);
128  if ($identifier !== NULL) {
129  $value = $identifier;
130  }
131  }
132  return $value;
133  }
134 
140  protected function hasMappingErrorOccurred() {
141  return $this->controllerContext->getRequest()->getOriginalRequest() !== NULL;
142  }
143 
150  protected function getLastSubmittedFormData() {
151  $propertyPath = rtrim(preg_replace('/(\\]\\[|\\[|\\])/', '.', $this->getNameWithoutPrefix()), '.');
152  $value = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($this->controllerContext->getRequest()->getOriginalRequest()->getArguments(), $propertyPath);
153  return $value;
154  }
155 
163  $propertySegments = explode('.', $this->arguments['property']);
164  if (count($propertySegments) >= 2) {
165  // hierarchical property. If there is no "." inside (thus $propertySegments == 1), we do not need to do anything
166  $formObject = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject');
167  $objectName = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
168  // If Count == 2 -> we need to go through the for-loop exactly once
169  for ($i = 1; $i < count($propertySegments); $i++) {
170  $object = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($formObject, implode('.', array_slice($propertySegments, 0, $i)));
171  $objectName .= '[' . $propertySegments[($i - 1)] . ']';
172  $hiddenIdentityField = $this->renderHiddenIdentityField($object, $objectName);
173  // Add the hidden identity field to the ViewHelperVariableContainer
174  $additionalIdentityProperties = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties');
175  $additionalIdentityProperties[$objectName] = $hiddenIdentityField;
176  $this->viewHelperVariableContainer->addOrUpdate('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties', $additionalIdentityProperties);
177  }
178  }
179  }
180 
186  protected function getPropertyValue() {
187  if (!$this->viewHelperVariableContainer->exists('TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper', 'formObject')) {
188  return NULL;
189  }
190  $formObject = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject');
191  $propertyName = $this->arguments['property'];
192  if (is_array($formObject)) {
193  return isset($formObject[$propertyName]) ? $formObject[$propertyName] : NULL;
194  }
195  return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($formObject, $propertyName);
196  }
197 
203  protected function isObjectAccessorMode() {
204  return $this->hasArgument('property') && $this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
205  }
206 
212  protected function setErrorClassAttribute() {
213  if ($this->hasArgument('class')) {
214  $cssClass = $this->arguments['class'] . ' ';
215  } else {
216  $cssClass = '';
217  }
218  if ($this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
219  $mappingResultsForProperty = $this->getMappingResultsForProperty();
220  if ($mappingResultsForProperty->hasErrors()) {
221  if ($this->hasArgument('errorClass')) {
222  $cssClass .= $this->arguments['errorClass'];
223  } else {
224  $cssClass .= 'error';
225  }
226  $this->tag->addAttribute('class', $cssClass);
227  }
228  } else {
229  // @deprecated since Fluid 1.4.0, will will be removed two versions after Fluid 6.1.
230  $errors = $this->getErrorsForProperty();
231  if (count($errors) > 0) {
232  if ($this->hasArgument('errorClass')) {
233  $cssClass .= $this->arguments['errorClass'];
234  } else {
235  $cssClass .= 'error';
236  }
237  $this->tag->addAttribute('class', $cssClass);
238  }
239  }
240  }
241 
247  protected function getMappingResultsForProperty() {
248  if (!$this->isObjectAccessorMode()) {
249  return new \TYPO3\CMS\Extbase\Error\Result();
250  }
251  $originalRequestMappingResults = $this->controllerContext->getRequest()->getOriginalRequestMappingResults();
252  $formObjectName = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
253  return $originalRequestMappingResults->forProperty($formObjectName)->forProperty($this->arguments['property']);
254  }
255 
262  protected function getErrorsForProperty() {
263  if (!$this->isObjectAccessorMode()) {
264  return array();
265  }
266  $errors = $this->controllerContext->getRequest()->getErrors();
267  $formObjectName = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
268  $propertyName = $this->arguments['property'];
269  foreach ($errors as $error) {
270  if ($error instanceof \TYPO3\CMS\Extbase\Validation\PropertyError && $error->getPropertyName() === $formObjectName) {
271  $formErrors = $error->getErrors();
272  foreach ($formErrors as $formError) {
273  if ($formError instanceof \TYPO3\CMS\Extbase\Validation\PropertyError && $formError->getPropertyName() === $propertyName) {
274  return $formError->getErrors();
275  }
276  }
277  }
278  }
279  return array();
280  }
281 
288  protected function renderHiddenFieldForEmptyValue() {
289  $hiddenFieldNames = array();
290  if ($this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields')) {
291  $hiddenFieldNames = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields');
292  }
293  $fieldName = $this->getName();
294  if (substr($fieldName, -2) === '[]') {
295  $fieldName = substr($fieldName, 0, -2);
296  }
297  if (!in_array($fieldName, $hiddenFieldNames)) {
298  $hiddenFieldNames[] = $fieldName;
299  $this->viewHelperVariableContainer->addOrUpdate('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'renderedHiddenFields', $hiddenFieldNames);
300  return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="" />';
301  }
302  return '';
303  }
304 }
registerArgument($name, $type, $description, $required=FALSE, $defaultValue=NULL)
static getPropertyPath($subject, $propertyPath)