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

Allow other fields to use subfields attribute (not just checklist_dependency) #4069

Merged
Merged
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
11 changes: 9 additions & 2 deletions src/app/Library/CrudPanel/Traits/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,18 @@ public function getRelationFields()
array_push($relationFields, $field);
}

if (isset($field['subfields']) &&
// if a field has an array name AND subfields
// then take those fields into account (check if they have relationships);
// this is done in particular for the checklist_dependency field,
// but other fields could use it too, in the future;
if (is_array($field['name']) &&
isset($field['subfields']) &&
is_array($field['subfields']) &&
count($field['subfields'])) {
foreach ($field['subfields'] as $subfield) {
array_push($relationFields, $subfield);
if (isset($subfield['model']) && $subfield['model'] !== false) {
array_push($relationFields, $subfield);
}
}
}
}
Expand Down
25 changes: 8 additions & 17 deletions src/app/Library/CrudPanel/Traits/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,7 @@ public function getUpdateFields($id = false)
$entry = ($id != false) ? $this->getEntry($id) : $this->getCurrentEntry();

foreach ($fields as &$field) {
// set the value
if (! isset($field['value'])) {
if (isset($field['subfields'])) {
$field['value'] = [];
foreach ($field['subfields'] as $subfield) {
$field['value'][] = $entry->{$subfield['name']};
}
} else {
$field['value'] = $this->getModelAttributeValue($entry, $field);
}
}
$field['value'] = $field['value'] ?? $this->getModelAttributeValue($entry, $field);
}

// always have a hidden input for the entry id
Expand Down Expand Up @@ -96,8 +86,8 @@ private function getModelAttributeValue($model, $field)

