Skip to content

Allow removing options that EntityType can't accept #976

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions src/Autocomplete/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## Next

- Allow not passing custom options to the nested `EntityType` by passing a list through another custom option
called `autocomplete_excluded_options` - #420

## 2.9.0

- Add support for symfony/asset-mapper
Expand Down
28 changes: 26 additions & 2 deletions src/Autocomplete/src/Form/AutocompleteEntityTypeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ public function preSetData(FormEvent $event)
// pass to AutocompleteChoiceTypeExtension
$options['autocomplete'] = true;
$options['autocomplete_url'] = $this->autocompleteUrl;
unset($options['searchable_fields'], $options['security'], $options['filter_query']);

$form->add('autocomplete', EntityType::class, $options);
$form->add('autocomplete', EntityType::class, $this->withoutExcludedOptions($options));
}

public function preSubmit(FormEvent $event)
Expand Down Expand Up @@ -104,4 +103,29 @@ public static function getSubscribedEvents(): array
FormEvents::PRE_SUBMIT => 'preSubmit',
];
}

/**
* If you create a custom Form Type based on {@link ParentEntityAutocompleteType} and make it accept any non-standard
* options, these options need to be removed from the $options array passed to EntityType because it doesn't
* accept those custom options.
*
* @see https://github.com/symfony/ux/issues/420
*/
private function withoutExcludedOptions(array $options): array
{
$optionBlacklist = [
// Options added by ParentEntityAutocompleteType
'searchable_fields', 'security', 'filter_query', 'property',
// Any other options that were added to the custom AutocompleteType that EntityType doesn't understand
...$options['autocomplete_excluded_options'],
// The list of blacklisted options itself (EntityType doesn't understand that either!)
'autocomplete_excluded_options',
];

foreach ($optionBlacklist as $option) {
unset($options[$option]);
}

return $options;
}
}
2 changes: 2 additions & 0 deletions src/Autocomplete/src/Form/ParentEntityAutocompleteType.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public function configureOptions(OptionsResolver $resolver)
'security' => false,
// set the max results number that a query on automatic endpoint return.
'max_results' => 10,
/* @see AutocompleteEntityTypeSubscriber::withoutExcludedOptions() */
'autocomplete_excluded_options' => [],
]);

$resolver->setRequired(['class']);
Expand Down