diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index d8e2e1304..e3d973130 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -45,7 +45,7 @@ class Form extends WidgetBase /** * @var array Collection of all fields used in this form. */ - protected $allFields = []; + protected $fields = []; /** * @var array Collection of all form widgets used in this form. @@ -179,10 +179,10 @@ class Form extends WidgetBase public function renderField($field, $options = []) { if (is_string($field)) { - if (!isset($this->allFields[$field])) + if (!isset($this->fields[$field])) throw new ApplicationException(Lang::get('backend::lang.form.missing_definition', compact('field'))); - $field = $this->allFields[$field]; + $field = $this->fields[$field]; } if (!isset($options['useContainer'])) $options['useContainer'] = true; @@ -241,7 +241,7 @@ class Form extends WidgetBase $this->model->fill($data); $this->data = (object) array_merge((array) $this->data, (array) $data); - foreach ($this->allFields as $field) + foreach ($this->fields as $field) $field->value = $this->getFieldValue($field); return $data; @@ -274,10 +274,10 @@ class Form extends WidgetBase if (($updateFields = post('fields')) && is_array($updateFields)) { foreach ($updateFields as $field) { - if (!isset($this->allFields[$field])) + if (!isset($this->fields[$field])) continue; - $fieldObject = $this->allFields[$field]; + $fieldObject = $this->fields[$field]; $result['#' . $fieldObject->getId('group')] = $this->makePartial('field', ['field' => $fieldObject]); } } @@ -357,7 +357,7 @@ class Form extends WidgetBase /* * Bind all form widgets to controller */ - foreach ($this->allFields as $field) { + foreach ($this->fields as $field) { if ($field->type != 'widget') continue; @@ -413,7 +413,7 @@ class Form extends WidgetBase continue; } - $this->allFields[$name] = $fieldObj; + $this->fields[$name] = $fieldObj; switch (strtolower($addToArea)) { case 'primary': @@ -589,7 +589,7 @@ class Form extends WidgetBase */ public function getFields() { - return $this->allFields; + return $this->fields; } /** @@ -599,7 +599,7 @@ class Form extends WidgetBase */ public function getField($field) { - return $this->allFields[$field]; + return $this->fields[$field]; } /** @@ -621,10 +621,10 @@ class Form extends WidgetBase public function getFieldValue($field) { if (is_string($field)) { - if (!isset($this->allFields[$field])) + if (!isset($this->fields[$field])) throw new ApplicationException(Lang::get('backend::lang.form.missing_definition', compact('field'))); - $field = $this->allFields[$field]; + $field = $this->fields[$field]; } $columnName = $field->columnName; @@ -694,7 +694,7 @@ class Form extends WidgetBase * Boolean fields (checkbox, switch) won't be present value FALSE * Number fields should be converted to integers */ - foreach ($this->allFields as $field) { + foreach ($this->fields as $field) { if (!in_array($field->type, ['switch', 'checkbox', 'number'])) continue; diff --git a/modules/backend/widgets/Lists.php b/modules/backend/widgets/Lists.php index b157ea729..67e5eaaba 100644 --- a/modules/backend/widgets/Lists.php +++ b/modules/backend/widgets/Lists.php @@ -46,7 +46,7 @@ class Lists extends WidgetBase protected $visibleColumns; /** - * @var array All available columns. + * @var array Collection of all list columns used in this list. */ protected $columns; @@ -402,15 +402,8 @@ class Lists extends WidgetBase * Apply sorting */ if ($sortColumn = $this->getSortColumn()) { - // Determine if the column has an sqlSelect - foreach ($this->getListColumns() as $column) { - if ($column->columnName == $sortColumn) { - if ($column->sqlSelect) { - $sortColumn = $column->sqlSelect; - } - break; - } - } + if ($column = array_get($this->columns, $sortColumn) && $column->sqlSelect) + $sortColumn = $column->sqlSelect; $query->orderBy($sortColumn, $this->sortDirection); } @@ -488,12 +481,31 @@ class Lists extends WidgetBase return Html::attributes(['onclick' => $recordOnClick]); } + /** + * Get all the registered columns for the instance. + * @return array + */ + public function getColumns() + { + return $this->columns ?: $this->defineListColumns(); + } + + /** + * Get a specified column object + * @param string $column + * @return mixed + */ + public function getColumn($column) + { + return $this->columns[$column]; + } + /** * Returns the list columns that are visible by list settings or default */ protected function getVisibleListColumns() { - $definitions = $this->getListColumns(); + $definitions = $this->defineListColumns(); $columns = []; /* @@ -531,7 +543,7 @@ class Lists extends WidgetBase /** * Builds an array of list columns with keys as the column name and values as a ListColumn object. */ - protected function getListColumns() + protected function defineListColumns() { if (!isset($this->config->columns) || !is_array($this->config->columns) || !count($this->config->columns)) throw new ApplicationException(Lang::get('backend::lang.list.missing_columns', ['class'=>get_class($this->controller)])); @@ -832,7 +844,7 @@ class Lists extends WidgetBase */ protected function getSearchableColumns() { - $columns = $this->columns ?: $this->getListColumns(); + $columns = $this->getColumns(); $searchable = []; foreach ($columns as $column) { @@ -942,7 +954,7 @@ class Lists extends WidgetBase if ($this->sortableColumns !== null) return $this->sortableColumns; - $columns = $this->columns ?: $this->getListColumns(); + $columns = $this->getColumns(); $sortable = []; foreach ($columns as $column) { @@ -1006,12 +1018,12 @@ class Lists extends WidgetBase /* * Force all columns invisible */ - $allColumns = $this->getListColumns(); - foreach ($allColumns as $column) { + $columns = $this->defineListColumns(); + foreach ($columns as $column) { $column->invisible = true; } - return array_merge($allColumns, $this->getVisibleListColumns()); + return array_merge($columns, $this->getVisibleListColumns()); } //