Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Implemented attributes for choices #10309

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 @@ -96,7 +96,8 @@
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
{% set attr = choice.attributes %}
<option {% if attr is not empty %}{{ block('option_attributes') }} {% endif %}value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
{% endif %}
{% endfor %}
{% endspaceless %}
Expand Down Expand Up @@ -421,3 +422,16 @@
{%- endfor -%}
{% endspaceless %}
{% endblock button_attributes %}

{% block option_attributes %}
{% spaceless %}
{%- for attrname, attrvalue in attr -%}
{{- " " -}}
{% if attrvalue is sameas(true) -%}
{{- attrname }}="{{ attrname }}"
{%- elseif attrvalue is not sameas(false) -%}
{{- attrname }}="{{ attrvalue }}"
{%- endif -%}
{%- endfor -%}
{% endspaceless %}
{% endblock option_attributes %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<?php echo $formHelper->block($form, 'choice_widget_options', array('choices' => $choice)) ?>
</optgroup>
<?php else: ?>
<option value="<?php echo $view->escape($choice->value) ?>"<?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape($translatorHelper->trans($choice->label, array(), $translation_domain)) ?></option>
<option <?php if (!empty($choice->attributes)): ?><?php echo $view['form']->block($form, 'option_attributes', array('attr' => $choice->attributes)) ?> <?php endif; ?>value="<?php echo $view->escape($choice->value) ?>"<?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape($translatorHelper->trans($choice->label, array(), $translation_domain)) ?></option>
<?php endif ?>
<?php endforeach ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php foreach ($attr as $k => $v): ?>
<?php if ($v === true): ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
<?php elseif ($v !== false): ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
<?php endif ?>
<?php endforeach ?>
53 changes: 32 additions & 21 deletions src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,27 @@ class ChoiceList implements ChoiceListInterface
/**
* Creates a new choice list.
*
* @param array|\Traversable $choices The array of choices. Choices may also be given
* as hierarchy of unlimited depth. Hierarchies are
* created by creating nested arrays. The title of
* the sub-hierarchy can be stored in the array
* key pointing to the nested array. The topmost
* level of the hierarchy may also be a \Traversable.
* @param array $labels The array of labels. The structure of this array
* should match the structure of $choices.
* @param array $preferredChoices A flat array of choices that should be
* presented to the user with priority.
* @param array|\Traversable $choices The array of choices. Choices may also be given
* as hierarchy of unlimited depth. Hierarchies are
* created by creating nested arrays. The title of
* the sub-hierarchy can be stored in the array
* key pointing to the nested array. The topmost
* level of the hierarchy may also be a \Traversable.
* @param array $labels The array of labels. The structure of this array
* should match the structure of $choices.
* @param array $preferredChoices A flat array of choices that should be presented to the user with priority.
* @param array $attributes A multidimensionnal array of attributes. The structure of this
* array should match the structure of $choices.
*
* @throws UnexpectedTypeException If the choices are not an array or \Traversable.
*/
public function __construct($choices, array $labels, array $preferredChoices = array())
public function __construct($choices, array $labels, array $preferredChoices = array(), array $attributes = array())
{
if (!is_array($choices) && !$choices instanceof \Traversable) {
throw new UnexpectedTypeException($choices, 'array or \Traversable');
}

$this->initialize($choices, $labels, $preferredChoices);
$this->initialize($choices, $labels, $preferredChoices, $attributes);
}

/**
Expand All @@ -99,8 +100,9 @@ public function __construct($choices, array $labels, array $preferredChoices = a
* @param array|\Traversable $choices The choices to write into the list.
* @param array $labels The labels belonging to the choices.
* @param array $preferredChoices The choices to display with priority.
* @param array $attributes The choices' attributes.
*/
protected function initialize($choices, array $labels, array $preferredChoices)
protected function initialize($choices, array $labels, array $preferredChoices, array $attributes = array())
{
$this->choices = array();
$this->values = array();
Expand All @@ -112,7 +114,8 @@ protected function initialize($choices, array $labels, array $preferredChoices)
$this->remainingViews,
$choices,
$labels,
$preferredChoices
$preferredChoices,
$attributes
);
}

