Coverage index

src/Plugin/views/field/VisitorsDevice.php

Lines: 100.00%

LineHitsSource
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\Attribute\ViewsField;
9
use Drupal\views\Plugin\views\field\FieldPluginBase;
10
use Drupal\views\Render\ViewsRenderPipelineMarkup;
11
use Drupal\views\ResultRow;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Field handler to display the device of the visitors.
17
 *
18
 * @ingroup views_field_handlers
19
 */
20
#[ViewsField("visitors_device")]
21
final 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) {
5911
    parent::__construct($configuration, $plugin_id, $plugin_definition);
60
6111
    $this->moduleHandler = $module_handler;
6211
    $this->request = $request;
63
6411
    $base_path = $this->request->getBasePath();
6511
    $visitors_path = $this->moduleHandler->getModule('visitors')->getPath();
6611
    $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) {
7311
    return new self(
7411
      $configuration,
7511
      $plugin_id,
7611
      $plugin_definition,
7711
      $container->get('module_handler'),
7811
      $container->get('request_stack')->getCurrentRequest(),
7911
    );
80
  }
81
82
  /**
83
   * Define the available options.
84
   *
85
   * @return array
86
   *   The available options.
87
   */
88
  protected function defineOptions() {
8911
    $options = parent::defineOptions();
9011
    $options['icon'] = ['default' => TRUE];
91
9211
    return $options;
93
  }
94
95
  /**
96
   * Provide the options form.
97
   */
98
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
99
1001
    $form['icon'] = [
1011
      '#title' => $this->t('Show icon'),
1021
      '#type' => 'checkbox',
1031
      '#default_value' => $this->options['icon'],
1041
    ];
105
1061
    parent::buildOptionsForm($form, $form_state);
107
  }
108
109
  /**
110
   * {@inheritdoc}
111
   */
112
  public function render(ResultRow $values) {
1132
    $output = '';
1142
    $device_type = $this->getValue($values) ?? 'unknown';
115
1162
    $image = $this->path . '/icons/devices/' . $device_type . '.png';
1172
    if ($this->options['icon']) {
1181
      $output .= '<img src="' . $image . '" width="16" height="16" /> ';
119
    }
120
1212
    $output .= ucwords($device_type);
122
1232
    return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output));
124
  }
125
126
}