-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.php
49 lines (40 loc) · 1.6 KB
/
macros.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
<?php
use Illuminate\Support\HtmlString;
use Illuminate\Support\ViewErrorBag;
/*
* Add a translatable dropdown select field
*
* @param string $name The field name
* @param string $title The field title
* @param object $errors The laravel errors object
* @param string $lang the language of the field
* @param array $choice The choice of the select
* @param null|array $object The entity of the field
*
* @return HtmlString
*/
Form::macro('i18optionSelect', function ($name, $title, ViewErrorBag $errors, $lang, array $choice, $object = null, array $options = []) {
if (array_key_exists('multiple', $options)) {
$nameForm = "{$lang}{$name}[]";
} else {
$nameForm = "{$lang}$name";
}
$string = "<div class='form-group dropdown" . ($errors->has($lang . '.' . $name) ? ' has-error' : '') . "'>";
$string .= "<label for='$nameForm'>$title</label>";
if (is_object($object)) {
$currentData = $object->hasTranslation($lang) ? $object->translate($lang)->{$name} : '';
} else {
$currentData = false;
}
/* Bootstrap default class */
$array_option = ['class' => 'form-control'];
if (array_key_exists('class', $options)) {
$array_option = ['class' => $array_option['class'] . ' ' . $options['class']];
unset($options['class']);
}
$options = array_merge($array_option, $options);
$string .= Form::select($nameForm, $choice, old($nameForm, $currentData), $options);
$string .= $errors->first("{$lang}.{$name}", '<span class="help-block">:message</span>');
$string .= '</div>';
return new HtmlString($string);
});