Expand Down Expand Up @@ -258,18 +261,21 @@ public function getIndicesForValues(array $values)
* @param array|\Traversable $choices The list of choices.
* @param array $labels The labels corresponding to the choices.
* @param array $preferredChoices The preferred choices.
* @param array $attributes The choices' attributes.
*
* @throws InvalidArgumentException If the structures of the choices and labels array do not match.
* @throws InvalidConfigurationException If no valid value or index could be created for a choice.
*/
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices)
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices, array $attributes = array())
{
// Add choices to the nested buckets
foreach ($choices as $group => $choice) {
if (!array_key_exists($group, $labels)) {
throw new InvalidArgumentException('The structures of the choices and labels array do not match.');
}

$attribute = isset($attributes[$group]) ? $attributes[$group] : array();

if (is_array($choice)) {
// Don't do the work if the array is empty
if (count($choice) > 0) {
Expand All @@ -279,7 +285,8 @@ protected function addChoices(array &$bucketForPreferred, array &$bucketForRemai
$bucketForRemaining,
$choice,
$labels[$group],
$preferredChoices
$preferredChoices,
$attribute
);
}
} else {
Expand All @@ -288,7 +295,8 @@ protected function addChoices(array &$bucketForPreferred, array &$bucketForRemai
$bucketForRemaining,
$choice,
$labels[$group],
$preferredChoices
$preferredChoices,
$attribute
);
}
}
Expand All @@ -305,10 +313,11 @@ protected function addChoices(array &$bucketForPreferred, array &$bucketForRemai
* @param array $choices The list of choices in the group.
* @param array $labels The labels corresponding to the choices in the group.
* @param array $preferredChoices The preferred choices.
* @param array $attributes The choices' attributes.
*
* @throws InvalidConfigurationException If no valid value or index could be created for a choice.
*/
protected function addChoiceGroup($group, array &$bucketForPreferred, array &$bucketForRemaining, array $choices, array $labels, array $preferredChoices)
protected function addChoiceGroup($group, array &$bucketForPreferred, array &$bucketForRemaining, array $choices, array $labels, array $preferredChoices, array $attributes = array())
{
// If this is a choice group, create a new level in the choice
// key hierarchy
Expand All @@ -320,7 +329,8 @@ protected function addChoiceGroup($group, array &$bucketForPreferred, array &$bu
$bucketForRemaining[$group],
$choices,
$labels,
$preferredChoices
$preferredChoices,
$attributes
);

// Remove child levels if empty
Expand All @@ -342,10 +352,11 @@ protected function addChoiceGroup($group, array &$bucketForPreferred, array &$bu
* @param mixed $choice The choice to add.
* @param string $label The label for the choice.
* @param array $preferredChoices The preferred choices.
* @param array $attributes The choices' attributes.
*
* @throws InvalidConfigurationException If no valid value or index could be created.
*/
protected function addChoice(array &$bucketForPreferred, array &$bucketForRemaining, $choice, $label, array $preferredChoices)
protected function addChoice(array &$bucketForPreferred, array &$bucketForRemaining, $choice, $label, array $preferredChoices, array $attributes = array())
{
$index = $this->createIndex($choice);

Expand All @@ -359,7 +370,7 @@ protected function addChoice(array &$bucketForPreferred, array &$bucketForRemain
throw new InvalidConfigurationException(sprintf('The value created by the choice list is of type "%s", but should be a string.', gettype($value)));
}

$view = new ChoiceView($choice, $value, $label);
$view = new ChoiceView($choice, $value, $label, $attributes);

$this->choices[$index] = $this->fixChoice($choice);
$this->values[$index] = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,36 @@ class ObjectChoiceList extends ChoiceList
/**
* Creates a new object choice list.
*
* @param array|\Traversable $choices The array of choices. Choices may also be given
* @param array|\Traversable $choices The array of choices. Choices may also be given
* as hierarchy of unlimited depth by creating nested
* arrays. The title of the sub-hierarchy can be
* 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
* @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
* by calling the getter on the object. If the
* path is NULL, the object's __toString() method
* is used instead.
* @param array $preferredChoices A flat array of choices that should be
* @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
* @param string $groupPath A property path pointing to the property used
* to group the choices. Only allowed if
* the choices are given as flat array.
* @param string $valuePath A property path pointing to the property used
* @param string $valuePath A property path pointing to the property used
* for the choice values. If not given, integers
* are generated instead.
* @param PropertyAccessorInterface $propertyAccessor The reflection graph for reading property paths.
* @param array $attributes The choices' attributes.
*/
public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null)
public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null, array $attributes = array())
{
$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;

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

/**
Expand All @@ -103,11 +104,12 @@ public function __construct($choices, $labelPath = null, array $preferredChoices
* @param array|\Traversable $choices The choices to write into the list.
* @param array $labels Ignored.
* @param array $preferredChoices The choices to display with priority.
* @param array $attributes The choices' attributes.
*
* @throws InvalidArgumentException When passing a hierarchy of choices and using
* the "groupPath" option at the same time.
*/
protected function initialize($choices, array $labels, array $preferredChoices)
protected function initialize($choices, array $labels, array $preferredChoices, array $attributes = array())
{
if (null !== $this->groupPath) {
$groupedChoices = array();
Expand Down Expand Up @@ -145,7 +147,7 @@ protected function initialize($choices, array $labels, array $preferredChoices)

$this->extractLabels($choices, $labels);

parent::initialize($choices, $labels, $preferredChoices);
parent::initialize($choices, $labels, $preferredChoices, $attributes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ class SimpleChoiceList extends ChoiceList
/**
* Creates a new simple choice list.
*
* @param array $choices The array of choices with the choices as keys and
* the labels as values. Choices may also be given
* as hierarchy of unlimited depth by creating nested
* arrays. The title of the sub-hierarchy is stored
* in the array key pointing to the nested array.
* @param array $choices The array of choices with the choices as keys and
* the labels as values. Choices may also be given
* as hierarchy of unlimited depth by creating nested
* arrays. The title of the sub-hierarchy is stored
* in the array key pointing to the nested array.
* @param array $preferredChoices A flat array of choices that should be
* presented to the user with priority.
* @param array $attributes The choices' attributes having the same structure than $choices.
*/
public function __construct(array $choices, array $preferredChoices = array())
public function __construct(array $choices, array $preferredChoices = array(), array $attributes = array())
{
// Flip preferred choices to speed up lookup
parent::__construct($choices, $choices, array_flip($preferredChoices));
parent::__construct($choices, $choices, array_flip($preferredChoices), $attributes);
}

/**
Expand Down Expand Up @@ -85,11 +86,14 @@ public function getValuesForChoices(array $choices)
* @param array|\Traversable $choices The list of choices.
* @param array $labels Ignored.
* @param array $preferredChoices The preferred choices.
* @param array $attributes The choices' attributes.
*/
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices)
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices, array $attributes = array())
{
// Add choices to the nested buckets
foreach ($choices as $choice => $label) {
$attribute = isset($attributes[$choice]) ? $attributes[$choice] : array();

if (is_array($label)) {
// Don't do the work if the array is empty
if (count($label) > 0) {
Expand All @@ -99,7 +103,8 @@ protected function addChoices(array &$bucketForPreferred, array &$bucketForRemai
$bucketForRemaining,
$label,
$label,
$preferredChoices
$preferredChoices,
$attribute
);
}
} else {
Expand All @@ -108,7 +113,8 @@ protected function addChoices(array &$bucketForPreferred, array &$bucketForRemai
$bucketForRemaining,
$choice,
$label,
$preferredChoices
$preferredChoices,
$attribute
);
}
}
Expand Down
Loading