Last active
December 29, 2015 17:59
-
-
Save Tom32i/7707445 to your computer and use it in GitHub Desktop.
Working with choice attributes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Twig; | |
use Symfony\Component\PropertyAccess\PropertyAccess; | |
use Doctrine\Common\Inflector\Inflector; | |
class ChoiceExtension extends \Twig_Extension | |
{ | |
/** | |
* Property Accessor | |
* | |
* @var Symfony\Component\PropertyAccess\PropertyAccess\PropertyAccessorInterface | |
*/ | |
private $accessor; | |
/** | |
* Doctrine inflector | |
* | |
* @var Doctrine\Common\Inflector\Inflector | |
*/ | |
private $inflector; | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
$this->accessor = PropertyAccess::getPropertyAccessor(); | |
$this->inflector = new Inflector(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'acme_demo_choice_extension'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFunctions() | |
{ | |
return array( | |
new \Twig_SimpleFunction('chosen', array($this, 'getChosenValue')), | |
); | |
} | |
/** | |
* Get the chosen value of a choice attribute | |
* | |
* @param mixed $entity The entity to get chosen value for | |
* @param string $property The property | |
* @param string $choicesProperty The property that hold the translation keys | |
* | |
* @return string | |
*/ | |
public function getChosenValue($entity, $property, $choicesProperty = null) | |
{ | |
if ($choicesProperty === null) { | |
$choicesProperty = $this->inflector->pluralize($property); | |
} | |
$value = $this->accessor->getValue($entity, $property); | |
$choices = $this->accessor->getValue($entity, $choicesProperty); | |
return isset($choices[$value]) ? $choices[$value] : null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Acme\DemoBundle\Entity\User; | |
class ProfileType extends AbstractType | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('status', 'choice', array('choices' => User::getStatus())); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array('data_class' => 'Acme\DemoBundle\Entity\User')); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'profile'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* User | |
* | |
* @ORM\Table() | |
* @ORM\Entity() | |
*/ | |
class User | |
{ | |
const STATUS_SINGLE = 0; | |
const STATUS_RELATIONSHIP = 1; | |
const STATUS_ENGAGED = 2; | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="status", type="smallint") | |
* @Assert\Choice(callback="getAvailableStatus") | |
*/ | |
protected $status; | |
/** | |
* Get available values and translate keys for status | |
* | |
* @return array | |
*/ | |
public static function getStatus() | |
{ | |
return array( | |
self::STATUS_SINGLE => 'user.status.single', | |
self::STATUS_RELATIONSHIP => 'user.status.relationship', | |
self::STATUS_ENGAGED => 'user.status.engaged', | |
); | |
} | |
/** | |
* Get available values for status | |
* | |
* @return array | |
*/ | |
public static function getAvailableStatus() | |
{ | |
return array_keys(self::getStatus()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ chosen(user, 'status')|trans }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment