Easy Admin - 在 Show Action 中显示完整的国家/地区名称

Easy Admin - Display Full Country Name in Show Action

我有一个需要国家/地区的地址实体。在关联的表单中,我使用的是 Symfony 的 CountryType,它显示了用户友好的国家选择并将其缩写存储在实体中(例如 DE 代表德国或 CH 代表瑞士)。

为了在管理面板的显示操作中显示地址的国家/地区,我在 easy_admin.yaml 中使用以下行:

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

问题:

这只显示缩写而不是国家的实际名称。我该如何更改?

地址实体中的国家/地区:

/**
 * @ORM\Column(type="string", length=255)
 */
private $country;

我认为最好的解决方案是使用内置 Symfony intl component

composer require symfony/intl 安装组件。

然后在您的实体中您可以use Symfony\Component\Intl\Intl;

我建议在名为 countryName 的实体上创建一个新的 属性,只要您设置国家/地区代码,就会调用 属性 的 setter。您的 setter 可能看起来像这样:

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

然后在您的 yaml 文件中将 address.entity.country 更改为 address.entity.countryName