Coverage index

src/Plugin/views/field/VisitorsBrowser.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
namespace Drupal\visitors\Plugin\views\field;
4
5
use DeviceDetector\Parser\Client\Browser;
6
use Drupal\Component\Utility\Xss as UtilityXss;
7
use Drupal\Core\Extension\ModuleHandlerInterface;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\views\Attribute\ViewsField;
10
use Drupal\views\Plugin\views\field\FieldPluginBase;
11
use Drupal\views\Render\ViewsRenderPipelineMarkup;
12
use Drupal\views\ResultRow;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Field handler to display the browser version of the visitors.
18
 *
19
 * @ingroup views_field_handlers
20
 */
21
#[ViewsField("visitors_browser")]
22
final class VisitorsBrowser 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) {
6011
    parent::__construct($configuration, $plugin_id, $plugin_definition);
61
6211
    $this->moduleHandler = $module_handler;
6311
    $this->request = $request;
64
6511
    $base_path = $this->request->getBasePath();
6611
    $visitors_path = $this->moduleHandler->getModule('visitors')->getPath();
6711
    $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) {
7411
    return new self(
7511
      $configuration,
7611
      $plugin_id,
7711
      $plugin_definition,
7811
      $container->get('module_handler'),
7911
      $container->get('request_stack')->getCurrentRequest(),
8011
    );
81
  }
82
83
  /**
84
   * Define the available options.
85
   *
86
   * @return array
87
   *   The available options.
88
   */
89
  protected function defineOptions() {
9011
    $options = parent::defineOptions();
9111
    $options['icon'] = ['default' => TRUE];
92
9311
    return $options;
94
  }
95
96
  /**
97
   * Provide the options form.
98
   */
99
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
100
1011
    $form['icon'] = [
1021
      '#title' => $this->t('Show icon'),
1031
      '#type' => 'checkbox',
1041
      '#default_value' => $this->options['icon'],
1051
    ];
106
1071
    parent::buildOptionsForm($form, $form_state);
108
  }
109
110
  /**
111
   * {@inheritdoc}
112
   */
113
  public function render(ResultRow $values) {
1141
    $output = '';
1151
    $short_name = $this->getValue($values) ?? '';
1161
    $available_browsers = Browser::getAvailableBrowsers();
1171
    $browser_name = $available_browsers[$short_name] ?? $short_name;
118
1191
    $image = $this->path . '/icons/browsers/' . $short_name . '.png';
1201
    if ($this->options['icon']) {
1211
      $output .= '<img src="' . $image . '" width="16" height="16" /> ';
122
    }
123
1241
    $output .= $browser_name;
125
1261
    return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output));
127
  }
128
129
}