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

Prevent configuration from breaking on problematic Resources #45

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 23 additions & 18 deletions src/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,33 @@ public function review(NovaRequest $request, string $file): Response

protected function getAvailableFieldsForImport(string $resource, NovaRequest $request): array
{
$novaResource = new $resource(new $resource::$model);
$fieldsCollection = collect($novaResource->creationFields($request));

if (method_exists($novaResource, 'excludeAttributesFromImport')) {
$fieldsCollection = $fieldsCollection->filter(function(Field $field) use ($novaResource, $request) {
return !in_array($field->attribute, $novaResource::excludeAttributesFromImport($request));
try {
$novaResource = new $resource(new $resource::$model);
$fieldsCollection = collect($novaResource->creationFields($request));

if (method_exists($novaResource, 'excludeAttributesFromImport')) {
$fieldsCollection = $fieldsCollection->filter(function(Field $field) use ($novaResource, $request) {
return !in_array($field->attribute, $novaResource::excludeAttributesFromImport($request));
});
}

$fields = $fieldsCollection->map(function (Field $field) use ($novaResource, $request) {
return [
'name' => $field->name,
'attribute' => $field->attribute,
'rules' => $this->extractValidationRules($novaResource, $request)->get($field->attribute),
];
});
}

$fields = $fieldsCollection->map(function (Field $field) use ($novaResource, $request) {
// Note: ->values() is used here to avoid this array being turned into an object due to
// non-sequential keys (which might happen due to the filtering above.
return [
'name' => $field->name,
'attribute' => $field->attribute,
'rules' => $this->extractValidationRules($novaResource, $request)->get($field->attribute),
$novaResource->uriKey() => $fields->values(),
];
});
} catch (\Throwable $th) {
return [];
}

// Note: ->values() is used here to avoid this array being turned into an object due to
// non-sequential keys (which might happen due to the filtering above.
return [
$novaResource->uriKey() => $fields->values(),
];
}

protected function getAvailableResourcesForImport(NovaRequest $request): Collection
Expand Down