|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use BBSLab\NovaTranslation\Models\Contracts\IsTranslatable; |
|
6
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
|
7
|
|
|
use BBSLab\NovaTranslation\Models\Translation as TranslationModel; |
|
8
|
|
|
use BBSLab\NovaTranslation\NovaTranslation; |
|
9
|
|
|
use BBSLab\NovaTranslation\NovaTranslationServiceProvider; |
|
10
|
|
|
use Laravel\Nova\Fields\Field; |
|
11
|
|
|
|
|
12
|
|
|
class Translation extends Field |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The field's component. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
public $component = 'nova-translation-field'; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct($name, $attribute = null, callable $resolveCallback = null) |
|
22
|
|
|
{ |
|
23
|
|
|
$name = trans(NovaTranslationServiceProvider::PACKAGE_ID.'::lang.field'); |
|
24
|
|
|
$attribute = 'translation'; |
|
25
|
|
|
|
|
26
|
|
|
parent::__construct($name, $attribute, $resolveCallback); |
|
27
|
|
|
|
|
28
|
|
|
$this->withMeta([ |
|
29
|
|
|
'locales' => $this->locales(), |
|
30
|
|
|
]); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function resolve($resource, $attribute = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->withMeta([ |
|
39
|
|
|
'translations' => $resource instanceof IsTranslatable ? $this->translations($resource) : [], |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
parent::resolve($resource, $attribute); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Return all indexed locales. |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function locales() |
|
52
|
|
|
{ |
|
53
|
|
|
return NovaTranslation::locales()->mapWithKeys(function (Locale $locale) { |
|
54
|
|
|
return [$locale->getKey() => $locale->toArray()]; |
|
55
|
|
|
})->toArray(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Return translations entries for given translatable model. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $resource |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function translations(IsTranslatable $resource) |
|
65
|
|
|
{ |
|
66
|
|
|
return $resource->translations->mapWithKeys(function (TranslationModel $translation) { |
|
|
|
|
|
|
67
|
|
|
return [ |
|
68
|
|
|
$translation->locale_id => $translation->toArray(), |
|
69
|
|
|
]; |
|
70
|
|
|
})->toArray(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: