Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] Accept a closure as label formatter in an ObjectChoiceList #10261

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
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ class ObjectChoiceList extends ChoiceList
* stored in the array key pointing to the nested
* array. The topmost level of the hierarchy may also
* be a \Traversable.
* @param string $labelPath A property path pointing to the property used
* for the choice labels. The value is obtained
* by calling the getter on the object. If the
* path is NULL, the object's __toString() method
* @param string|\Closure $labelPath A property path pointing to the property used
* for the choice labels.
* If the path is a string, the value is obtained
* by calling the getter on the object.
* If the path is NULL, the object's __toString() method
* is used instead.
* If the path is a Closure, the closure is called with
* the choice as first argument.
* @param array $preferredChoices A flat array of choices that should be
* presented to the user with priority.
* @param string $groupPath A property path pointing to the property used
Expand All @@ -88,10 +91,16 @@ class ObjectChoiceList extends ChoiceList
public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null)
{
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->labelPath = null !== $labelPath ? new PropertyPath($labelPath) : null;
$this->groupPath = null !== $groupPath ? new PropertyPath($groupPath) : null;
$this->valuePath = null !== $valuePath ? new PropertyPath($valuePath) : null;

$this->labelPath = null;
if ($labelPath instanceof \Closure) {
$this->labelPath = $labelPath;
} elseif (is_string($labelPath)) {
$this->labelPath = new PropertyPath($labelPath);
}

parent::__construct($choices, array(), $preferredChoices);
}

Expand Down Expand Up @@ -173,7 +182,11 @@ private function extractLabels($choices, array &$labels)
$labels[$i] = array();
$this->extractLabels($choice, $labels[$i]);
} elseif ($this->labelPath) {
$labels[$i] = $this->propertyAccessor->getValue($choice, $this->labelPath);
if ($this->labelPath instanceof \Closure) {
$labels[$i] = call_user_func($this->labelPath, $choice);
} else {
$labels[$i] = $this->propertyAccessor->getValue($choice, $this->labelPath);
}
} elseif (method_exists($choice, '__toString')) {
$labels[$i] = (string) $choice;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@ public function testInitArrayWithValuePath()
$this->assertEquals(array(0 => new ChoiceView($this->obj1, '10', 'A'), 3 => new ChoiceView($this->obj4, '40', 'D')), $this->list->getRemainingViews());
}

public function testInitArrayWithClosure()
{
$this->obj1 = (object) array('name' => 'A', 'id' => 10);
$this->obj2 = (object) array('name' => 'B', 'id' => 20);
$this->obj3 = (object) array('name' => 'C', 'id' => 30);
$this->obj4 = (object) array('name' => 'D', 'id' => 40);

$prefix = '--> ';
$closure = function ($choice) use ($prefix) {
return $prefix.$choice->name.' ('.$choice->id.')';
};

$this->list = new ObjectChoiceList(
array($this->obj1, $this->obj2, $this->obj3, $this->obj4),
$closure,
array($this->obj2, $this->obj3),
null,
'id'
);

$this->assertSame(array($this->obj1, $this->obj2, $this->obj3, $this->obj4), $this->list->getChoices());
$this->assertSame(array('10', '20', '30', '40'), $this->list->getValues());
$this->assertEquals(array(1 => new ChoiceView($this->obj2, '20', '--> B (20)'), 2 => new ChoiceView($this->obj3, '30', '--> C (30)')), $this->list->getPreferredViews());
$this->assertEquals(array(0 => new ChoiceView($this->obj1, '10', '--> A (10)'), 3 => new ChoiceView($this->obj4, '40', '--> D (40)')), $this->list->getRemainingViews());
}

public function testInitArrayUsesToString()
{
$this->obj1 = new ObjectChoiceListTest_EntityWithToString('A');
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.