innoweb/silverstripe-international-phone-number-field

使用 Google 的 libphonenumber 添加电话字段。

5.1.1 2024-09-10 01:04 UTC

README

Version License

简介

使用 Google 的 libphonenumber 和 intl-tel-input 插件,添加国际电话号码的数据库和表单字段。

支持 IP 地理位置服务

要求

注意:此版本与 SilverStripe 5 兼容。对于 SilverStripe 4,请参阅 4 版本分支

安装

使用 composer 安装模块

composer require innoweb/silverstripe-international-phone-number-field dev-master

然后运行 dev/build。

用法

数据库字段

此模块提供了一个数据库字段,用于数据对象

private static $db = [
	...
	'PhoneNumber' => 'Phone',
	...
];

此字段将电话号码以 varchar 格式存储在数据库中。

在 CMS 中,数据类型 PhoneInternationalPhoneNumberField 的形式渲染。

在模板中,以下格式化函数可用

  • $PhoneNumber.International:返回国际格式的电话号码,例如 "+41 44 668 1800"
  • $PhoneNumber.National:返回国家格式的电话号码,例如 "044 668 1800"
  • $PhoneNumber.E164:返回国际格式的电话号码,但不进行格式化,例如 "+41446681800"
  • $PhoneNumber.URL$PhoneNumber.RFC3966:返回国际格式的电话号码,但所有空格和其他分隔符号均用连字符替换,并将任何电话号码扩展名附加到 ";ext=" 后。它还将添加 "tel:" 前缀,例如 "tel:+41-44-668-1800"。

表单字段

InternationalPhoneNumberField 可用于任何存储电话号码的 Varchar 字段。

配置

要将字段设置为使用用户当前位置作为默认值并自定义字段,您可以配置以下选项 InternationalPhoneNumberField

  • geolocation_service:使用 IP 位置确定当前用户的国家代码。可以是 'ipstack''ipinfo''ipgeolocation''freegeoip'。默认为 'freegeoip'
  • geolocation_api_key:来自 ipstack.comipinfo.ioipgeolocation 的 API 密钥。默认为 false
  • geolocation_protocol:用于连接到地理位置服务的协议。默认为 'https'
  • initial_country:电话号码字段中最初显示的国家代码。默认为 'auto',在这种情况下,如果已设置,则使用地理位置确定位置。
  • only_countries:可用于选择的数组国家代码。默认为 false,列出所有国家。
  • preferred_countries:推送到下拉列表顶部的数组国家代码。默认为 false,按字母顺序列出所有国家。
  • excluded_countries:要从下拉列表中排除的国家代码数组。默认为 false,列出所有国家。

许可证

BSD 3-Clause License,请参阅 许可证