|
1
|
|
|
<?php namespace Propaganistas\LaravelTranslatableBootForms\Form; |
|
2
|
|
|
|
|
3
|
|
|
use AdamWathan\Form\Binding\BoundData; |
|
4
|
|
|
use AdamWathan\Form\Elements\Label; |
|
5
|
|
|
use AdamWathan\Form\FormBuilder as _FormBuilder; |
|
6
|
|
|
|
|
7
|
|
|
class FormBuilder extends _FormBuilder |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Array of locale keys. |
|
12
|
|
|
* |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $locales; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Sets the available locales for translatable fields. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $locales |
|
21
|
|
|
*/ |
|
22
|
39 |
|
public function setLocales(array $locales) |
|
23
|
|
|
{ |
|
24
|
39 |
|
$this->locales = $locales; |
|
25
|
39 |
|
} |
|
26
|
|
|
|
|
27
|
18 |
|
public function bind($data) |
|
28
|
|
|
{ |
|
29
|
18 |
|
$this->boundData = new BoundData($data); |
|
30
|
18 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Getting value from Model or ModelTranslation to populate form. |
|
34
|
|
|
* Courtesy of TypiCMS/TranslatableBootForms (https://github.com/TypiCMS/TranslatableBootForms/blob/master/src/TranslatableFormBuilder.php) |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $name key |
|
37
|
|
|
* @return string value |
|
38
|
|
|
*/ |
|
39
|
6 |
|
protected function getBoundValue($name, $default) |
|
40
|
|
|
{ |
|
41
|
6 |
|
$inputName = preg_split('/[\[\]]+/', $name, - 1, PREG_SPLIT_NO_EMPTY); |
|
42
|
6 |
|
if (config('app.translator') === 'spatie') { |
|
43
|
3 |
|
if (count($inputName) == 2 && in_array($inputName[1], $this->locales)) { |
|
44
|
3 |
|
list($name, $lang) = $inputName; |
|
45
|
3 |
|
$translation = $this->boundData->data()->getTranslation($name, $lang); |
|
46
|
3 |
|
$value = $translation ? : ''; |
|
47
|
|
|
|
|
48
|
3 |
|
return $this->escape($value); |
|
49
|
|
|
} |
|
50
|
|
|
} else { |
|
51
|
3 |
|
if (count($inputName) == 2 && in_array($inputName[0], $this->locales)) { |
|
52
|
|
|
list($lang, $name) = $inputName; |
|
53
|
|
|
$value = isset($this->boundData->data()->translate($lang)->{$name}) |
|
54
|
|
|
? $this->boundData->data()->translate($lang)->{$name} |
|
55
|
|
|
: ''; |
|
56
|
|
|
|
|
57
|
|
|
return $this->escape($value); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->escape($this->boundData->get($name, $default)); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|