Development

/branches/1.3/lib/widget/sfWidgetFormChoiceBase.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/widget/sfWidgetFormChoiceBase.class.php

Revision 29309, 2.0 kB (checked in by bschussek, 5 years ago)

[1.3, 1.4] fixed: Choices are not always automatically translated. Added option "translate_choices" that defaults to true (closes #7714)

  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfWidgetFormChoiceBase is the base class for all choice/select widgets
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Bernhard Schussek <bernhard.schussek@symfony-project.com>
17  * @version    SVN: $Id$
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 }
Note: See TracBrowser for help on using the browser.