Commit 345dbaa5 authored by bojanz's avatar bojanz
Browse files

Issue #2954927 by jsacksick, bojanz: Add a display_name setting to the Subdivision views field

parent 45c2d9a8
...@@ -157,6 +157,22 @@ field.widget.settings.address_zone_default: ...@@ -157,6 +157,22 @@ field.widget.settings.address_zone_default:
type: boolean type: boolean
label: 'Show the zone label field' label: 'Show the zone label field'
views.field.country_code:
type: views_field
label: 'Country code'
mapping:
display_name:
type: boolean
label: 'Display the country name instead of the country code'
views.field.subdivision:
type: views_field
label: 'Subdivision'
mapping:
display_name:
type: boolean
label: 'Display the subdivision name instead of the subdivision code'
views.filter.country_code: views.filter.country_code:
type: views.filter.in_operator type: views.filter.in_operator
label: 'Country' label: 'Country'
......
...@@ -70,7 +70,7 @@ class CountryCode extends FieldPluginBase { ...@@ -70,7 +70,7 @@ class CountryCode extends FieldPluginBase {
public function buildOptionsForm(&$form, FormStateInterface $form_state) { public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['display_name'] = [ $form['display_name'] = [
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => $this->t('Display the localized country name instead of the two character country code'), '#title' => $this->t('Display the country name instead of the country code'),
'#default_value' => !empty($this->options['display_name']), '#default_value' => !empty($this->options['display_name']),
]; ];
parent::buildOptionsForm($form, $form_state); parent::buildOptionsForm($form, $form_state);
......
...@@ -4,6 +4,7 @@ namespace Drupal\address\Plugin\views\field; ...@@ -4,6 +4,7 @@ namespace Drupal\address\Plugin\views\field;
use CommerceGuys\Addressing\Locale; use CommerceGuys\Addressing\Locale;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface; use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow; use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -54,13 +55,35 @@ class Subdivision extends FieldPluginBase { ...@@ -54,13 +55,35 @@ class Subdivision extends FieldPluginBase {
); );
} }
/**
* {@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 subdivision name instead of the subdivision code'),
'#default_value' => !empty($this->options['display_name']),
];
parent::buildOptionsForm($form, $form_state);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function render(ResultRow $values) { public function render(ResultRow $values) {
$value = $this->getValue($values); $value = $this->getValue($values);
if (empty($value)) { if (empty($value) || empty($this->options['display_name'])) {
return ''; return $this->sanitizeValue($value);
} }
$entity = $this->getEntity($values); $entity = $this->getEntity($values);
...@@ -93,7 +116,6 @@ class Subdivision extends FieldPluginBase { ...@@ -93,7 +116,6 @@ class Subdivision extends FieldPluginBase {
} }
/** @var \CommerceGuys\Addressing\Subdivision\Subdivision $subdivision */ /** @var \CommerceGuys\Addressing\Subdivision\Subdivision $subdivision */
$subdivision = $this->subdivisionRepository->get($code, $parents); $subdivision = $this->subdivisionRepository->get($code, $parents);
// @todo Allow a choice between subdivision code and name.
if ($subdivision) { if ($subdivision) {
$use_local_name = Locale::matchCandidates($address->getLocale(), $subdivision->getLocale()); $use_local_name = Locale::matchCandidates($address->getLocale(), $subdivision->getLocale());
$value = $use_local_name ? $subdivision->getLocalName() : $subdivision->getName(); $value = $use_local_name ? $subdivision->getLocalName() : $subdivision->getName();
......
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