Skip to content

Commit 616fc8a

Browse files
committed
add choice_label_attr
1 parent 995cf4e commit 616fc8a

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed

src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
181181
/**
182182
* {@inheritdoc}
183183
*/
184-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
184+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelAttr = null)
185185
{
186186
// The input is not validated on purpose. This way, the decorated
187187
// factory may decide which input to accept and which not.
188-
$hash = self::generateHash(array($list, $preferredChoices, $label, $index, $groupBy, $attr));
188+
$hash = self::generateHash(array($list, $preferredChoices, $label, $index, $groupBy, $attr, $labelAttr));
189189

190190
if (!isset($this->views[$hash])) {
191191
$this->views[$hash] = $this->decoratedFactory->createView(
@@ -194,7 +194,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
194194
$label,
195195
$index,
196196
$groupBy,
197-
$attr
197+
$attr,
198+
$labelAttr
198199
);
199200
}
200201

src/Symfony/Component/Form/ChoiceList/Factory/ChoiceListFactoryInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
112112
* group names
113113
* @param null|array|callable $attr The callable generating the
114114
* HTML attributes
115+
* @param null|array|callable $labelAttr The callable generating the
116+
* label HTML attributes
115117
*
116118
* @return ChoiceListView The choice list view
117119
*/
118-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null);
120+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelAttr = null);
119121
}

src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
6363
/**
6464
* {@inheritdoc}
6565
*/
66-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
66+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelAttr = null)
6767
{
6868
// Backwards compatibility
6969
if ($list instanceof LegacyChoiceListAdapter && empty($preferredChoices)
@@ -111,6 +111,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
111111
$keys,
112112
$index,
113113
$attr,
114+
$labelAttr,
114115
$preferredChoices,
115116
$preferredViews,
116117
$otherViews
@@ -125,6 +126,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
125126
$keys,
126127
$index,
127128
$attr,
129+
$labelAttr,
128130
$preferredChoices,
129131
$preferredViews,
130132
$otherViews
@@ -148,7 +150,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
148150
return new ChoiceListView($otherViews, $preferredViews);
149151
}
150152

151-
private static function addChoiceView($choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
153+
private static function addChoiceView($choice, $value, $label, $keys, &$index, $attr, $labelAttr, $isPreferred, &$preferredViews, &$otherViews)
152154
{
153155
// $value may be an integer or a string, since it's stored in the array
154156
// keys. We want to guarantee it's a string though.
@@ -162,7 +164,8 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
162164
null === $label ? (string) $key : (string) call_user_func($label, $choice, $key, $value),
163165
// The attributes may be a callable or a mapping from choice indices
164166
// to nested arrays
165-
is_callable($attr) ? call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())
167+
is_callable($attr) ? call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array()),
168+
is_callable($labelAttr) ? call_user_func($labelAttr, $choice, $key, $value) : (isset($labelAttr[$key]) ? $labelAttr[$key] : array())
166169
);
167170

168171
// $isPreferred may be null if no choices are preferred
@@ -173,7 +176,7 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
173176
}
174177
}
175178

176-
private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
179+
private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $keys, &$index, $attr, $labelAttr, $isPreferred, &$preferredViews, &$otherViews)
177180
{
178181
foreach ($groupBy as $key => $value) {
179182
if (null === $value) {
@@ -192,6 +195,7 @@ private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $key
192195
$keys,
193196
$index,
194197
$attr,
198+
$labelAttr,
195199
$isPreferred,
196200
$preferredViewsForGroup,
197201
$otherViewsForGroup
@@ -216,14 +220,15 @@ private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $key
216220
$keys,
217221
$index,
218222
$attr,
223+
$labelAttr,
219224
$isPreferred,
220225
$preferredViews,
221226
$otherViews
222227
);
223228
}
224229
}
225230

