Load the file(s) when `$this->value` is set
authorCyperghost <olaf_schmitz_1@t-online.de>
Wed, 19 Jun 2024 09:09:45 +0000 (11:09 +0200)
committerCyperghost <olaf_schmitz_1@t-online.de>
Wed, 19 Jun 2024 09:09:45 +0000 (11:09 +0200)
com.woltlab.wcf/templates/shared_fileProcessorFormField.tpl
ts/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor.js
wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php

index ea9c174bdbf9dc99546281f81805460853b74590..110fb7ac77daf54e547d1e6f1afb3cf7035b956d 100644 (file)
@@ -1,14 +1,9 @@
+{assign var="files" value=$field->getFiles()}
 {if $maxUploads === 1 && $imageOnly}
        <div class="fileUpload__preview">
                {if $field->getValue()}
-               {*<woltlab-core-file>…</woltlab-core-file>*}
-                       <ul class="fileUpload__preview__buttons buttonList">
-                               <li>
-                                       <button class="button small" type="button">
-                                               {lang}wcf.global.button.delete{/lang}
-                                       </button>
-                               </li>
-                       </ul>
+                       {assign var="file" value=$files|reset}
+                       {unsafe:$file->toHtmlElement()}
                {/if}
        </div>
 {else}
index b1113cfd4e9eacaf3f36194ea97213e842cac576..889b17533feb78c8f7eb96984f6e29c804d23058 100644 (file)
@@ -29,6 +29,10 @@ export class FileProcessor {
       void this.#registerFile(event.detail);
     });
     this.#fileInput = this.#uploadButton.shadowRoot!.querySelector<HTMLInputElement>('input[type="file"]')!;
+
+    this.#container.querySelectorAll<WoltlabCoreFileElement>("woltlab-core-file").forEach((element) => {
+      void this.#registerFile(element);
+    });
   }
 
   get isSingleFileUpload(): boolean {
index 0bec3cb7bcc197d9fdbfc3f08501edfbc60a623a..1dd15ec25dfbac8a74abeeb344e934175cd56bc3 100644 (file)
@@ -25,6 +25,9 @@ define(["require", "exports", "WoltLabSuite/Core/Language", "WoltLabSuite/Core/A
                 void this.#registerFile(event.detail);
             });
             this.#fileInput = this.#uploadButton.shadowRoot.querySelector('input[type="file"]');
+            this.#container.querySelectorAll("woltlab-core-file").forEach((element) => {
+                void this.#registerFile(element);
+            });
         }
         get isSingleFileUpload() {
             // TODO check if only images are allowed
index 9251ecedd1f2b9274b562a459ab5b7c9d5f03f0f..9ee31e5037f3ea5a596c6f685bed060bfe57f1a2 100644 (file)
@@ -2,9 +2,12 @@
 
 namespace wcf\system\form\builder\field;
 
+use wcf\data\file\File;
+use wcf\data\file\FileList;
 use wcf\system\file\processor\FileProcessor;
 use wcf\system\file\processor\IFileProcessor;
 use wcf\system\form\builder\TObjectTypeFormNode;
+use wcf\util\ArrayUtil;
 use wcf\util\ImageUtil;
 
 /**
@@ -25,13 +28,31 @@ final class FileProcessorFormField extends AbstractFormField
 
     private array $context = [];
 
+    /**
+     * @var File[]
+     */
+    private array $files = [];
+
     #[\Override]
     public function readValue()
     {
-        \wcfDebug($this->getDocument()->getRequestData());
         if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
-            $this->context = $this->getDocument()->getRequestData($this->getPrefixedId());
+            $value = $this->getDocument()->getRequestData($this->getPrefixedId());
+
+            if ($this->isSingleFileUpload()) {
+                $this->value(\intval($value));
+            } else {
+                $this->value(ArrayUtil::toIntegerArray($value));
+            }
         }
+
+        return $this;
+    }
+
+    #[\Override]
+    public function hasSaveValue()
+    {
+        return $this->isSingleFileUpload();
     }
 
     #[\Override]
@@ -55,12 +76,46 @@ final class FileProcessorFormField extends AbstractFormField
         return $this->getObjectType()->getProcessor();
     }
 
+    private function isSingleFileUpload(): bool
+    {
+        return $this->getFileProcessor()->getMaximumCount($this->context) === 1;
+    }
+
     #[\Override]
     public function getObjectTypeDefinition()
     {
         return 'com.woltlab.wcf.file';
     }
 
+    public function getFiles(): array
+    {
+        return $this->files;
+    }
+
+    #[\Override]
+    public function value($value)
+    {
+        if ($this->isSingleFileUpload()) {
+            $file = new File($value);
+            if ($file->fileID === $value) {
+                $this->files = [$file];
+            }
+
+            return parent::value($value);
+        } else {
+            if (!\is_array($value)) {
+                $value = [$value];
+            }
+
+            $fileList = new FileList();
+            $fileList->setObjectIDs($value);
+            $fileList->readObjects();
+            $this->files = $fileList->getObjects();
+
+            return parent::value($value);
+        }
+    }
+
     /**
      * Returns the context for the file processor.
      */