Country.php in Address 8
File
src/Plugin/views/field/Country.php
View source
<?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;
class Country extends FieldPluginBase {
protected $countryRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CountryRepositoryInterface $country_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->countryRepository = $country_repository;
}
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'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['display_name'] = [
'default' => TRUE,
];
return $options;
}
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);
}
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);
}
}
Classes
Name |
Description |
Country |
Allows the country name to be displayed instead of the country code. |