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);