|
Revision 29309, 2.0 kB
(checked in by bschussek, 3 years ago)
|
[1.3, 1.4] fixed: Choices are not always automatically translated. Added option "translate_choices" that defaults to true (closes #7714)
|
| Line | |
|---|
| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
abstract class sfWidgetFormChoiceBase extends sfWidgetForm |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Constructor. |
|---|
| 23 |
* |
|---|
| 24 |
* Available options: |
|---|
| 25 |
* |
|---|
| 26 |
* * choices: An array of possible choices (required) |
|---|
| 27 |
* |
|---|
| 28 |
* @param array $options An array of options |
|---|
| 29 |
* @param array $attributes An array of default HTML attributes |
|---|
| 30 |
* |
|---|
| 31 |
* @see sfWidgetForm |
|---|
| 32 |
*/ |
|---|
| 33 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 34 |
{ |
|---|
| 35 |
$this->addRequiredOption('choices'); |
|---|
| 36 |
$this->addOption('translate_choices', true); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
* Returns the translated choices configured for this widget |
|---|
| 41 |
* |
|---|
| 42 |
* @return array An array of strings |
|---|
| 43 |
*/ |
|---|
| 44 |
public function getChoices() |
|---|
| 45 |
{ |
|---|
| 46 |
$choices = $this->getOption('choices'); |
|---|
| 47 |
|
|---|
| 48 |
if ($choices instanceof sfCallable) |
|---|
| 49 |
{ |
|---|
| 50 |
$choices = $choices->call(); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
if (!$this->getOption('translate_choices')) |
|---|
| 54 |
{ |
|---|
| 55 |
return $choices; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
$results = array(); |
|---|
| 59 |
foreach ($choices as $key => $choice) |
|---|
| 60 |
{ |
|---|
| 61 |
if (is_array($choice)) |
|---|
| 62 |
{ |
|---|
| 63 |
$results[$this->translate($key)] = $this->translateAll($choice); |
|---|
| 64 |
} |
|---|
| 65 |
else |
|---|
| 66 |
{ |
|---|
| 67 |
$results[$key] = $this->translate($choice); |
|---|
| 68 |
} |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
return $results; |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
* Clones this object |
|---|
| 76 |
*/ |
|---|
| 77 |
public function __clone() |
|---|
| 78 |
{ |
|---|
| 79 |
if ($this->getOption('choices') instanceof sfCallable) |
|---|
| 80 |
{ |
|---|
| 81 |
$callable = $this->getOption('choices')->getCallable(); |
|---|
| 82 |
if (is_array($callable) && $callable[0] instanceof self) |
|---|
| 83 |
{ |
|---|
| 84 |
$callable[0] = $this; |
|---|
| 85 |
$this->setOption('choices', new sfCallable($callable)); |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|