Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
VisitorsDevice
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 defineOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 buildOptionsForm
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Drupal\visitors\Plugin\views\field;
4
5use Drupal\Component\Utility\Xss as UtilityXss;
6use Drupal\Core\Extension\ModuleHandlerInterface;
7use Drupal\Core\Form\FormStateInterface;
8use Drupal\views\Plugin\views\field\FieldPluginBase;
9use Drupal\views\Render\ViewsRenderPipelineMarkup;
10use Drupal\views\ResultRow;
11use Symfony\Component\DependencyInjection\ContainerInterface;
12use Symfony\Component\HttpFoundation\Request;
13
14/**
15 * Field handler to display the browser version of the visitors.
16 *
17 * @ingroup views_field_handlers
18 *
19 * @ViewsField("visitors_device")
20 */
21final class VisitorsDevice 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    $output = '';
114    $device_type = $this->getValue($values) ?? 'unknown';
115
116    $image = $this->path . '/icons/devices/' . $device_type . '.png';
117    if ($this->options['icon']) {
118      $output .= '<img src="' . $image . '" width="16" height="16" /> ';
119    }
120
121    $output .= ucwords($device_type);
122
123    return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output));
124  }
125
126}