226-
private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
231+
private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $labelAttr, $isPreferred, &$preferredViews, &$otherViews)
227232
{
228233
$groupLabel = call_user_func($groupBy, $choice, $keys[$value], $value);
229234

@@ -236,6 +241,7 @@ private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label
236241
$keys,
237242
$index,
238243
$attr,
244+
$labelAttr,
239245
$isPreferred,
240246
$preferredViews,
241247
$otherViews
@@ -260,6 +266,7 @@ private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label
260266
$keys,
261267
$index,
262268
$attr,
269+
$labelAttr,
263270
$isPreferred,
264271
$preferredViews[$groupLabel]->choices,
265272
$otherViews[$groupLabel]->choices

src/Symfony/Component/Form/ChoiceList/Factory/PropertyAccessDecorator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
166166
*
167167
* @return ChoiceListView The choice list view
168168
*/
169-
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
169+
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelAttr = null)
170170
{
171171
$accessor = $this->propertyAccessor;
172172

@@ -229,6 +229,16 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
229229
};
230230
}
231231

232-
return $this->decoratedFactory->createView($list, $preferredChoices, $label, $index, $groupBy, $attr);
232+
if (is_string($labelAttr)) {
233+
$labelAttr = new PropertyPath($labelAttr);
234+
}
235+
236+
if ($labelAttr instanceof PropertyPath) {
237+
$labelAttr = function ($choice) use ($accessor, $labelAttr) {
238+
return $accessor->getValue($choice, $labelAttr);
239+
};
240+
}
241+
242+
return $this->decoratedFactory->createView($list, $preferredChoices, $label, $index, $groupBy, $attr, $labelAttr);
233243
}
234244
}

src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,27 @@ class ChoiceView extends LegacyChoiceView
7575
*/
7676
public $attr;
7777

78+
/**
79+
* Additional attributes for labels HTML tag.
80+
*
81+
* @var array
82+
*/
83+
public $labelAttr;
84+
7885
/**
7986
* Creates a new choice view.
8087
*
81-
* @param mixed $data The original choice
82-
* @param string $value The view representation of the choice
83-
* @param string $label The label displayed to humans
84-
* @param array $attr Additional attributes for the HTML tag
88+
* @param mixed $data The original choice
89+
* @param string $value The view representation of the choice
90+
* @param string $label The label displayed to humans
91+
* @param array $attr Additional attributes for the HTML tag
92+
* @param array $labelAttr Additional attributes for labels HTML tag
8593
*/
86-
public function __construct($data, $value, $label, array $attr = array())
94+
public function __construct($data, $value, $label, array $attr = array(), array $labelAttr = array())
8795
{
8896
parent::__construct($data, $value, $label);
8997

9098
$this->attr = $attr;
99+
$this->labelAttr = $labelAttr;
91100
}
92101
}

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ public function configureOptions(OptionsResolver $resolver)
325325
'choice_name' => null,
326326
'choice_value' => null,
327327
'choice_attr' => null,
328+
'choice_label_attr' => null,
328329
'preferred_choices' => array(),
329330
'group_by' => null,
330331
'empty_data' => $emptyData,
@@ -353,6 +354,7 @@ public function configureOptions(OptionsResolver $resolver)
353354
$resolver->setAllowedTypes('choice_name', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
354355
$resolver->setAllowedTypes('choice_value', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
355356
$resolver->setAllowedTypes('choice_attr', array('null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
357+
$resolver->setAllowedTypes('choice_label_attr', array('null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
356358
$resolver->setAllowedTypes('preferred_choices', array('array', '\Traversable', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
357359
$resolver->setAllowedTypes('group_by', array('null', 'array', '\Traversable', 'string', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
358360
}
@@ -427,6 +429,7 @@ private function addSubForm(FormBuilderInterface $builder, $name, ChoiceView $ch
427429
'value' => $choiceView->value,
428430
'label' => $choiceView->label,
429431
'attr' => $choiceView->attr,
432+
'label_attr' => $choiceView->labelAttr,
430433
'translation_domain' => $options['translation_domain'],
431434
'block_name' => 'entry',
432435
);
@@ -459,7 +462,8 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
459462
$options['choice_label'],
460463
$options['choice_name'],
461464
$options['group_by'],
462-
$options['choice_attr']
465+
$options['choice_attr'],
466+
$options['choice_label_attr']
463467
);
464468
}
465469
}

0 commit comments

Comments
 (0)