Coverage index

src/Plugin/views/field/VisitorsCountry.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
namespace Drupal\visitors\Plugin\views\field;
4
5
use Drupal\Component\Utility\Xss as UtilityXss;
6
use Drupal\Core\Extension\ModuleHandlerInterface;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\views\Attribute\ViewsField;
9
use Drupal\views\Plugin\views\field\FieldPluginBase;
10
use Drupal\views\Render\ViewsRenderPipelineMarkup;
11
use Drupal\views\ResultRow;
12
use Drupal\visitors\VisitorsLocationInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Field handler to display the country of the visitors.
18
 *
19
 * @ingroup views_field_handlers
20
 */
21
#[ViewsField("visitors_country")]
22
final class VisitorsCountry extends FieldPluginBase {
23
24
  /**
25
   * The module handler.
26
   *
27
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
28
   */
29
  protected $moduleHandler;
30
31
  /**
32
   * The request.
33
   *
34
   * @var \Symfony\Component\HttpFoundation\Request
35
   */
36
  protected $request;
37
38
  /**
39
   * The path.
40
   *
41
   * @var string
42
   */
43
  protected $path;
44
45
  /**
46
   * The location service.
47
   *
48
   * @var \Drupal\visitors\VisitorsLocationInterface
49
   */
50
  protected $location;
51
52
  /**
53
   * Browser version field.
54
   *
55
   * @param array $configuration
56
   *   The plugin configuration.
57
   * @param string $plugin_id
58
   *   The plugin ID.
59
   * @param mixed $plugin_definition
60
   *   The plugin definition.
61
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
62
   *   The module handler.
63
   * @param \Symfony\Component\HttpFoundation\Request $request
64
   *   The request.
65
   * @param \Drupal\visitors\VisitorsLocationInterface $location
66
   *   The location service.
67
   */
68
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, Request $request, VisitorsLocationInterface $location) {
6911
    parent::__construct($configuration, $plugin_id, $plugin_definition);
70
7111
    $this->moduleHandler = $module_handler;
7211
    $this->request = $request;
73
7411
    $base_path = $this->request->getBasePath();
7511
    $visitors_path = $this->moduleHandler->getModule('visitors')->getPath();
7611
    $this->path = $base_path . '/' . $visitors_path;
77
7811
    $this->location = $location;
79
  }
80
81
  /**
82
   * {@inheritdoc}
83
   */
84
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
8511
    return new self(
8611
      $configuration,
8711
      $plugin_id,
8811
      $plugin_definition,
8911
      $container->get('module_handler'),
9011
      $container->get('request_stack')->getCurrentRequest(),
9111
      $container->get('visitors.location'),
9211
    );
93
  }
94
95
  /**
96
   * Define the available options.
97
   *
98
   * @return array
99
   *   The available options.
100
   */
101
  protected function defineOptions() {
10211
    $options = parent::defineOptions();
10311
    $options['icon'] = ['default' => TRUE];
10411
    $options['text'] = ['default' => TRUE];
10511
    $options['abbreviation'] = ['default' => FALSE];
106
10711
    return $options;
108
  }
109
110
  /**
111
   * Provide the options form.
112
   */
113
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
114
1151
    $form['icon'] = [
1161
      '#title' => $this->t('Show flag'),
1171
      '#type' => 'checkbox',
1181
      '#default_value' => $this->options['icon'],
1191
    ];
1201
    $form['text'] = [
1211
      '#title' => $this->t('Show name'),
1221
      '#type' => 'checkbox',
1231
      '#default_value' => $this->options['text'],
1241
    ];
125
1261
    $form['abbreviation'] = [
1271
      '#title' => $this->t('Use abbreviation'),
1281
      '#type' => 'checkbox',
1291
      '#default_value' => $this->options['abbreviation'],
1301
    ];
131
1321
    parent::buildOptionsForm($form, $form_state);
133
  }
134
135
  /**
136
   * {@inheritdoc}
137
   */
138
  public function render(ResultRow $values) {
139
1401
    $output = '';
1411
    $code = $this->getValue($values);
1421
    $value = $code;
1431
    if (!$this->options['abbreviation']) {
1441
      $value = $this->location->getCountryLabel($code);
145
    }
146
1471
    if ($this->options['icon']) {
1481
      $flag = $code ? strtolower($code) : 'xx';
1491
      $image = $this->path . "/icons/flags/$flag.png";
150
1511
      $output .= '<img src="' . $image . '" width="16" height="16" /> ';
152
    }
1531
    if ($this->options['text'] || $this->options['abbreviation']) {
1541
      $output .= $value;
155
    }
156
1571
    return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output));
158
  }
159
160
}