-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTranslatable.php
166 lines (147 loc) · 4.8 KB
/
Translatable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace YesWeDev\Nova\Translatable;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Fields\Field;
class Translatable extends Field
{
/**
* The field's component.
*
* @var string
*/
public $component = 'translatable';
/**
* Create a new field.
*
* @param string $name
* @param string|null $attribute
* @param mixed|null $resolveCallback
* @return void
*/
public function __construct($name, $attribute = null, $resolveCallback = null)
{
parent::__construct($name, $attribute, $resolveCallback);
$locales = array_map(function ($value) {
return __($value);
}, config('translatable.locales'));
$this->withMeta([
'locales' => $locales,
'indexLocale' => app()->getLocale()
]);
}
/**
* Resolve the given attribute from the given resource.
*
* @param mixed $resource
* @param string $attribute
* @return mixed
*/
protected function resolveAttribute($resource, $attribute)
{
$results = [];
if ( class_exists('\Spatie\Translatable\TranslatableServiceProvider') && method_exists($resource, 'getTranslations') ) {
$results = $resource->getTranslations($attribute);
} elseif ( class_exists('\Astrotomic\Translatable\TranslatableServiceProvider') && method_exists($resource, 'translations') ) {
$results = $resource->translations->pluck($attribute, config('translatable.locale_key'));
} else {
$results = data_get($resource, $attribute);
}
return $results;
}
/**
* Hydrate the given attribute on the model based on the incoming request.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param string $requestAttribute
* @param object $model
* @param string $attribute
* @return void
*/
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
{
if ( class_exists('\Astrotomic\Translatable\TranslatableServiceProvider') && method_exists($model, 'translateOrNew') ) {
if ( is_array($request[$requestAttribute]) ) {
foreach ( $request[$requestAttribute] as $lang => $value ) {
$model->translateOrNew($lang)->{$attribute} = $value;
}
}
} else {
parent::fillAttributeFromRequest($request, $requestAttribute, $model, $attribute);
}
}
/**
* Set the locales to display / edit.
*
* @param array $locales
* @return $this
*/
public function locales(array $locales)
{
return $this->withMeta(['locales' => $locales]);
}
/**
* Set the locale to display on index.
*
* @param string $locale
* @return $this
*/
public function indexLocale($locale)
{
return $this->withMeta(['indexLocale' => $locale]);
}
/**
* Set the input field to a single line text field.
*/
public function singleLine()
{
return $this->withMeta(['singleLine' => true]);
}
/**
* Use Trix Editor.
*/
public function trix()
{
return $this->withMeta(['trix' => true]);
}
/**
* Display the field as raw HTML.
*/
public function asHtml()
{
return $this->withMeta(['asHtml' => true]);
}
/**
* Truncate on Detail Page.
*/
public function truncate()
{
return $this->withMeta(['truncate' => true]);
}
/**
* Override the default isRequired behaviour to support custom required rule
*
* @param NovaRequest $request
* @return mixed
*/
public function isRequired(NovaRequest $request)
{
$requiredRuleName = config('nova-translatable.required_rule_name', 'required');
return with($this->requiredCallback, function ($callback) use ($request, $requiredRuleName) {
if ($callback === true || (is_callable($callback) && call_user_func($callback, $request))) {
return true;
}
if (!empty($this->attribute) && is_null($callback)) {
if ($request->isResourceIndexRequest() || $request->isActionRequest()) {
return in_array($requiredRuleName, $this->getCreationRules($request)[$this->attribute]);
}
if ($request->isCreateOrAttachRequest()) {
return in_array($requiredRuleName, $this->getCreationRules($request)[$this->attribute]);
}
if ($request->isUpdateOrUpdateAttachedRequest()) {
return in_array($requiredRuleName, $this->getUpdateRules($request)[$this->attribute]);
}
}
return false;
});
}
}