You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds the ability to insert translations in content using a language table.
5
-
For example you can create a main model that stores the object id and the data that is not translatable, on the lang model you have to define the foreign key for the main object(in this case post_id) and the one for the language that has always to be "language_id".
5
+
For example, you can create a main model that stores the object id and the data that is not translatable, on the lang model you have to define the foreign key for the main object(in this case post_id) and the one for the language that has always to be "language_id".
6
6
Below an example of model definition in YAML for blueprint package:
7
7
```yaml
8
8
Post:
@@ -20,7 +20,6 @@ Below an example of model definition in YAML for blueprint package:
20
20
relationships:
21
21
belongsTo: Language
22
22
```
23
-
24
23
## INSTALLATION
25
24
26
25
Simply install using composer
@@ -54,95 +53,104 @@ class AdminPanelProvider extends PanelProvider
54
53
}
55
54
```
56
55
57
-
## MAIN MODEL
56
+
## HOW TO USE IT
58
57
59
-
In the main model extend the FmtModel:
58
+
### MAIN MODEL
59
+
In the main model use the provided trait "HasTranslation":
60
60
61
61
```php
62
-
use Unusualdope\FilamentModelTranslatable\Models\FmtModel;
62
+
use Unusualdope\FilamentModelTranslatable\Traits\HasTranslation;
63
63
64
-
class Post extends FmtModel
64
+
class Post extends Model
65
65
{
66
+
use HasTranslation;
67
+
}
66
68
//...
67
69
```
68
-
69
-
Define some properties to make the plugin work, see the example with comments:
70
+
The plugin assumes that the translatable model is named as the main model + "Language" and the foreign key is the main model name in CamelCase + "Language" (e.g. PostLanguage)
'title' => [ //field name that will match with the LangModel db field and property
81
-
'formType' => 'TextInput', //The type of form input field as in Filament
82
-
'name' => 'Title', //Field Label
83
-
'methods' => [ //The methods you want to call from filament on your field to define it
84
-
'required' => '1',
85
-
'prefix' => 'title',
86
-
...
87
-
'anotherMethod' => [
88
-
'param1' => '1',
89
-
'param2' => 'test'
90
-
...
91
-
'paramN' => 'xxx'
92
-
]
93
-
],
94
-
],
95
-
'content' => [
96
-
'formType' => 'RichEditor',
97
-
'name' => 'Content',
98
-
'methods' => [
99
-
'columnSpanFull' => '',
100
-
],
101
-
],
102
-
];
80
+
protected $lang_model = 'App\Models\PostTranslated'; //fqn of the translatable model
103
81
```
82
+
the plugin assumes that the foreign key is the standard laravel foreign key
83
+
(eg: post_id), if you want to change it you can do so overwriting the property in your main model
84
+
```php
85
+
/**
86
+
* Specify the foreign key if not the standard one
87
+
*/
104
88
105
-
## RESOURCE
89
+
protected $lang_foreign_key = 'post_ext_id';
90
+
```
91
+
if for any reason you want to stop/pause the translat-ability of your model
92
+
you can set the property $is_translatable to false
93
+
```php
94
+
/**
95
+
* Set to false in order to disable the translatable feature
96
+
*/
106
97
107
-
In the RESOURCE you have to use the Trait fmtTrait and retrieve the translatable fields with
98
+
protected bool $is_translatable = false;
99
+
```
100
+
On the main model you have to define the fields that will be translatable using standard Filament fields
101
+
as you would do in a resource, specify them in method setTranslatableFilamentFields(), the make() method
102
+
has to contain the field names that are present in the database on the <Model>Language table
108
103
109
104
```php
110
-
self::addTranslatableFieldsToSchema(array $schema, Form $form, Bool false);
111
-
```
105
+
use Filament\Forms\Components\Textarea;
106
+
use Filament\Forms\Components\TextInput;
107
+
108
+
public function setTranslatableFilamentFields()
109
+
{
110
+
return [
111
+
TextInput::make('name')
112
+
->required()
113
+
->label('Name'),
114
+
TextInput::make('link_rewrite')
115
+
->maxLength(128)
116
+
->label('Link Rewrite'),
117
+
TextInput::make('meta_title')
118
+
->maxLength(128)
119
+
->label('Meta Title'),
120
+
Textarea::make('meta_description')
121
+
->maxLength(512)
122
+
->label('Meta Description'),
123
+
//...
124
+
];
125
+
}
126
+
```
112
127
113
-
1 - As first parameter you can pass the current schema and it will give you back the full schema with the translatable fields appended at the end.
128
+
### RESOURCE
114
129
115
-
2 - the Form object
130
+
In the RESOURCE when defining the form insert the translatable fields where you want
131
+
by using the method addTranslatableFieldsToSchema() and passing as parameter the field name
132
+
exactly as defined in the database and in the main model
116
133
117
-
3 - If you want back only the array containing the schema of the translatable fields (to allow you to place it in the middle of your schema) set this to false (default is true)
The trait HasTranslation provides a method to retrieve the translated data for the current language
193
+
by using the defined HasOne relationship "currentLanguage", if you want to retrieve the post name in the
194
+
current language in frontend you can do so by using the following code:
195
+
196
+
```php
197
+
$post = Post::find(1);
198
+
$post->currentLanguage->name;
199
+
```
200
+
201
+
if you need to access all the data for all the languages
202
+
you can use the HasMany relationship "languageData", you can do so by using the following code:
203
+
204
+
```php
205
+
$post = Post::find(1);
206
+
$post->languageData;
207
+
```
181
208
182
209
## ISSUES OR SUGGESTIONS
183
210
184
-
Please feel free to give any suggestions for improvements or report any issue directly on the github [plugin repository](https://github.com/geimsdin/filament-model-translatable)
211
+
Please feel free to give any suggestions for improvements or
212
+
report any issue directly on the gitHub [plugin repository](https://github.com/geimsdin/filament-model-translatable)
0 commit comments