Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
VisitorsOperatingSystem
100.00% covered (success)
100.00%
30 / 30
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%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Drupal\visitors\Plugin\views\field;
4
5use DeviceDetector\Parser\OperatingSystem;
6use Drupal\Component\Utility\Xss as UtilityXss;
7use Drupal\Core\Extension\ModuleHandlerInterface;
8use Drupal\Core\Form\FormStateInterface;
9use Drupal\views\Plugin\views\field\FieldPluginBase;
10use Drupal\views\Render\ViewsRenderPipelineMarkup;
11use Drupal\views\ResultRow;
12use Symfony\Component\DependencyInjection\ContainerInterface;
13use Symfony\Component\HttpFoundation\Request;
14
15/**
16 * Field handler to display the OperatingSystem of the visitor.
17 *
18 * @ingroup views_field_handlers
19 *
20 * @ViewsField("visitors_operating_system")
21 */
22final class VisitorsOperatingSystem 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   * Browser version field.
47   *
48   * @param array $configuration
49   *   The plugin configuration.
50   * @param string $plugin_id
51   *   The plugin ID.
52   * @param mixed $plugin_definition
53   *   The plugin definition.
54   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
55   *   The module handler.
56   * @param \Symfony\Component\HttpFoundation\Request $request
57   *   The request.
58   */
59  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, Request $request) {
60    parent::__construct($configuration, $plugin_id, $plugin_definition);
61
62    $this->moduleHandler = $module_handler;
63    $this->request = $request;
64
65    $base_path = $this->request->getBasePath();
66    $visitors_path = $this->moduleHandler->getModule('visitors')->getPath();
67    $this->path = $base_path . '/' . $visitors_path;
68  }
69
70  /**
71   * {@inheritdoc}
72   */
73  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
74    return new self(
75      $configuration,
76      $plugin_id,
77      $plugin_definition,
78      $container->get('module_handler'),
79      $container->get('request_stack')->getCurrentRequest(),
80    );
81  }
82
83  /**
84   * Define the available options.
85   *
86   * @return array
87   *   The available options.
88   */
89  protected function defineOptions() {
90    $options = parent::defineOptions();
91    $options['icon'] = ['default' => TRUE];
92
93    return $options;
94  }
95
96  /**
97   * Provide the options form.
98   */
99  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
100
101    $form['icon'] = [
102      '#title' => $this->t('Show icon'),
103      '#type' => 'checkbox',
104      '#default_value' => $this->options['icon'],
105    ];
106
107    parent::buildOptionsForm($form, $form_state);
108  }
109
110  /**
111   * {@inheritdoc}
112   */
113  public function render(ResultRow $values) {
114    $output = '';
115    $short_name = $this->getValue($values);
116    $os_name = OperatingSystem::getOsFamily($short_name);
117
118    $image = $this->path . '/icons/os/' . $short_name . '.png';
119    if ($this->options['icon']) {
120      $output .= '<img src="' . $image . '" width="16" height="16" /> ';
121    }
122
123    $output .= $os_name;
124
125    return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output));
126  }
127
128}