--- /dev/null
+{include file='__formFieldHeader'}
+
+{if $field->isImmutable() && $field->getValue()}
+ {if $field->getMedia()->isImage && $field->getMedia()->hasThumbnail('small')}
+ <div id="{@$field->getPrefixedId()}_preview" class="selectedImagePreview">
+ {@$field->getMedia()->getThumbnailTag('small')}
+ </div>
+ {else}
+ <div class="box16 selectedImagePreview">
+ {@$field->getMedia()->getElementTag(16)}
+
+ <p>{$field->getMedia()->getTitle()}</p>
+ </div>
+ {/if}
+{else}
+ {if $field->isImageOnly()}
+ <div id="{@$field->getPrefixedId()}_preview" class="selectedImagePreview">
+ {if $field->getValue() && $field->getMedia()->hasThumbnail('small')}
+ {@$field->getMedia()->getThumbnailTag('small')}
+ {/if}
+ </div>
+ {/if}
+ <p class="button jsMediaSelectButton jsMediaSelectButton_{@$form->getPrefixedId()}" data-store="{@$field->getPrefixedId()}"{if $field->isImageOnly()} data-display="{@$field->getPrefixedId()}_preview"{/if}>{lang}wcf.media.choose{if $field->isImageOnly()}Image{else}File{/if}{/lang}</p>
+ <input type="hidden" name="{@$field->getPrefixedId()}" id="{@$field->getPrefixedId()}"{if $field->getValue()} value="{@$field->getValue()}"{/if}>
+
+ <script data-relocate="true">
+ {include file='mediaJavaScript'}
+
+ require(['WoltLabSuite/Core/Media/Manager/Select'], function(MediaManagerSelect) {
+ new MediaManagerSelect({
+ buttonClass: 'jsMediaSelectButton_{@$form->getPrefixedId()}',
+ {if $field->isImageOnly()}
+ dialogTitle: '{lang}wcf.media.chooseImage{/lang}',
+ imagesOnly: 1
+ {/if}
+ });
+ });
+ </script>
+{/if}
+
+{include file='__formFieldFooter'}
"__pollOptionsFormField",
"__radioButtonFormField",
"__ratingFormField",
+ "__singleMediaSelectionFormField",
"__singleSelectionFormField",
"__tabFormContainer",
"__tabMenuFormContainer",
--- /dev/null
+{include file='__formFieldHeader'}
+
+{if $field->isImmutable() && $field->getValue()}
+ {if $field->getMedia()->isImage && $field->getMedia()->hasThumbnail('small')}
+ <div id="{@$field->getPrefixedId()}_preview" class="selectedImagePreview">
+ {@$field->getMedia()->getThumbnailTag('small')}
+ </div>
+ {else}
+ <div class="box16 selectedImagePreview">
+ {@$field->getMedia()->getElementTag(16)}
+
+ <p>{$field->getMedia()->getTitle()}</p>
+ </div>
+ {/if}
+{else}
+ {if $field->isImageOnly()}
+ <div id="{@$field->getPrefixedId()}_preview" class="selectedImagePreview">
+ {if $field->getValue() && $field->getMedia()->hasThumbnail('small')}
+ {@$field->getMedia()->getThumbnailTag('small')}
+ {/if}
+ </div>
+ {/if}
+ <p class="button jsMediaSelectButton jsMediaSelectButton_{@$form->getPrefixedId()}" data-store="{@$field->getPrefixedId()}"{if $field->isImageOnly()} data-display="{@$field->getPrefixedId()}_preview"{/if}>{lang}wcf.media.choose{if $field->isImageOnly()}Image{else}File{/if}{/lang}</p>
+ <input type="hidden" name="{@$field->getPrefixedId()}" id="{@$field->getPrefixedId()}"{if $field->getValue()} value="{@$field->getValue()}"{/if}>
+
+ <script data-relocate="true">
+ {include file='mediaJavaScript'}
+
+ require(['WoltLabSuite/Core/Media/Manager/Select'], function(MediaManagerSelect) {
+ new MediaManagerSelect({
+ buttonClass: 'jsMediaSelectButton_{@$form->getPrefixedId()}',
+ {if $field->isImageOnly()}
+ dialogTitle: '{lang}wcf.media.chooseImage{/lang}',
+ imagesOnly: 1
+ {/if}
+ });
+ });
+ </script>
+{/if}
+
+{include file='__formFieldFooter'}
--- /dev/null
+<?php
+namespace wcf\system\form\builder\field\media;
+use wcf\data\media\ViewableMedia;
+use wcf\system\form\builder\field\AbstractFormField;
+use wcf\system\form\builder\field\IImmutableFormField;
+use wcf\system\form\builder\field\TImmutableFormField;
+
+/**
+ * Implementation of a form field to select a single media file.
+ *
+ * @author Matthias Schmidt
+ * @copyright 2001-2019 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Form\Builder\Field
+ * @since 5.2
+ */
+class SingleMediaSelectionFormField extends AbstractFormField implements IImmutableFormField {
+ use TImmutableFormField;
+
+ /**
+ * is `true` if only images can be selected and `false` otherwise
+ * @var boolean
+ */
+ protected $imageOnly = false;
+
+ /**
+ * media object with the current value as id
+ * @var null|ViewableMedia
+ */
+ protected $media;
+
+ /**
+ * @inheritDoc
+ */
+ protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Value';
+
+ /**
+ * @inheritDoc
+ */
+ protected $templateName = '__singleMediaSelectionFormField';
+
+ /**
+ * Returns the media object with the current value as id.
+ *
+ * @return ViewableMedia
+ *
+ * @throws \InvalidArgumentException if no or an invalid media id is set as value
+ */
+ public function getMedia() {
+ if ($this->media === null) {
+ if (!$this->getValue()) {
+ throw new \BadMethodCallException("Cannot be media object if no valid media id is set as value.");
+ }
+
+ $this->media = ViewableMedia::getMedia($this->getValue());
+ if ($this->media === null) {
+ throw new \UnexpectedValueException("Cannot load media with id '{$this->getValue()}'.");
+ }
+ }
+
+ return $this->media;
+ }
+
+ /**
+ * Sets if only images can be selected and returns this field.
+ *
+ * @param boolean $imageOnly
+ * @return static this field
+ */
+ public function imageOnly($imageOnly = true) {
+ $this->imageOnly = $imageOnly;
+
+ return $this;
+ }
+
+ /**
+ * Returns `true` if only images can be selected and `false` otherwise.
+ *
+ * By default, all images can be selected.
+ *
+ * @return boolean
+ */
+ public function isImageOnly() {
+ return $this->imageOnly;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function readValue() {
+ if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
+ $value = $this->getDocument()->getRequestData($this->getPrefixedId());
+
+ if ($value) {
+ $this->value = $value;
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function validate() {
+ parent::validate();
+
+ try {
+ $media = $this->getMedia();
+ if (!$media->isAccessible() || ($this->isImageOnly() && !$media->isImage)) {
+ $this->value = null;
+ }
+ }
+ catch (\BadMethodCallException $e) {
+ $this->value = null;
+ }
+ catch (\UnexpectedValueException $e) {
+ $this->value = null;
+ }
+ }
+}
<item name="wcf.media.caption.enableHtml"><![CDATA[HTML in der Bildunterschrift verwenden]]></item>
<item name="wcf.media.category.choose"><![CDATA[Kategorien]]></item>
<item name="wcf.media.categoryID"><![CDATA[Kategorie]]></item>
+ <item name="wcf.media.chooseFile"><![CDATA[Datei auswählen]]></item>
<item name="wcf.media.chooseImage"><![CDATA[Bild auswählen]]></item>
<item name="wcf.media.delete.confirmMessage"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Möchtest du{else}Möchten Sie{/if} die Datei <span class="confirmationObject">{$title}</span> wirklich löschen?]]></item>
<item name="wcf.media.edit"><![CDATA[Datei bearbeiten]]></item>
<item name="wcf.media.caption.enableHtml"><![CDATA[Enable HTML code in caption]]></item>
<item name="wcf.media.category.choose"><![CDATA[Categories]]></item>
<item name="wcf.media.categoryID"><![CDATA[Category]]></item>
+ <item name="wcf.media.chooseFile"><![CDATA[Select File]]></item>
<item name="wcf.media.chooseImage"><![CDATA[Select Image]]></item>
<item name="wcf.media.delete.confirmMessage"><![CDATA[Do you really want to delete the media file <span class="confirmationObject">{$title}</span>?]]></item>
<item name="wcf.media.edit"><![CDATA[Edit Media File]]></item>