From 35402e7169c9e9910fef138a7223d8cd86cef7c4 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Tue, 16 Apr 2019 18:54:45 +0200 Subject: [PATCH] Add `ContentLanguageFormField` See #2509 --- .../templates/__contentLanguageFormField.tpl | 36 ++++++++ syncTemplates.json | 1 + .../templates/__contentLanguageFormField.tpl | 36 ++++++++ .../install/files/acp/templates/header.tpl | 1 + .../Builder/Field/Language/ContentLanguage.js | 29 ++++++ .../js/WoltLabSuite/Core/Language/Chooser.js | 5 +- .../ContentLanguageFormField.class.php | 89 +++++++++++++++++++ 7 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 com.woltlab.wcf/templates/__contentLanguageFormField.tpl create mode 100644 wcfsetup/install/files/acp/templates/__contentLanguageFormField.tpl create mode 100644 wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Language/ContentLanguage.js create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/language/ContentLanguageFormField.class.php diff --git a/com.woltlab.wcf/templates/__contentLanguageFormField.tpl b/com.woltlab.wcf/templates/__contentLanguageFormField.tpl new file mode 100644 index 0000000000..f26a83a1a2 --- /dev/null +++ b/com.woltlab.wcf/templates/__contentLanguageFormField.tpl @@ -0,0 +1,36 @@ +{include file='__formFieldHeader'} + + + + + +{include file='__formFieldFooter'} diff --git a/syncTemplates.json b/syncTemplates.json index f1eb2bcb31..a8f71962cd 100644 --- a/syncTemplates.json +++ b/syncTemplates.json @@ -6,6 +6,7 @@ "templates": [ "__aclFormField", "__booleanFormField", + "__contentLanguageFormField", "__dateFormField", "__devtoolsLanguageChooser", "__form", diff --git a/wcfsetup/install/files/acp/templates/__contentLanguageFormField.tpl b/wcfsetup/install/files/acp/templates/__contentLanguageFormField.tpl new file mode 100644 index 0000000000..f26a83a1a2 --- /dev/null +++ b/wcfsetup/install/files/acp/templates/__contentLanguageFormField.tpl @@ -0,0 +1,36 @@ +{include file='__formFieldHeader'} + + + + + +{include file='__formFieldFooter'} diff --git a/wcfsetup/install/files/acp/templates/header.tpl b/wcfsetup/install/files/acp/templates/header.tpl index 3d61100245..89432be481 100644 --- a/wcfsetup/install/files/acp/templates/header.tpl +++ b/wcfsetup/install/files/acp/templates/header.tpl @@ -118,6 +118,7 @@ 'wcf.global.form.error.lessThan': '{lang __literal=true}wcf.global.form.error.lessThan{/lang}', 'wcf.global.form.error.multilingual': '{lang}wcf.global.form.error.multilingual{/lang}', 'wcf.global.form.input.maxItems': '{lang}wcf.global.form.input.maxItems{/lang}', + 'wcf.global.language.noSelection': '{lang}wcf.global.language.noSelection{/lang}', 'wcf.global.loading': '{lang}wcf.global.loading{/lang}', 'wcf.global.noSelection': '{lang}wcf.global.noSelection{/lang}', 'wcf.global.select': '{lang}wcf.global.select{/lang}', diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Language/ContentLanguage.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Language/ContentLanguage.js new file mode 100644 index 0000000000..57788494db --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Language/ContentLanguage.js @@ -0,0 +1,29 @@ +/** + * Data handler for a content language form builder field in an Ajax form. + * + * @author Matthias Schmidt + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Form/Builder/Field/Language/ContentLanguage + * @since 5.2 + */ +define(['Core', 'WoltLabSuite/Core/Language/Chooser', '../Value'], function(Core, LanguageChooser, FormBuilderFieldValue) { + "use strict"; + + /** + * @constructor + */ + function FormBuilderFieldContentLanguage(fieldId) { + this.init(fieldId); + }; + Core.inherit(FormBuilderFieldContentLanguage, FormBuilderFieldValue, { + /** + * @see WoltLabSuite/Core/Form/Builder/Field/Field#destroy + */ + destroy: function() { + LanguageChooser.removeChooser(this._fieldId); + } + }); + + return FormBuilderFieldContentLanguage; +}); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Language/Chooser.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Language/Chooser.js index eca57aae4b..a012d25288 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Language/Chooser.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Language/Chooser.js @@ -79,7 +79,10 @@ define(['Dictionary', 'Language', 'Dom/Traverse', 'Dom/Util', 'ObjectMap', 'Ui/S if (element.parentNode.nodeName === 'DD') { container = elCreate('div'); container.className = 'dropdown'; - element.parentNode.insertBefore(container, element); + + // language chooser is the first child so that descriptions and error messages + // are always shown below the language chooser + DomUtil.prepend(container, element.parentNode); } else { container = element.parentNode; diff --git a/wcfsetup/install/files/lib/system/form/builder/field/language/ContentLanguageFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/language/ContentLanguageFormField.class.php new file mode 100644 index 0000000000..dd8e0f7826 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/language/ContentLanguageFormField.class.php @@ -0,0 +1,89 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field\Language + * @since 5.2 + */ +class ContentLanguageFormField extends AbstractFormField implements IImmutableFormField { + use TDefaultIdFormField; + use TImmutableFormField; + + /** + * @inheritDoc + */ + protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/Language/ContentLanguage'; + + /** + * @inheritDoc + */ + protected $templateName = '__contentLanguageFormField'; + + /** + * Creates a new instance of `ContentLanguageFormField`. + */ + public function __construct() { + $this->label('wcf.user.language'); + } + + /** + * @inheritDoc + */ + public function getContentLanguages() { + return LanguageFactory::getInstance()->getContentLanguages(); + } + + /** + * @inheritDoc + */ + public function isAvailable() { + return LanguageFactory::getInstance()->multilingualismEnabled() + && !empty(LanguageFactory::getInstance()->getContentLanguageIDs()) + && parent::isAvailable(); + } + + /** + * @inheritDoc + */ + public function readValue() { + if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { + $this->value = intval($this->getDocument()->getRequestData($this->getPrefixedId())); + + if (!$this->isRequired() && !$this->value) { + $this->value = null; + } + } + + return $this; + } + + /** + * @inheritDoc + */ + public function validate() { + if ($this->isRequired() && LanguageFactory::getInstance()->getLanguage($this->getValue()) === null) { + $this->addValidationError(new FormFieldValidationError( + 'invalidValue', + 'wcf.global.form.error.noValidSelection' + )); + } + } + + /** + * @inheritDoc + */ + protected static function getDefaultId() { + return 'languageID'; + } +} -- 2.20.1