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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By joeck
      Hi,
      I'm facing the issue where i have a repeater field with a multi language textarea (german & english, german beeing default). In german more blocks should be displayed as in english.
      The textarea field is configured as inherit default language if blank (I don't want to change this since it is the wanted behaviour for all other pages...).
      Now I want to access the unformatted value of the textarea field in the repeater and only show it if it is not empty. Something like this:
      $body = ""; $page->of(false); foreach($page->blocks as $block){ //blocks is repeater field, each block has title and textarea if ($block->textarea->getLanguageValue($user->language)){ $body .= <<<EOD <div> <div class='uk-card uk-card-default uk-card-body'> <h3 class='uk-card-title'>$block->title</h3> $block->textarea </div> </div> EOD; } } $page->of(true); print $body; However this doesn't work: Call to a member function getLanguageValue() on string.
      I also tried the other syntax for getLanguageValue:
      $page->getLanguageValue($language, $field) But I didn't manage to get a unique name of the textarea field in the repeater.
      I looked into the API of FieltypeRepeater but couldn't find anyhting that fixed my issue.
      Any ideas?
    • By picarica
      So hello i am trying to get a .png file from file field and put it automatically to image field, why png image is in the file field is because i already have a hook that extracts .zip and uploads all content into file field, but i just realizes i cant use size() function on image in file field so i am tryin got reupload it to images field
      i already have something like  this in ready.php
      $word = ".png"; foreach($page->subor_hry as $file) { if(strpos($file, $word) !== false){ $page->images_thumb = $file->url; } } by my logic it should work but it dosnt i get error ProcessPageEdit: Unable to read: /site-hry/assets/files/1027/flash_fishy_screenshot.png
      when i remove url from $file->url i just get ProcessPageEdit: Item added to ProcessWire\Pageimages is not an allowed type
      so what am i doing wrong? is there some other way to do this ?
      also can i have all this in
      $this->addHookAfter('Pages::saveReady', function(HookEvent $event) { whats the correct function to have it apply on all pages ?
    • By rooofl
      Hi! I am trying to create a hook that takes the value of a form file field, and add it to an image gallery of an existing page.
      $forms->addHookBefore('FormBuilderProcessor::processInputDone', function($e) { $form = $e->arguments(0); if($form->name == 'new-inuse') { // related_font is a page selector $font = $form->getChildByName('related_font'); // getting the page name $fontName = $font->attr('value')->name; // finds the page from its name $fontPage = wire('pages')->find("name=$fontName"); // in_use_image is a file upload field $image = $form->getChildByName('in_use_image'); // trying to add the image to the existing image gallery in_use field $fontPage->in_use->add('$image'); // error here $fontPage->save(); } }); This outputs the following error: `Call to a member function add() on null`. Any idea what’s wrong with my code?
    • By picarica
      so what i am trying to do is that i uploded some files into file field, and then i want hook to get MD5sum and other stuff from .xml and put it into text field into it coresponding pages, pages like this are gonna be many, for each one i want it to output it into its fields
    • By Paul Greinke
      Hi there. I wrote a custom module for one of my projects. In fact I maybe want to use my module in other projects too. In order to be variable and customizable  I need to implement some custom hooks into my module. So I can afterwards hook into the my functions in order to modify them to match the needs of the new project.
      I tried simply defining functions with the '__' prefix. But that did not work. I'm imagining something like the following:
      <?php class MyClass { public function ___someFunction() { // Do something } } // ready.php $this->addHookBefore('MyClass::someFunction', function($event) { // some customization }); Is there a way to accomplish that? 
×
×
  • Create New...