Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][v7] - guess column subfields attributes #5611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/app/Library/CrudPanel/CrudColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/app/Library/CrudPanel/Traits/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
4 changes: 2 additions & 2 deletions src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand Down