AbstractFormFieldViewHelper_getPropertyValue.php

Andreas Sommer, 2014-10-23 23:51

Download (3.15 KB)

 
1
<?php
2
namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
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
 *                                                                        */
23
/**
24
 * Abstract Form View Helper. Bundles functionality related to direct property access of objects in other Form ViewHelpers.
25
 *
26
 * If you set the "property" attribute to the name of the property to resolve from the object, this class will
27
 * automatically set the name and value of a form element.
28
 *
29
 * @api
30
 */
31
abstract class AbstractFormFieldViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper {
32
        
33
        /**
34
         * Get the current property of the object bound to this form.
35
         *
36
         * @return mixed Value
37
         */
38
        protected function getPropertyValue() {
39
                if (!$this->viewHelperVariableContainer->exists('TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper', 'formObject')) {
40
                        return NULL;
41
                }
42
                $formObject = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject');
43
                $propertyName = $this->arguments['property'];
44
                
45
                if (is_array($formObject)) {
46
                        return isset($formObject[$propertyName]) ? $formObject[$propertyName] : NULL;
47
                }
48
                        // if the formobject has mm related checkbox fields then the propertyName is written like this "fieldname.uid"
49
                if(preg_match('/\./', $propertyName))
50
                {
51
                                // split the propertyName by the .
52
                        $tmp = explode('.', $propertyName);
53
                                // genreate the getFunctionName
54
                        $fn = 'get' . ucfirst($tmp[0]);
55
                                // loop stored relations
56
                        foreach($formObject->$fn() as $_ => $child)
57
                        {
58
                                        // prevent object errors on empy values
59
                                if(is_object($child))
60
                                {
61
                                                // create an uid collection
62
                                        $array[] = $child->getUid();
63
                                }
64
                        }
65
                        return $array;
66
                }
67
                return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($formObject, $propertyName);
68
        }
69
}
70
?>