Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
48 / 48 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
VisitorsCountry | |
100.00% |
48 / 48 |
|
100.00% |
5 / 5 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
defineOptions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
buildOptionsForm | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
6 |
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\Plugin\views\field\FieldPluginBase; |
9 | use Drupal\views\Render\ViewsRenderPipelineMarkup; |
10 | use Drupal\views\ResultRow; |
11 | use Drupal\visitors\VisitorsLocationInterface; |
12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
13 | use Symfony\Component\HttpFoundation\Request; |
14 | |
15 | /** |
16 | * Field handler to display the device brand of the visitors. |
17 | * |
18 | * @ingroup views_field_handlers |
19 | * |
20 | * @ViewsField("visitors_country") |
21 | */ |
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) { |
69 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
70 | |
71 | $this->moduleHandler = $module_handler; |
72 | $this->request = $request; |
73 | |
74 | $base_path = $this->request->getBasePath(); |
75 | $visitors_path = $this->moduleHandler->getModule('visitors')->getPath(); |
76 | $this->path = $base_path . '/' . $visitors_path; |
77 | |
78 | $this->location = $location; |
79 | } |
80 | |
81 | /** |
82 | * {@inheritdoc} |
83 | */ |
84 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
85 | return new self( |
86 | $configuration, |
87 | $plugin_id, |
88 | $plugin_definition, |
89 | $container->get('module_handler'), |
90 | $container->get('request_stack')->getCurrentRequest(), |
91 | $container->get('visitors.location'), |
92 | ); |
93 | } |
94 | |
95 | /** |
96 | * Define the available options. |
97 | * |
98 | * @return array |
99 | * The available options. |
100 | */ |
101 | protected function defineOptions() { |
102 | $options = parent::defineOptions(); |
103 | $options['icon'] = ['default' => TRUE]; |
104 | $options['text'] = ['default' => TRUE]; |
105 | $options['abbreviation'] = ['default' => FALSE]; |
106 | |
107 | return $options; |
108 | } |
109 | |
110 | /** |
111 | * Provide the options form. |
112 | */ |
113 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { |
114 | |
115 | $form['icon'] = [ |
116 | '#title' => $this->t('Show flag'), |
117 | '#type' => 'checkbox', |
118 | '#default_value' => $this->options['icon'], |
119 | ]; |
120 | $form['text'] = [ |
121 | '#title' => $this->t('Show name'), |
122 | '#type' => 'checkbox', |
123 | '#default_value' => $this->options['text'], |
124 | ]; |
125 | |
126 | $form['abbreviation'] = [ |
127 | '#title' => $this->t('Use abbreviation'), |
128 | '#type' => 'checkbox', |
129 | '#default_value' => $this->options['abbreviation'], |
130 | ]; |
131 | |
132 | parent::buildOptionsForm($form, $form_state); |
133 | } |
134 | |
135 | /** |
136 | * {@inheritdoc} |
137 | */ |
138 | public function render(ResultRow $values) { |
139 | |
140 | $output = ''; |
141 | $code = $this->getValue($values); |
142 | $value = $code; |
143 | if (!$this->options['abbreviation']) { |
144 | $value = $this->location->getCountryLabel($code); |
145 | } |
146 | |
147 | if ($this->options['icon']) { |
148 | $flag = $code ? strtolower($code) : 'xx'; |
149 | $image = $this->path . "/icons/flags/$flag.png"; |
150 | |
151 | $output .= '<img src="' . $image . '" width="16" height="16" /> '; |
152 | } |
153 | if ($this->options['text'] || $this->options['abbreviation']) { |
154 | $output .= $value; |
155 | } |
156 | |
157 | return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output)); |
158 | } |
159 | |
160 | } |