I’m facing an issue while trying to update a file’s field in a hook, on a multi-language setup.
I just can’t manage to overcome the current language I’m editing the page with by adding a second argument.
My config.php shows :
c::set('languages', array(
array(
'code' => 'fr',
'name' => 'French',
'default' => true,
'locale' => 'fr_FR',
'url' => '/',
),
array(
'code' => 'en',
'name' => 'English',
'locale' => 'en_US',
'url' => '/en',
),
));
c::set('language.detect', true);
Basically this hook is only pre-formatting a percent-based value I’m using for images placeholders :
kirby()->hook(['panel.file.upload', 'panel.file.replace'], function($file) {
if ($file->type() == 'image') {
(... $ratio = (blablabla) ...)
$file->update(array(
'paddingtop' => $ratio
), 'en');
}
});
Despite this it creates a .fr.txt file if I’m uploading the file while editing in french, and a .en.txt one if editing in english.
What could cause this ? I’d like to update both files with one upload.
Thanks !
The current language file will always be created automatically, but you can try to do the following:
$data = ['paddingtop' => $ratio];
$file->update($data, 'en');
$file->update($data, 'fr');
Otherwise you can also just write it into the default language file and then use “translate: false” for that field in the blueprint to make sure that the value is not being overwritten in a later update.
Thanks for your answer Bastian !
This snippet sounds exactly like what I’m trying to achieve but I tried it previously and unfortunately it doesn’t work, I’m still only getting the current language file.
The second would imply that my client remembers to only add files while using the default language, right ? Or is there a way to lock adding files in the ‘EN panel’ the same way than with fields ?
I currently have it running by creating the missing file by hand, maybe there is some cleaner way to do it.
// (... sets $ratio, $color and some variables ...)
$language = $file->page()->content()->language();
// Put all this in an array
$data = ['paddingtop' => $ratio, 'color' => $color];
// Update image metadata the conventional way
$file->update($data, $language);
// Defines some variables for creating the unset .txt
$unsetlanguage = ($language == 'en') ? 'fr' : 'en';
$unsetname = $name . '.' . $extension . '.' . $unsetlanguage . '.txt';
$unsetpath = $file->page()->root() . '/' . $unsetname;
$unsetdata = data::encode($data, 'txt');
// Removing file if it already exists
if (f::exists($unsetpath)) {
f::remove($unsetpath);
}
// (Re-)creates the file
f::write($unsetpath , $unsetdata, true);