Jump to content

Acces multilang field values inside processInput Hook


Recommended Posts

hello,

I want to build a custom validator on my multilang site.

I added following hook, but I get only the default language value. How can I access the values of the other languages?

 

 

public function init() {
  $this->addHookAfter("InputfieldTextarea::processInput", $this, "validateShortlinks");
}

public function validateShortlinks($event) {
	$field = $event->object;
	$text = $field->value; // only the default language, how can I get the other languages? 
 }

 

Link to comment
Share on other sites

@Andreas Augustin

Try to change init to ready as on init stage language is not set. 

public function ready() {
  $this->addHookAfter("InputfieldTextarea::processInput", $this, "validateShortlinks");
}

public function validateShortlinks($event) {
	$field = $event->object;
	$text = $field->value; // only the default language, how can I get the other languages? 
 }

 

Link to comment
Share on other sites

I don't normally deal with multi-language so I'm not sure if this is the best way, but it seems to work...

public function validateShortlinks($event) {
    $field = $event->object;
    
    // Get specific language value
    $french = $this->languages->get('french');
    $french_value = $field->get('value' . $french->id);
    
    // Or loop over all languages
    foreach($this->languages as $language) {
        $value = $field->get('value' . $language->id);
        //...
    }
}

 

Link to comment
Share on other sites

@Andreas Augustin

It's interesting - if you dump/log the $value here directly in InputfieldTextarea::processInput you can see that the method is called for every language. However, you can only actually hook the call for the default language - for the other languages the method must be called in such a way that hooks are not triggered (by calling the method name with the three underscores included).

So you'll probably have to get the other language values from $input (either the WireInput argument to the method or via the API $input variable). For example...

$wire->addHookBefore('InputfieldTextarea::processInput', function(HookEvent $event) {
    $inputfield = $event->object;
    $input = $event->arguments(0);

    if(!$inputfield->hasField || $inputfield->hasField != 'your_field_name') return;

    // The value for the default language
    $default_language_value = $inputfield->value;

    // The value for French language
    $french = $this->languages->get('french');
    $input_var_name = "{$inputfield->name}__{$french->id}";
    $french_value = $input->$input_var_name;

    // ...
});

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...