Translatable Support for File Upload Form Widget #86
Replies: 7 comments 3 replies
-
You mean being able to use the fileupload formwidget to upload files per language ? |
Beta Was this translation helpful? Give feedback.
-
I use it myself with custom modifications to upload images for different locales. |
Beta Was this translation helpful? Give feedback.
-
I have this class: <?php namespace StudioAzura\Foundation\Classes;
use App;
use Config;
use Event;
use Lang;
use System\Models\File as FileModel;
class ExtendWinterTranslate
{
public static function boot()
{
\Winter\Storm\Database\Model::extend(function ($model) {
if ($model->attachOne) {
foreach ($model->{'attachOne'} as $relationName => $options) {
if (App::runningInBackend() && $options[0] === 'System\Models\File' && isset($options['translatable']) && !isset($model->attachMany[$relationName])) {
unset($model->attachOne[$relationName]);
$model->addAttachManyRelation($relationName, $options);
}
}
}
});
Event::listen('backend.form.extendFields', function ($widget) {
if ($widget->model instanceof FileModel) {
$widget->addFields([
'locale' => [
'label' => 'Locale',
'type' => 'dropdown',
'options' => 'StudioAzura\Foundation\Classes\Tools::getLocaleOptions',
'emptyOption' => Lang::get('studioazura.foundation::lang.labels.locale.emptyOption'),
],
]);
}
});
FileModel::extend(function ($model) {
$model->bindEvent('model.beforeSave', function () use ($model) {
if (empty($model->locale)) {
$model->locale = null;
}
});
if (!App::runningInBackend()) {
$model::addGlobalScope(new Localized);
}
});
}
} |
Beta Was this translation helpful? Give feedback.
-
This scope: <?php namespace StudioAzura\Foundation\Scopes;
use App;
use Illuminate\Database\Eloquent\Scope;
class Localized implements Scope
{
public function apply($builder, $model)
{
$wheres = $builder->getQuery()->wheres;
$attachmentId = $model->qualifyColumn('attachment_id');
$attachmentType = $model->qualifyColumn('attachment_type');
$columnValues = array_values(array_column($wheres, 'column'));
if (in_array($attachmentId, $columnValues) && in_array($attachmentType, $columnValues)) {
$builder->whereNull('locale')->orWhere('locale','')->orWhere('locale', $this->getLocale());
}
}
protected function getLocale()
{
if (!$locale = App::getLocale()) {
return 'en';
}
if (str_contains($locale, '-')) {
$locale = explode('-', $locale)[0];
}
return $locale;
}
} |
Beta Was this translation helpful? Give feedback.
-
And this is the method that fetches the different locales: <?php namespace StudioAzura\Foundation\Classes;
use Winter\Translate\Models\Locale;
class Tools
{
public static function getLocaleOptions($locale = null)
{
$locales = [];
if (class_exists(Locale::class)) {
$locales = Locale::listEnabled();
}
if (is_string($locale)) {
return array_get($locales, $locale, $locale);
} else {
return $locales;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
And this is the migration for the system_files table: <?php namespace StudioAzura\Foundation\Updates;
use Schema;
use Winter\Storm\Database\Updates\Migration;
class SystemFilesAddLocale extends Migration
{
public function up()
{
Schema::table('system_files', function ($table) {
$table->string('locale')->nullable();
});
}
public function down()
{
Schema::table('system_files', function ($table) {
$table->dropColumn('locale');
});
}
} |
Beta Was this translation helpful? Give feedback.
-
Many thanks, this helps me a lot! |
Beta Was this translation helpful? Give feedback.
-
Subject: Request for Support of File Upload Form Widget
I would like to raise a concern regarding the lack of support for the file upload form widget. As a user, I find the
mediafinder
to be more complicated to use. It requires me to upload a file through the media finder and then select and add it, which becomes tedious, especially when switching between language options.If I have to repeat this process for each language—imagine if there are 10 fields—it becomes increasingly cumbersome.
If possible, I would greatly appreciate an upgrade to the file upload form widget to provide better support for this functionality.
Thank you for considering my request!
Beta Was this translation helpful? Give feedback.
All reactions