Skip to content

Commit 5ae2afd

Browse files
committed
adds support for the Country field
1 parent 1d774e0 commit 5ae2afd

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

src/fields/Country.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace craft\feedme\fields;
4+
5+
use Cake\Utility\Hash;
6+
use Craft;
7+
use craft\feedme\base\Field;
8+
use craft\feedme\base\FieldInterface;
9+
use craft\fields\Country as CountryField;
10+
11+
/**
12+
*
13+
* @property-read string $mappingTemplate
14+
*/
15+
class Country extends Field implements FieldInterface
16+
{
17+
// Properties
18+
// =========================================================================
19+
20+
/**
21+
* @var string
22+
*/
23+
public static string $name = 'Country';
24+
25+
/**
26+
* @var string
27+
*/
28+
public static string $class = CountryField::class;
29+
30+
31+
// Templates
32+
// =========================================================================
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function getMappingTemplate(): string
38+
{
39+
return 'feed-me/_includes/fields/country';
40+
}
41+
42+
// Public Methods
43+
// =========================================================================
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function parseField(): mixed
49+
{
50+
$value = $this->fetchValue();
51+
52+
if ($value === null) {
53+
return null;
54+
}
55+
56+
$value = (string) $value;
57+
$default = Hash::get($this->fieldInfo, 'default');
58+
59+
$options = Craft::$app->getAddresses()->getCountryRepository()->getList(Craft::$app->language);
60+
61+
$match = Hash::get($this->fieldInfo, 'options.match', 'value');
62+
63+
// if we're matching by value - just look for the key in the $options array or check the default
64+
if (($match === 'value' && isset($options[$value])) || $default === $value) {
65+
return $value;
66+
}
67+
68+
// if we're looking by label - look for the keys
69+
if ($match === 'label') {
70+
$found = array_filter($options, function($option) use ($value) {
71+
return strcasecmp($option, $value) === 0;
72+
});
73+
74+
if (!empty($found)) {
75+
return array_key_first($found);
76+
}
77+
}
78+
79+
if (empty($value)) {
80+
return $value;
81+
}
82+
83+
return null;
84+
}
85+
86+
/**
87+
* @inheritDoc
88+
*/
89+
public function fetchValue(): mixed
90+
{
91+
return (string) parent::fetchValue();
92+
}
93+
}

src/services/Fields.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use craft\feedme\fields\Checkboxes;
1717
use craft\feedme\fields\CommerceProducts;
1818
use craft\feedme\fields\CommerceVariants;
19+
use craft\feedme\fields\Country;
1920
use craft\feedme\fields\Date;
2021
use craft\feedme\fields\DefaultField;
2122
use craft\feedme\fields\DigitalProducts;
@@ -127,6 +128,7 @@ public function getRegisteredFields(): array
127128
Checkboxes::class,
128129
CommerceProducts::class,
129130
CommerceVariants::class,
131+
Country::class,
130132
Date::class,
131133
Dropdown::class,
132134
Entries::class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{# ------------------------ #}
2+
{# Available Variables #}
3+
{# ------------------------ #}
4+
{# Attributes: #}
5+
{# type, name, handle, instructions, attribute, default, feed, feedData #}
6+
{# ------------------------ #}
7+
{# Fields: #}
8+
{# name, handle, instructions, feed, feedData, field, fieldClass #}
9+
{# ------------------------ #}
10+
11+
{% import 'feed-me/_macros' as feedMeMacro %}
12+
{% import '_includes/forms' as forms %}
13+
14+
{% if field is defined %}
15+
{% set options = [{
16+
'label': ' ',
17+
'value': '__blank__'
18+
}] %}
19+
{% set options = options|merge(craft.app.addresses.getCountryRepository().getList(craft.app.language)) %}
20+
{% set default = default ?? {
21+
type: 'select',
22+
options: options,
23+
} %}
24+
{% endif %}
25+
26+
{% extends 'feed-me/_includes/fields/_base' %}
27+
28+
{% block extraSettings %}
29+
<div class="element-match">
30+
<span>{{ 'Data provided for this field is:'|t('feed-me') }}</span>
31+
32+
{{ forms.selectField({
33+
name: 'options[match]',
34+
class: '',
35+
options: [
36+
{ value: 'value', label: 'Value'|t('feed-me') },
37+
{ value: 'label', label: 'Label'|t('feed-me') }
38+
],
39+
value: hash_get(feed.fieldMapping, optionsPath ~ '.match') ?: '',
40+
}) }}
41+
</div>
42+
{% endblock %}

0 commit comments

Comments
 (0)