Make it easier to find a form field from a FormBuilder form
authorMarcel Werk <burntime@woltlab.com>
Wed, 3 Jul 2024 12:04:26 +0000 (14:04 +0200)
committerMarcel Werk <burntime@woltlab.com>
Wed, 3 Jul 2024 12:04:26 +0000 (14:04 +0200)
Basically, it is just a wrapper around `getNodeById()`, which also checks whether it is a `IFormField`.

wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php
wcfsetup/install/files/lib/system/form/builder/IFormDocument.class.php

index b792e0e9c43620b9cd1a10391117c79b90a053ea..f7121567604ab6ddbdc5e8fbc338b5e30fce4733 100644 (file)
@@ -783,4 +783,20 @@ class FormDocument implements IFormDocument
             $this->traitValidate();
         }
     }
+
+    /**
+     * @inheritDoc
+     */
+    public function getFormField(string $nodeId): ?IFormField
+    {
+        $node = $this->getNodeById($nodeId);
+        if ($node === null) {
+            return null;
+        }
+        if (!($node instanceof IFormField)) {
+            return null;
+        }
+
+        return $node;
+    }
 }
index 973df89323b88dba9e77fd2917620bc19d1b211c..666929524b37d7097b7aad7cf3ff83244288a41d 100644 (file)
@@ -5,6 +5,7 @@ namespace wcf\system\form\builder;
 use wcf\data\IStorableObject;
 use wcf\system\form\builder\button\IFormButton;
 use wcf\system\form\builder\data\IFormDataHandler;
+use wcf\system\form\builder\field\IFormField;
 
 /**
  * Represents a "whole" form (document).
@@ -410,4 +411,11 @@ interface IFormDocument extends IFormParentNode
      * @throws  \InvalidArgumentException   if the given form mode is invalid
      */
     public function successMessage($languageItem = null, array $variables = []);
+
+    /**
+     * Returns the form field with the given id or `null` if no such field exists.
+     *
+     * @since 6.1
+     */
+    public function getFormField(string $nodeId): ?IFormField;
 }