Commit dd634b18 authored by bojanz's avatar bojanz Committed by bojanz
Browse files

Issue #3034122 by bojanz, Pancho: Rename the 'country_code' views field and filter to 'country'

parent ed15c7d6
......@@ -20,7 +20,7 @@ function address_field_views_data(FieldStorageConfigInterface $field) {
$field_name = $field->getName();
if ($field_type == 'address') {
$columns = [
'country_code' => 'country_code',
'country_code' => 'country',
'administrative_area' => 'subdivision',
'locality' => 'subdivision',
'dependent_locality' => 'subdivision',
......@@ -41,7 +41,7 @@ function address_field_views_data(FieldStorageConfigInterface $field) {
'property' => $column,
];
}
$data[$table_name][$field_name . '_country_code']['filter']['id'] = 'country_code';
$data[$table_name][$field_name . '_country_code']['filter']['id'] = 'country';
$data[$table_name][$field_name . '_country_code']['sort']['id'] = 'country';
$data[$table_name][$field_name . '_administrative_area']['filter']['id'] = 'administrative_area';
}
......@@ -49,11 +49,11 @@ function address_field_views_data(FieldStorageConfigInterface $field) {
elseif ($field_type == 'address_country') {
foreach ($data as $table_name => $table_data) {
$data[$table_name][$field_name . '_value']['field'] = [
'id' => 'country_code',
'id' => 'country',
'field_name' => $field_name,
'property' => 'value',
];
$data[$table_name][$field_name . '_value']['filter']['id'] = 'country_code';
$data[$table_name][$field_name . '_value']['filter']['id'] = 'country';
$data[$table_name][$field_name . '_value']['sort']['id'] = 'country';
}
}
......
......@@ -157,14 +157,18 @@ field.widget.settings.address_zone_default:
type: boolean
label: 'Show the zone label field'
views.field.country_code:
views.field.country:
type: views_field
label: 'Country code'
label: 'Country'
mapping:
display_name:
type: boolean
label: 'Display the country name instead of the country code'
views.field.country_code:
type: views.field.country
label: 'Country code'
views.field.subdivision:
type: views_field
label: 'Subdivision'
......@@ -173,10 +177,14 @@ views.field.subdivision:
type: boolean
label: 'Display the subdivision name instead of the subdivision code'
views.filter.country_code:
views.filter.country:
type: views.filter.in_operator
label: 'Country'
views.filter.country_code:
type: views.filter.country
label: 'Country code'
views.filter.administrative_area:
type: views.filter.in_operator
label: 'Administrative area'
......
<?php
namespace Drupal\address\Plugin\views\field;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Allows the country name to be displayed instead of the country code.
*
* @ingroup views_field_handlers
*
* @ViewsField("country")
*/
class Country extends FieldPluginBase {
/**
* The country repository.
*
* @var \CommerceGuys\Addressing\Country\CountryRepositoryInterface
*/
protected $countryRepository;
/**
* Constructs a Country object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The id of the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \CommerceGuys\Addressing\Country\CountryRepositoryInterface $country_repository
* The country repository.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CountryRepositoryInterface $country_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->countryRepository = $country_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('address.country_repository')
);
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['display_name'] = ['default' => TRUE];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['display_name'] = [
'#type' => 'checkbox',
'#title' => $this->t('Display the country name instead of the country code'),
'#default_value' => !empty($this->options['display_name']),
];
parent::buildOptionsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
if (!empty($this->options['display_name']) && !empty($value)) {
$countries = $this->countryRepository->getList();
if (isset($countries[$value])) {
$value = $countries[$value];
}
}
return $this->sanitizeValue($value);
}
}
......@@ -2,93 +2,13 @@
namespace Drupal\address\Plugin\views\field;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Allows the country name to be displayed instead of the country code.
*
* @ingroup views_field_handlers
*
* @ViewsField("country_code")
*
* @deprecated in 1.5, to be removed before 2.x. Use the Country plugin instead.
*/
class CountryCode extends FieldPluginBase {
/**
* The country repository.
*
* @var \CommerceGuys\Addressing\Country\CountryRepositoryInterface
*/
protected $countryRepository;
/**
* Constructs a CountryCode object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The id of the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \CommerceGuys\Addressing\Country\CountryRepositoryInterface $country_repository
* The country repository.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, CountryRepositoryInterface $country_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->countryRepository = $country_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('address.country_repository')
);
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['display_name'] = ['default' => TRUE];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['display_name'] = [
'#type' => 'checkbox',
'#title' => $this->t('Display the country name instead of the country code'),
'#default_value' => !empty($this->options['display_name']),
];
parent::buildOptionsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
if (!empty($this->options['display_name']) && !empty($value)) {
$countries = $this->countryRepository->getList();
if (isset($countries[$value])) {
$value = $countries[$value];
}
}
return $this->sanitizeValue($value);
}
}
class CountryCode extends Country {}
<?php
namespace Drupal\address\Plugin\views\filter;
/**
* Filter by country.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("country")
*/
class Country extends CountryAwareInOperatorBase {
/**
* {@inheritdoc}
*/
public function getValueOptions() {
if (!isset($this->valueOptions)) {
$this->valueOptions = $this->getAvailableCountries();
}
return $this->valueOptions;
}
}
......@@ -8,18 +8,7 @@ namespace Drupal\address\Plugin\views\filter;
* @ingroup views_filter_handlers
*
* @ViewsFilter("country_code")
*
* @deprecated in 1.5, to be removed before 2.x. Use the Country plugin instead.
*/
class CountryCode extends CountryAwareInOperatorBase {
/**
* {@inheritdoc}
*/
public function getValueOptions() {
if (!isset($this->valueOptions)) {
$this->valueOptions = $this->getAvailableCountries();
}
return $this->valueOptions;
}
}
class CountryCode extends Country {}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment