diff --git a/CHANGELOG.md b/CHANGELOG.md index 300d3f690..e2ed0d4a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ * **Build 14x** (2014-09-xx) - Added new `hint` form field type (see Backend > Forms docs). + - Added new `containerAttributes` property to form fields (see Backend > Forms docs). * **Build 145** (2014-09-13) - Standard setting pages now have **Save** and **Save and Close** buttons. diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 68ae63e67..6646b71fd 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -1,6 +1,7 @@ placeholder = $config['placeholder']; if (isset($config['default'])) $this->defaults = $config['default']; if (isset($config['cssClass'])) $this->cssClass = $config['cssClass']; - if (isset($config['attributes'])) $this->attributes = $config['attributes']; + if (isset($config['attributes'])) $this->attributes($config['attributes']); + if (isset($config['containerAttributes'])) $this->attributes($config['containerAttributes'], 'container'); if (isset($config['depends'])) $this->depends = $config['depends']; if (isset($config['path'])) $this->path = $config['path']; @@ -266,6 +268,41 @@ class FormField return $this; } + /** + * Sets the attributes for this field in a given position. + * - field: Attributes are added to the form field element (input, select, textarea, etc) + * - container: Attributes are added to the form field container (div.form-group) + * @param array $items + * @param strubg $position + * @return void + */ + public function attributes($items, $position = 'field') + { + if (!is_array($items)) + return; + + $multiArray = array_filter($items, 'is_array'); + if (!$multiArray) { + $this->attributes[$position] = $items; + return; + } + + foreach ($items as $_position => $_items) { + $this->attributes($_items, $_position); + } + } + + /** + * Returns the attributes for this field at a given position. + * @param string $position + * @return array + */ + public function getAttributes($position = 'field', $htmlBuild = true) + { + $result = array_get($this->attributes, $position, []); + return $htmlBuild ? HTML::attributes($result) : $result; + } + /** * Returns a value suitable for the field name property. * @param string $arrayName Specify a custom array name diff --git a/modules/backend/formwidgets/Relation.php b/modules/backend/formwidgets/Relation.php index 6a38dfbc2..436d45339 100644 --- a/modules/backend/formwidgets/Relation.php +++ b/modules/backend/formwidgets/Relation.php @@ -116,7 +116,8 @@ class Relation extends FormWidgetBase // by joining its pivot table. Remove all joins from the query. $query->getQuery()->getQuery()->joins = []; - if (in_array('October\Rain\Database\Traits\NestedTree', class_uses($relatedObj))) + $treeTraits = ['October\Rain\Database\Traits\NestedTree', 'October\Rain\Database\Traits\SimpleTree']; + if (count(array_intersect($treeTraits, class_uses($relatedObj))) > 0) $field->options = $query->listsNested($this->nameFrom, $relatedObj->getKeyName()); else $field->options = $query->lists($this->nameFrom, $relatedObj->getKeyName()); diff --git a/modules/backend/widgets/form/partials/_field-container.htm b/modules/backend/widgets/form/partials/_field-container.htm index d825e1cbb..458705560 100644 --- a/modules/backend/widgets/form/partials/_field-container.htm +++ b/modules/backend/widgets/form/partials/_field-container.htm @@ -2,6 +2,7 @@ class="form-group previewMode ? 'form-group-preview' : '' ?> type ?>-field span-span ?> required?'is-required':'' ?> stretch?'layout-relative':'' ?> cssClass ?>" getFieldDepends($field)): ?>data-field-depends="" data-field-name="fieldName ?>" + getAttributes('container') ?> id="getId('group') ?>"> makePartial('field', ['field' => $field]) ?> \ No newline at end of file diff --git a/modules/backend/widgets/form/partials/_field_balloon-selector.htm b/modules/backend/widgets/form/partials/_field_balloon-selector.htm index 3383261e6..2fa9620c4 100644 --- a/modules/backend/widgets/form/partials/_field_balloon-selector.htm +++ b/modules/backend/widgets/form/partials/_field_balloon-selector.htm @@ -2,7 +2,7 @@ $fieldOptions = $field->options(); ?> -
attributes) ?>> +
getAttributes() ?>>