diff --git a/src/app/Library/CrudPanel/CrudColumn.php b/src/app/Library/CrudPanel/CrudColumn.php index ea0c5e9747..b69873ba37 100644 --- a/src/app/Library/CrudPanel/CrudColumn.php +++ b/src/app/Library/CrudPanel/CrudColumn.php @@ -106,6 +106,26 @@ public function key(string $key) return $this; } + /** + * When subfields are defined, pass them through the guessing function + * so that they have label, relationship attributes, etc. + * + * @param array $subfields Subfield definition array + * @return self + */ + public function subfields($subfields) + { + $callAttributeMacro = ! isset($this->attributes['subfields']); + $this->attributes['subfields'] = $subfields; + $this->attributes = $this->crud()->makeSureColumnHasNeededAttributes($this->attributes); + + if ($callAttributeMacro) { + $this->callRegisteredAttributeMacros(); + } + + return $this->save(); + } + /** * Remove the current column from the current operation. * diff --git a/src/app/Library/CrudPanel/Traits/Columns.php b/src/app/Library/CrudPanel/Traits/Columns.php index eccb24d53a..d98e386dbe 100644 --- a/src/app/Library/CrudPanel/Traits/Columns.php +++ b/src/app/Library/CrudPanel/Traits/Columns.php @@ -378,7 +378,7 @@ public function makeSureColumnHasNeededAttributes($column) $column = $this->makeSureColumnHasRelationType($column); $column = $this->makeSureColumnHasType($column); $column = $this->makeSureColumnHasPriority($column); - $column = $this->makeSureSubfieldsHaveNecessaryAttributes($column); + $column = $this->makeSureSubfieldsHaveNecessaryAttributes($column, false); // check if the column exists in the database (as a db column) $column_exists_in_db = $this->hasDatabaseColumn($this->model->getTable(), $column['name']); diff --git a/src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php b/src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php index 376c2ed6cf..d8d05de918 100644 --- a/src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php +++ b/src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php @@ -254,7 +254,7 @@ protected function inferFieldTypeFromRelationType($relationType) * @param array $field Field definition array. * @return array The improved definition of that field (a better 'subfields' array) */ - protected function makeSureSubfieldsHaveNecessaryAttributes($field) + protected function makeSureSubfieldsHaveNecessaryAttributes($field, $includeRelationFields = true) { if (! isset($field['subfields']) || ! is_array($field['subfields'])) { return $field; @@ -299,7 +299,7 @@ protected function makeSureSubfieldsHaveNecessaryAttributes($field) // when field has any of `many` relations we need to append either the pivot selector for the `ToMany` or the // local key for the `many` relations. Other relations don't need any special treatment when used as subfields. - if (isset($field['relation_type'])) { + if (isset($field['relation_type']) && $includeRelationFields) { switch ($field['relation_type']) { case 'MorphToMany': case 'BelongsToMany':