1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-08 07:47:00 +02:00

Add $inputfield->setLanguageValue($language, $value) and $inputfield->getLanguageValue($language) to Inputfield class when LanguageSupport is installed. This provides a nicer API for getting/setting multi-language values with Inputfields (where supported). Previously you could only get/set by dealing with "value1234" type settings where 1234 is language ID.

This commit is contained in:
Ryan Cramer
2024-04-19 11:57:39 -04:00
parent 3c0e9f3c43
commit 57b23ef9fe
3 changed files with 73 additions and 6 deletions

View File

@@ -102,16 +102,16 @@
* @property null|bool|Fieldtype $hasFieldtype The Fieldtype using this Inputfield, or boolean false when known not to have a Fieldtype, or null when not known. #pw-group-other
* @property null|Field $hasField The Field object associated with this Inputfield, or null when not applicable or not known. #pw-group-other
* @property null|Page $hasPage The Page object associated with this Inputfield, or null when not applicable or not known. #pw-group-other
* @property null|Inputfield $hasInputfield If this Inputfield is owned/managed by another (other than parent/child relationship), it may be set here. 3.0.176+ #pw-group-other
* @property bool|null $useLanguages When multi-language support active, can be set to true to make it provide inputs for each language, where supported (default=false). #pw-group-behavior
* @property null|Inputfield $hasInputfield If this Inputfield is owned/managed by another (other than parent/child relationship), it may be set here. @since 3.0.176 #pw-group-other
* @property bool|null $useLanguages When multi-language support active, can be set to true to make it provide inputs for each language, where supported (default=false). #pw-group-behavior #pw-group-languages
* @property null|bool|int $entityEncodeLabel Set to boolean false to specifically disable entity encoding of field header/label (default=true). #pw-group-output
* @property null|bool $entityEncodeText Set to boolean false to specifically disable entity encoding for other text: description, notes, etc. (default=true). #pw-group-output
* @property int $renderFlags Options that can be applied to render, see "render*" constants (default=0). #pw-group-output 3.0.204+
* @property int $renderFlags Options that can be applied to render, see "render*" constants (default=0). @since 3.0.204 #pw-group-output
* @property int $renderValueFlags Options that can be applied to renderValue mode, see "renderValue" constants (default=0). #pw-group-output
* @property string $wrapClass Optional class name (CSS) to apply to the HTML element wrapping the Inputfield. #pw-group-other
* @property string $headerClass Optional class name (CSS) to apply to the InputfieldHeader element #pw-group-other
* @property string $contentClass Optional class name (CSS) to apply to the InputfieldContent element #pw-group-other
* @property string $addClass Formatted class string letting you add class to any of the above (see addClass method). #pw-group-other 3.0.204+
* @property string $addClass Formatted class string letting you add class to any of the above (see addClass method). @since 3.0.204 #pw-group-other
* @property int|null $textFormat Text format to use for description/notes text in Inputfield (see textFormat constants) #pw-group-output
*
* @method string|Inputfield required($required = null) Get or set required state. @since 3.0.110 #pw-group-behavior
@@ -121,7 +121,11 @@
* @method string|Inputfield headerClass($class = null) Get header class attribute or add a class to it. @since 3.0.110 #pw-group-other
* @method string|Inputfield contentClass($class = null) Get content class attribute or add a class to it. @since 3.0.110 #pw-group-other
*
*
* MULTI-LANGUAGE METHODS (requires LanguageSupport module to be installed)
* ======================
* @method void setLanguageValue($language, $value) Set language value for Inputfield that supports it. Requires LanguageSupport module. $language can be Language, id (int) or name (string). @since 3.0.238 #pw-group-languages
* @method string|mixed getLanguageValue($language) Get language value for Inputfield that supports it. Requires LanguageSupport module. $language can be Language, id (int) or name (string). @since 3.0.238 #pw-group-languages
*
* HOOKABLE METHODS
* ================
* @method string render()

View File

@@ -385,7 +385,7 @@ class InputfieldText extends Inputfield {
if($this->hasFieldtype === false) {
/** @var InputfieldText $field */
$field = $modules->get('InputfieldText');
$field = $modules->get($this->className());
$field->setAttribute('name', 'initValue');
$field->label = $this->_('Initial Value');
$field->description = $this->_('Optional initial/default value pre-populated for the user.');

View File

@@ -206,6 +206,8 @@ class LanguageSupport extends WireData implements Module, ConfigurableModule {
$this->addHookAfter('Inputfield::renderValue', $this, 'hookInputfieldAfterRender');
$this->addHookAfter('Inputfield::processInput', $this, 'hookInputfieldAfterProcessInput');
$this->addHookBefore('Inputfield::processInput', $this, 'hookInputfieldBeforeProcessInput');
$this->addHookMethod('Inputfield::getLanguageValue', $this, 'hookInputfieldGetLanguageValue');
$this->addHookMethod('Inputfield::setLanguageValue', $this, 'hookInputfieldGetLanguageValue');
$this->addHookAfter('Field::getInputfield', $this, 'hookFieldGetInputfield');
$pages->addHook('added', $this, 'hookPageAdded');
$pages->addHook('deleteReady', $this, 'hookPageDeleteReady');
@@ -597,6 +599,67 @@ class LanguageSupport extends WireData implements Module, ConfigurableModule {
$inputfield->setTrackChanges(true);
}
/**
* Adds an Inputfield::setLanguageValue($language, $value) method
*
* @param HookEvent $event
* @since 3.0.238
*
*/
public function hookInputfieldSetLanguageValue(HookEvent $event) {
/** @var Inputfield $inputfield */
$inputfield = $event->object;
$language = $event->arguments(0);
$value = $event->arguments(1);
$valuePrevious = $inputfield->val();
if($value === $valuePrevious) return; // nothing to do
if(!$language instanceof Language) {
$language = $this->wire()->languages->get($language);
}
if(!$language || !$language->id) {
// nothing to do
} else if($valuePrevious instanceof LanguagesValueInterface) {
$valuePrevious->setLanguageValue($language->id, $value);
} else if($language->isDefault()) {
$inputfield->val($value);
} else {
$inputfield->set("value$language", $value);
}
}
/**
* Adds an Inputfield::getLanguageValue($language) method
*
* @param HookEvent $event
* @since 3.0.238
*
*/
public function hookInputfieldGetLanguageValue(HookEvent $event) {
/** @var Inputfield $inputfield */
$inputfield = $event->object;
$language = $event->arguments(0);
$value = $inputfield->val();
if(!$language instanceof Language) {
$language = $this->wire()->languages->get($language);
}
if(!$language || !$language->id) {
$event->return = null;
} else if($value instanceof LanguagesValueInterface) {
$event->return = $value->getLanguageValue($language->id);
} else if($language->isDefault()) {
$event->return = $value;
} else {
$event->return = $inputfield->get("value$language");
}
}
/**
* Hook into Field::getInputfield to change label/description to proper language
*