Add loadValuesFromObject support for form containers
authorMatthias Schmidt <gravatronics@live.com>
Sun, 24 Feb 2019 13:31:08 +0000 (14:31 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 24 Feb 2019 13:33:54 +0000 (14:33 +0100)
See #2509

wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php
wcfsetup/install/files/lib/system/form/builder/container/FormContainer.class.php
wcfsetup/install/files/lib/system/form/builder/container/IFormContainer.class.php

index 7dc0ebf34454e364cdfa01dfd4e353ccaf1904ce..005e2e59836f3b6c71f92d7f0608779dc10700fb 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\system\form\builder;
 use wcf\data\IStorableObject;
+use wcf\system\form\builder\container\IFormContainer;
 use wcf\system\form\builder\data\FormDataHandler;
 use wcf\system\form\builder\data\IFormDataHandler;
 use wcf\system\form\builder\field\data\processor\DefaultFormFieldDataProcessor;
@@ -277,19 +278,24 @@ class FormDocument implements IFormDocument {
                
                /** @var IFormNode $node */
                foreach ($this->getIterator() as $node) {
-                       if ($node instanceof IFormField && $node->isAvailable()) {
-                               if ($node->getObjectProperty() !== $node->getId()) {
-                                       try {
-                                               $node->loadValueFromObject($object);
+                       if ($node->isAvailable()) {
+                               if ($node instanceof IFormField) {
+                                       if ($node->getObjectProperty() !== $node->getId()) {
+                                               try {
+                                                       $node->loadValueFromObject($object);
+                                               }
+                                               catch (\InvalidArgumentException $e) {
+                                                       // if an object property is explicitly set,
+                                                       // ignore invalid values as this might not be
+                                                       // the appropriate field
+                                               }
                                        }
-                                       catch (\InvalidArgumentException $e) {
-                                               // if an object property is explicitly set,
-                                               // ignore invalid values as this might not be
-                                               // the appropriate field
+                                       else {
+                                               $node->loadValueFromObject($object);
                                        }
                                }
-                               else {
-                                       $node->loadValueFromObject($object);
+                               else if ($node instanceof IFormContainer) {
+                                       $node->loadValuesFromObject($object);
                                }
                        }
                }
index 075c458bb8a95253fa3ddacba508124ebc460172..ae9d6d22833a0f70462eca31c0d129eb746a8d6b 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system\form\builder\container;
+use wcf\data\IStorableObject;
 use wcf\system\form\builder\IFormChildNode;
 use wcf\system\form\builder\IFormDocument;
 use wcf\system\form\builder\TFormChildNode;
@@ -39,6 +40,15 @@ class FormContainer implements IFormContainer {
                ]), true);
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function loadValuesFromObject(IStorableObject $object) {
+               // does nothing
+               
+               return $this;
+       }
+       
        /**
         * @inheritDoc
         */
index 72455c732d697497b3a3d91b21cdcf435eb93c5f..5e80f09ddf975467270204402bb18085aa7c769f 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\system\form\builder\container;
+use wcf\data\IStorableObject;
 use wcf\system\form\builder\IFormChildNode;
 use wcf\system\form\builder\IFormElement;
 use wcf\system\form\builder\IFormParentNode;
@@ -13,4 +14,16 @@ use wcf\system\form\builder\IFormParentNode;
  * @package    WoltLabSuite\Core\System\Form\Builder\Container
  * @since      5.2
  */
-interface IFormContainer extends IFormChildNode, IFormElement, IFormParentNode {}
+interface IFormContainer extends IFormChildNode, IFormElement, IFormParentNode {
+       /**
+        * This method is called by `IFormDocument::loadValuesFromObject()` to inform the container
+        * that object data is loaded.
+        * 
+        * This method is *not* intended to generally call `IFormField::loadValueFromObject()` on
+        * its form field children as these methods are already called by `IFormDocument::loadValuesFromObject()`.
+        * 
+        * @param       IStorableObject         $object         object used to load field values
+        * @return      static                                  this container
+        */
+       public function loadValuesFromObject(IStorableObject $object);
+}