Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
35 / 35 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
VisitorsBrand | |
100.00% |
35 / 35 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
defineOptions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
buildOptionsForm | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 |
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 Symfony\Component\DependencyInjection\ContainerInterface; |
12 | use Symfony\Component\HttpFoundation\Request; |
13 | |
14 | /** |
15 | * Field handler to display the device brand of the visitors. |
16 | * |
17 | * @ingroup views_field_handlers |
18 | * |
19 | * @ViewsField("visitors_brand") |
20 | */ |
21 | final class VisitorsBrand extends FieldPluginBase { |
22 | |
23 | /** |
24 | * The module handler. |
25 | * |
26 | * @var \Drupal\Core\Extension\ModuleHandlerInterface |
27 | */ |
28 | protected $moduleHandler; |
29 | |
30 | /** |
31 | * The request. |
32 | * |
33 | * @var \Symfony\Component\HttpFoundation\Request |
34 | */ |
35 | protected $request; |
36 | |
37 | /** |
38 | * The path. |
39 | * |
40 | * @var string |
41 | */ |
42 | protected $path; |
43 | |
44 | /** |
45 | * Browser version field. |
46 | * |
47 | * @param array $configuration |
48 | * The plugin configuration. |
49 | * @param string $plugin_id |
50 | * The plugin ID. |
51 | * @param mixed $plugin_definition |
52 | * The plugin definition. |
53 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
54 | * The module handler. |
55 | * @param \Symfony\Component\HttpFoundation\Request $request |
56 | * The request. |
57 | */ |
58 | public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, Request $request) { |
59 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
60 | |
61 | $this->moduleHandler = $module_handler; |
62 | $this->request = $request; |
63 | |
64 | $base_path = $this->request->getBasePath(); |
65 | $visitors_path = $this->moduleHandler->getModule('visitors')->getPath(); |
66 | $this->path = $base_path . '/' . $visitors_path; |
67 | } |
68 | |
69 | /** |
70 | * {@inheritdoc} |
71 | */ |
72 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
73 | return new self( |
74 | $configuration, |
75 | $plugin_id, |
76 | $plugin_definition, |
77 | $container->get('module_handler'), |
78 | $container->get('request_stack')->getCurrentRequest(), |
79 | ); |
80 | } |
81 | |
82 | /** |
83 | * Define the available options. |
84 | * |
85 | * @return array |
86 | * The available options. |
87 | */ |
88 | protected function defineOptions() { |
89 | $options = parent::defineOptions(); |
90 | $options['icon'] = ['default' => TRUE]; |
91 | |
92 | return $options; |
93 | } |
94 | |
95 | /** |
96 | * Provide the options form. |
97 | */ |
98 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { |
99 | |
100 | $form['icon'] = [ |
101 | '#title' => $this->t('Show icon'), |
102 | '#type' => 'checkbox', |
103 | '#default_value' => $this->options['icon'], |
104 | ]; |
105 | |
106 | parent::buildOptionsForm($form, $form_state); |
107 | } |
108 | |
109 | /** |
110 | * {@inheritdoc} |
111 | */ |
112 | public function render(ResultRow $values) { |
113 | |
114 | $output = ''; |
115 | $brand = $this->getValue($values); |
116 | $brand_image = $brand; |
117 | if (empty($brand)) { |
118 | $brand_image = 'unk'; |
119 | $brand = 'unknown'; |
120 | if (!$this->options['icon']) { |
121 | return ''; |
122 | } |
123 | } |
124 | |
125 | $image = $this->path . '/icons/brand/' . $brand_image . '.png'; |
126 | if ($this->options['icon']) { |
127 | $output .= '<img src="' . $image . '" width="16" height="16" /> '; |
128 | } |
129 | |
130 | $output .= ucwords($brand); |
131 | |
132 | return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output)); |
133 | } |
134 | |
135 | } |