-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form][DX] Add choice_filter option to ChoiceType #28607
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,20 +14,26 @@ | |
use Doctrine\Common\Persistence\ObjectManager; | ||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceFilterInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
|
||
/** | ||
* Loads choices using a Doctrine object manager. | ||
* | ||
* @author Bernhard Schussek <[email protected]> | ||
*/ | ||
class DoctrineChoiceLoader implements ChoiceLoaderInterface | ||
class DoctrineChoiceLoader implements ChoiceLoaderInterface, ChoiceFilterInterface | ||
{ | ||
private $manager; | ||
private $class; | ||
private $idReader; | ||
private $objectLoader; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $choiceFilter; | ||
|
||
/** | ||
* @var ChoiceListInterface | ||
*/ | ||
|
@@ -68,6 +74,10 @@ public function loadChoiceList($value = null) | |
? $this->objectLoader->getEntities() | ||
: $this->manager->getRepository($this->class)->findAll(); | ||
|
||
if (null !== $this->choiceFilter) { | ||
$objects = array_filter($objects, $this->choiceFilter, ARRAY_FILTER_USE_BOTH); | ||
} | ||
|
||
return $this->choiceList = new ArrayChoiceList($objects, $value); | ||
} | ||
|
||
|
@@ -146,4 +156,12 @@ public function loadChoicesForValues(array $values, $value = null) | |
|
||
return $this->loadChoiceList($value)->getChoicesForValues($values); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setChoiceFilter(callable $choiceFilter) | ||
{ | ||
$this->choiceFilter = $choiceFilter; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,15 @@ | |
* | ||
* @author Jules Pietri <[email protected]> | ||
*/ | ||
class CallbackChoiceLoader implements ChoiceLoaderInterface | ||
class CallbackChoiceLoader implements ChoiceLoaderInterface, ChoiceFilterInterface | ||
{ | ||
private $callback; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $choiceFilter; | ||
|
||
/** | ||
* The loaded choice list. | ||
* | ||
|
@@ -46,7 +51,13 @@ public function loadChoiceList($value = null) | |
return $this->choiceList; | ||
} | ||
|
||
return $this->choiceList = new ArrayChoiceList(\call_user_func($this->callback), $value); | ||
$choices = \call_user_func($this->callback); | ||
|
||
if (null !== $this->choiceFilter) { | ||
$choices = array_filter($choices, $this->choiceFilter, ARRAY_FILTER_USE_BOTH); | ||
} | ||
|
||
return $this->choiceList = new ArrayChoiceList($choices, $value); | ||
} | ||
|
||
/** | ||
|
@@ -74,4 +85,12 @@ public function loadValuesForChoices(array $choices, $value = null) | |
|
||
return $this->loadChoiceList($value)->getValuesForChoices($choices); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setChoiceFilter(callable $choiceFilter) | ||
{ | ||
$this->choiceFilter = $choiceFilter; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Form/ChoiceList/Loader/ChoiceFilterInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\ChoiceList\Loader; | ||
|
||
/** | ||
* @author Yonel Ceruto <[email protected]> | ||
*/ | ||
interface ChoiceFilterInterface | ||
{ | ||
/** | ||
* @param callable $choiceFilter The callable returning a filtered array of choices | ||
*/ | ||
public function setChoiceFilter(callable $choiceFilter); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"options": { | ||
"own": [ | ||
"choice_attr", | ||
"choice_filter", | ||
"choice_label", | ||
"choice_loader", | ||
"choice_name", | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wild thoughts, as I don't know all the implications right now: I don't know if a new interface with a setter is a good idea. Shouldn't we simply inject in the loaders' constructors the
callable $choiceFilter
instead?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 makes sense, to keep compatibility with any choice_loader we could still decorate it if the choice filter wasnt injected initially.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expected this question :) My goal is enable this feature for all (more and more used)
CallbackChoiceLoader
in current projects, automatically.Otherwise, the feature will be enabled only for core types (e.g. Intl form types,
EntityType
) and will require more code and then more maintenance (injecting the$options['choice_filter']
everywhere).This was the simplest solution I found to achieve a better DX, so callback loaders don't need to do something to enable this option.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this would allow for custom choice loader to enable this feature without care about injecting
$options['choice_filter']
everywhere the loader is used. We only care about the implementation as a plug-and-play interface.Btw, there are many examples of interface with a setter into the core, I'm not sure it's a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #28624 for a different approach