Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy Admin - Display Full Country Name in Show Action

I have an Address entity which requires a country. In the associated form I am using Symfony's CountryType which displays a user-friendly selection of countries and stores its abbreviation in the entity (e.g. DE for Germany or CH for Switzerland).

To display the address' country in the show action of the admin panel, I am using the following line in the easy_admin.yaml:

- { property: country, label: 'address.entity.country' }

Problem:

This only displays the abbreviation and not the actual name of the country. How can I change that?

Country in Address entity:

/**
 * @ORM\Column(type="string", length=255)
 */
private $country;
like image 935
chrisp Avatar asked Sep 25 '18 08:09

chrisp


Video Answer


1 Answers

I think the best solution would be to use the built in Symfony intl component.

composer require symfony/intl to install the component.

Then in your entity you can use Symfony\Component\Intl\Intl;.

I suggest creating a new property on your entity called countryName where the setter of that property gets called whenever you set the country code. Your setter could look something like this:

public function setCountryName (string $countryCode) 
{
    $this->countryName = Intl::getRegionBundle()->getCountryName(strtoupper($countryCode));
}

Then in your yaml file change address.entity.country to address.entity.countryName.

like image 125
Dirk Scholten Avatar answered Oct 26 '22 13:10

Dirk Scholten