International Phone Number Field
, (*1)
Introduction
Adds a database and form field for international phone numbers using Google's libphonenumber and the intl-tel-input plugin., (*2)
IP Geo Location services supported:
* ipstack.com
* ipinfo.io
* ipgeolocation
* freegeoip (default), (*3)
Requirements
Installation
Install the module using composer:, (*6)
composer require innoweb/silverstripe-international-phone-number-field dev-master
Then run dev/build., (*7)
Usage
Database field
This module provides a database field to be used for data objects:, (*8)
private static $db = [
...
'PhoneNumber' => 'Phone',
...
];
This stores the phone number in the database as a varchar., (*9)
In the CMS the data type Phone
renders as a InternationalPhoneNumberField
., (*10)
In templates, the following formatting functions are available:, (*11)
-
$PhoneNumber.International
: Returns the phone number in international format, e.g. "+41 44 668 1800"
-
$PhoneNumber.National
: Returns the phone number in national format, e.g. "044 668 1800"
-
$PhoneNumber.E164
: Returns the phone number in international format, but with no formatting applied, e.g. "+41446681800"
-
$PhoneNumber.URL
or $PhoneNumber.RFC3966
: Returns the phone number in international format, but with all spaces and other separating symbols replaced with a hyphen, and with any phone number extension appended with ";ext=". It also will have a prefix of "tel:" added, e.g. "tel:+41-44-668-1800".
The InternationalPhoneNumberField
can be used for any Varchar field storing a phone number., (*12)
Configuration
To set the field to use the user's current location as default and customise the field, you can configure the following options for the InternationalPhoneNumberField
class:, (*13)
-
geolocation_service
: Uses IP location to determine the current users's country code. This can be 'ipstack'
, 'ipinfo'
, 'ipgeolocation'
or 'freegeoip'
. Defaults to 'freegeoip'
.
-
geolocation_api_key
: API key for ipstack.com, ipinfo.io or ipgeolocation. Defaults to false
.
-
geolocation_protocol
: Protocol to be used to connecto to geolocation service. Defaults to 'https'
.
-
initial_country
: Country code for initially shown country in the phone number field. Defaults to 'auto'
, in which case the location is determined using geolocation if that's set up.
-
only_countries
: Array of country codes available for selection. Defaults to false
, all countries are listed.
-
preferred_countries
: Array of country codes pushed to the top of the dropdown list. Defaults to false
, all countries are listed alphabetically.
-
excluded_countries
: Array of country codes to be excluded from the dropdown lost. Defaults to false
, all countries are listed.
License
BSD 3-Clause License, see License, (*14)