if (is_array($field['name'])) {
$result = [];
foreach ($field['name'] as $key => $value) {
$result = $model->{$value};
foreach ($field['name'] as $name) {
$result[] = $model->{$name};
}

return $result;
Expand All @@ -119,11 +109,12 @@ private function getModelAttributeValueFromRelationship($model, $field)
case 'HasMany':
case 'BelongsToMany':
case 'MorphToMany':
if (! isset($field['pivotFields'])) {
// use subfields aka. pivotFields
if (! isset($field['subfields'])) {
return $related_model->{$relation_method};
}
// we want to exclude the "self pivot field" since we already have it.
$pivot_fields = Arr::where($field['pivotFields'], function ($item) use ($field) {
$pivot_fields = Arr::where($field['subfields'], function ($item) use ($field) {
return $field['name'] != $item['name'];
});
$related_models = $related_model->{$relation_method};
Expand Down Expand Up @@ -174,9 +165,9 @@ private function getModelAttributeValueFromRelationship($model, $field)
return $related_entry->{Str::afterLast($field['entity'], '.')};
}

if ($field['fields']) {
if ($field['subfields']) {
$result = [];
foreach ($field['fields'] as $subfield) {
foreach ($field['subfields'] as $subfield) {
$name = is_string($subfield) ? $subfield : $subfield['name'];
$result[$name] = $related_entry->{$name};
}
Expand Down
7 changes: 4 additions & 3 deletions src/resources/views/crud/fields/date_range.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ function formatDate($entry, $dateFieldName)
return $formattedDate;
}
}

if (isset($field['value'])) {
if (isset($entry) && ! is_array($field['value'])) {
$start_value = formatDate($entry, $field['name'][0]);
$end_value = formatDate($entry, $field['name'][1]);
} elseif (is_array($field['value'])) {
$start_value = $field['value'][$field['name'][0]];
$end_value = $field['value'][$field['name'][1]];
} elseif (is_array($field['value'])) { // gets here when inside repeatable
$start_value = current($field['value']); // first array item
$end_value = next($field['value']); // second array item
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/resources/views/crud/fields/inc/repeatable_row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@endif

<div class="col-md-12 well repeatable-element row m-1 p-2" data-repeatable-identifier="{{ $field['name'] }}">
@if (isset($field['fields']) && is_array($field['fields']) && count($field['fields']))
@if (isset($field['subfields']) && is_array($field['subfields']) && count($field['subfields']))
<div class="controls">
<button type="button" class="close delete-element"><span aria-hidden="true">×</span></button>
@if ($field['reorder'])
Expand All @@ -16,7 +16,7 @@
</button>
@endif
</div>
@foreach($field['fields'] as $subfield)
@foreach($field['subfields'] as $subfield)
@php
// make sure the field definition is an array
if (is_string($subfield)) {
Expand Down
7 changes: 4 additions & 3 deletions src/resources/views/crud/fields/relationship.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// if field is not ajax but user wants to use InlineCreate
// we make minimum_input_length = 0 so when user open we show the entries like a regular select
$field['minimum_input_length'] = ($field['ajax'] !== true) ? 0 : ($field['minimum_input_length'] ?? 2);
$field['subfields'] = $field['subfields'] ?? false;

switch($field['relation_type']) {
case 'HasOne':
Expand All @@ -44,7 +45,7 @@
// The dev is trying to create a field for the ENTIRE hasOne/morphOne relationship
// -----
// if "subfields" is not defined, tell the dev to define it (+ link to docs)
if (!isset($field['fields'])) {
if (!is_array($field['subfields'])) {
abort(500, "<strong>Please define <code>subfields</code> on your <code>{$field['model']}</code> field.</strong><br>That way, you can allow the admin to edit the attributes on that related entry (through the hasOne relationship).<br>See <a target='_blank' href='https://backpackforlaravel.com/docs/crud-fields#crud-how-to#hasone-1-1-relationship'>the docs</a> for more information.");
}
// if "subfields" is defined, load a repeatable field with one entry (and 1 entry max)
Expand All @@ -54,7 +55,7 @@
case 'BelongsToMany':
case 'MorphToMany':
// if there are pivot fields we show the repeatable field
if(isset($field['pivotFields'])) {
if(is_array($field['subfields'])) {
$field['type'] = 'relationship.entries';
break;
}
Expand All @@ -80,7 +81,7 @@
$field['force_delete'] = $field['force_delete'] ?? false;

// if there are pivot fields we show the repeatable field
if(isset($field['pivotFields'])) {
if(is_array($field['subfields'])) {
$field['type'] = 'relationship.entries';
} else {
// we show a regular/ajax select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

@php
$field['type'] = 'repeatable';
$field['fields'] = $field['pivotFields'];
//each row represent a related entry in a database table. We should not "auto-add" one relationship if it's not the user intention.
$field['init_rows'] = 0;
$field['subfields'] = $field['subfields'] ?? [];
$field['reorder'] = $field['reorder'] ?? false;

$pivotSelectorField = $field['pivotSelect'] ?? [];
Expand All @@ -27,12 +29,12 @@
switch ($field['relation_type']) {
case 'MorphToMany':
case 'BelongsToMany':
$field['fields'] = Arr::prepend($field['fields'], $pivotSelectorField);
$field['subfields'] = Arr::prepend($field['subfields'], $pivotSelectorField);
break;
case 'MorphMany':
case 'HasMany':
if(isset($entry)) {
$field['fields'] = Arr::prepend($field['fields'], [
$field['subfields'] = Arr::prepend($field['subfields'], [
'name' => $entry->{$field['name']}()->getLocalKeyName(),
'type' => 'hidden',
]);
Expand Down
1 change: 1 addition & 0 deletions src/resources/views/crud/fields/repeatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
$field['max_rows'] = $field['max_rows'] ?? 0;
$field['min_rows'] = $field['min_rows'] ?? 0;
$field['reorder'] = $field['reorder'] ?? true;
$field['subfields'] = $field['subfields'] ?? $field['fields'] ?? [];
@endphp

@include('crud::fields.inc.wrapper_start')
Expand Down