冬季 / wn-location-plugin
Winter CMS的位置插件
Requires
- php: >=7.2
- composer/installers: ~1.0
This package is auto-updated.
Last update: 2024-09-10 14:52:39 UTC
README
此插件为Winter CMS添加基于位置的功能。
- 轻松地为任何模型添加国家和州
- 地址查找表单小部件(Google API)
Google API密钥要求
截至2016年6月22日,Google Maps服务需要API密钥。您可以从以下链接生成密钥:
复制密钥并在设置 > 位置设置区域中输入。如果您发现地址查找器无法工作,您可能需要启用Places API和Maps JavaScript API。
为任何模型添加国家和州
此插件提供了一种简单的方法,将位置字段(国家和州)添加到任何模型。只需将这些列添加到数据库表中
$table->integer('country_id')->unsigned()->nullable()->index(); $table->integer('state_id')->unsigned()->nullable()->index();
然后在模型类中实现Winter.Location.Behaviors.LocationModel行为
public $implement = ['Winter.Location.Behaviors.LocationModel'];
这将自动创建两个“属于”关系
- state - 对Winter\Location\Models\State的关系
- country - 对Winter\Location\Models\Country的关系
后端使用
表单
您可以自由添加以下表单字段定义
country: label: winter.location::lang.country.label type: dropdown span: left placeholder: winter.location::lang.country.select state: label: winter.location::lang.state.label type: dropdown span: right dependsOn: country placeholder: winter.location::lang.state.select
列表
对于列表列定义,您可以使用以下代码片段
country: label: winter.location::lang.country.label searchable: true relation: country select: name sortable: false state: label: winter.location::lang.state.label searchable: true relation: state select: name sortable: false
前端使用
前端也可以通过创建一个名为country-state的部分来使用关系,内容如下
{% set countryId = countryId|default(form_value('country_id')) %} {% set stateId = stateId|default(form_value('state_id')) %} <div class="form-group"> <label for="accountCountry">Country</label> {{ form_select_country('country_id', countryId, { id: 'accountCountry', class: 'form-control', emptyOption: '', 'data-request': 'onInit', 'data-request-update': { 'country-state': '#partialCountryState' } }) }} </div> <div class="form-group"> <label for="accountState">State</label> {{ form_select_state('state_id', countryId, stateId, { id: 'accountState', class: 'form-control', emptyOption: '' }) }} </div>
此部分可以使用以下方式在表单中渲染
<div id="partialCountryState"> {% partial 'country-state' countryId=user.country_id stateId=user.state_id %} </div>
简码访问器
该行为还将添加一个特殊的简码访问器和设置器到模型中,将country_code
和state_code
转换为它们相应的标识符。
// Softly looks up and sets the country_id and state_id // for these Country and State relations. $model->country_code = "US"; $model->state_code = "FL"; $model->save();
ISO 3166-1访问器
该行为还将添加ISO-3166-1值作为访问器到模型中(数据来源于league/iso3166包)。
可用的访问器有iso_name
(国家名称)、iso_alpha3
(三字母代码)、iso_numeric
(三位数代码)、iso_currencies
(三位数货币代码)和iso
(所有iso属性的数组)。
$usCountry = \Winter\Location\Models\Country::whereCode('US')->first(); $usCountry->iso_name; // (string) "United States of America" $usCountry->iso_alpha3; // (string) "USA" $usCountry->iso_numeric; // (string) "840" $usCountry->iso_currencies; // (array) [ 0 => "USD" ] $usCountry->iso; // (array) [ // "name" => "United States of America" // "alpha2" => "US" // "alpha3" => "USA" // "numeric" => "840" // "currency" => [ // 0 => "USD" // ] // ]
地址查找表单小部件
此插件引入了一个名为addressfinder
的地址查找表单字段。表单小部件渲染一个Google Maps自动完成地址字段,它将根据在地址中输入和选择的值自动填充映射字段。
可用的映射
- street
- city
- zip
- state
- country
- country-long
- latitude
- longitude
- vicinity
可用的选项
您可以通过定义countryRestriction
选项来限制地址查找到某些国家。该选项接受ISO 3166-1 ALPHA-2兼容的国家代码的逗号分隔列表(见:https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)。
用法
# =================================== # Form Field Definitions # =================================== fields: address: label: Address type: addressfinder countryRestriction: 'us,ch' fieldMap: latitude: latitude longitude: longitude city: city zip: zip country: country_code state: state_code vicinity: vicinity city: label: City zip: label: Zip country_code: label: Country state_code: label: State latitude: label: Latitude longitude: label: Longitude vicinity: label: Vicinity