Version: current

     1 <?php
     2 /**
     3  * This class represents "transformations" of a form - such as making it printable or making it readonly.
     4  * The idea is that sometimes you will want to make your own such transformations, and you shouldn't have
     5  * to edit the underlying code to support this.
     6  *
     7  * The first step in creating a transformation is subclassing FormTransformation.  After that, you have two
     8  * ways of defining specific functionality:
     9  *   - Define performMyTransformation() methods on each applicable FormField() object.
    10  *   - Define transformFieldType($field) methods on your subclass of FormTransformation.
    11  *
    12  * To actually perform the transformation, call $form->transform(new MyTransformation());
    13  * @package forms
    14  * @subpackage transformations
    15  */
    16 class FormTransformation extends Object {
    17     function transform(FormField $field) {
    18         // Look for a performXXTransformation() method on the field itself.
    19         // performReadonlyTransformation() is a pretty commonly applied method.
    20         // Otherwise, look for a transformXXXField() method on this object.
    21         // This is more commonly done in custom transformations
    22
    23         // We iterate through each array simultaneously, looking at [0] of both, then [1] of both.
    24         // This provides a more natural failover scheme.
    25
    26         $transNames = array_reverse(array_values(ClassInfo::ancestry($this->class)));
    27         $fieldClasses = array_reverse(array_values(ClassInfo::ancestry($field->class)));
    28
    29         $len = max(sizeof($transNames), sizeof($fieldClasses));
    30         for($i=0;$i<$len;$i++) {
    31             // This is lets fieldClasses be longer than transNames
    32             if($transName = $transNames[$i]) {
    33                 if($field->hasMethod('perform' . $transName)) {
    34                     $funcName = 'perform' . $transName;
    35                     //echo "<li>$field->class used $funcName";
    36                     return $field->$funcName($this);
    37                 }
    38             }
    39
    40             // And this one does the reverse.
    41             if($fieldClass = $fieldClasses[$i]) {
    42                 if($this->hasMethod('transform' . $fieldClass)) {
    43                     $funcName = 'transform' . $fieldClass;
    44                     //echo "<li>$field->class used $funcName";
    45                     return $this->$funcName($field);
    46                 }
    47             }
    48         }
    49
    50         user_error("FormTransformation:: Can't perform '$this->class' on '$field->class'", E_USER_ERROR);
    51     }
    52 }
    53
    54 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -