Skip to content

Commit 3322106

Browse files
committed
Add support for 'container' attributes
1 parent 33af3d3 commit 3322106

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/AttributeManager.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,21 @@ public function getExtraAttributes(View $view)
7979

8080
$valueRequiredBooleans = ['data-allow-clear'];
8181

82+
return $this->mapAttributesToString($extra, $valueRequiredBooleans);
83+
84+
}
85+
86+
public function getContainerAttributes(View $view)
87+
{
88+
$extra = $view->offsetExists('container') ? $view->offsetGet('container') : [];
89+
return $this->mapAttributesToString($extra);
90+
91+
}
92+
93+
protected function mapAttributesToString($attributes, $valueRequiredBooleans = []) {
94+
8295
// Convert extra properties to key="value" string
83-
$attributes = array_map(function ($key, $value) use ($valueRequiredBooleans) {
96+
$output = array_map(function ($key, $value) use ($valueRequiredBooleans) {
8497
// ['required']
8598
if (is_int($key) || is_string($key) && ctype_digit($key)) {
8699
return in_array($value, $valueRequiredBooleans) ? "{$value}=\"1\"" : $value;
@@ -100,14 +113,14 @@ public function getExtraAttributes(View $view)
100113
if (is_object($value) || is_array($value)) {
101114
$value = json_encode($value);
102115
}
103-
116+
104117
return sprintf('%s="%s"', $key, htmlspecialchars($value));
105-
}, array_keys($extra), $extra);
118+
}, array_keys($attributes), $attributes);
106119

107120
// Remove empty (boolean false) attributes
108-
$attributes = array_filter($attributes);
121+
$output = array_filter($output);
109122

110-
return implode(' ', $attributes);
123+
return implode(' ', $output);
111124
}
112125

113126
public function getFieldTemplate(View $view)

src/FieldViewComposer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function compose(View $view)
3030

3131
// Repopulate the template
3232
$view->offsetSet('extraAttributes', $this->attributeManager->getExtraAttributes($view));
33+
$view->offsetSet('containerAttributes', $this->attributeManager->getContainerAttributes($view));
3334
$view->offsetSet('fieldTemplate', $this->attributeManager->getFieldTemplate($view));
3435
$view->offsetSet('value', $this->attributeManager->getFieldValue($view));
3536
$view->offsetSet('type', $this->attributeManager->getFieldType($view));

views/container.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<input type="hidden" name="{{ $name }}" value="{{ $value }}">
33
@else
44
{!! isset($col) ? '<div class="'.$col.'">' : '' !!}
5-
<div class="form-group" @if($errors->has($name)) data-server-error="{{ $errors->first($name) }}" @endif>
5+
<div class="form-group" @if($errors->has($name)) data-server-error="{{ $errors->first($name) }}" @endif {!! $containerAttributes !!}>
66
@unless(($onlyTemplate ?? false) || ($label ?? true) === false)
77
<label for="{{ $name }}">
88
@if($rawLabel ?? false)

0 commit comments

Comments
 (0)