Webdevelopment

Have a dropdown list with countries in Sonata admin bundle

In the Sonata admin bundle you might want the standard list of countries as a dropdown. This is easily done because Symfony (the framework to use Sonata admin bundle with) actually has this list built in. So there is no need to produce a data table or import it externally.

In Symfony there’s the field  type ‘country’ which will produce a country list with two-letter codes as values.
The field type ‘locale’ will produce a list with languages. The value of an option is either a two letter code (ISO639-1) or a four letter code with underscore  (ISO3166) like so: fr_FR

In your admin class you will use the second parameter of the add method to define the field type. The following code snippet shows how:

[php]

class SourceFlowsAdmin extends Admin
{
protected $baseRouteName = ‘gasp_sourceflows_admin’;

protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add(‘source’)
->add(‘country’, ‘country’, array())
->add(‘locale’, ‘locale’, array())
;
}
}

[/php]

And that’s basically it