CountryCode.php 2.54 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\address\Plugin\views\field;

5
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
6
7
8
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
12
13
14
15
16
17
18
19

/**
 * Allows the country name to be displayed instead of the country code.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("country_code")
 */
class CountryCode extends FieldPluginBase {

20
21
22
  /**
   * The country repository.
   *
23
   * @var \CommerceGuys\Addressing\Country\CountryRepositoryInterface
24
25
26
27
28
29
30
31
   */
  protected $countryRepository;

  /**
   * Constructs a CountryCode object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
32
   * @param string $plugin_id
33
   *   The id of the plugin instance.
34
   * @param mixed $plugin_definition
35
   *   The plugin implementation definition.
36
   * @param \CommerceGuys\Addressing\Country\CountryRepositoryInterface $country_repository
37
38
   *   The country repository.
   */
39
40
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CountryRepositoryInterface $country_repository) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
41

42
    $this->countryRepository = $country_repository;
43
44
45
46
47
  }

  /**
   * {@inheritdoc}
   */
48
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49
50
    return new static(
      $configuration,
51
52
      $plugin_id,
      $plugin_definition,
53
54
55
56
      $container->get('address.country_repository')
    );
  }

57
58
59
60
61
  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
bojanz's avatar
bojanz committed
62
    $options['display_name'] = ['default' => TRUE];
63
64
65
66
67
68
69
70

    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
bojanz's avatar
bojanz committed
71
    $form['display_name'] = [
72
      '#type' => 'checkbox',
73
      '#title' => $this->t('Display the country name instead of the country code'),
74
      '#default_value' => !empty($this->options['display_name']),
bojanz's avatar
bojanz committed
75
    ];
76
77
78
79
80
81
82
83
    parent::buildOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $value = $this->getValue($values);
84
    if (!empty($this->options['display_name']) && !empty($value)) {
85
      $countries = $this->countryRepository->getList();
86
87
88
      if (isset($countries[$value])) {
        $value = $countries[$value];
      }
89
90
91
92
93
94
    }

    return $this->sanitizeValue($value);
  }

}