Coverage index

src/Plugin/views/field/VisitorsBrowserPlugin.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\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
class VisitorsBrowserPlugin extends FieldPluginBase {
18
19
  /**
20
   * The path.
21
   *
22
   * @var string
23
   */
24
  protected $path;
25
26
  /**
27
   * The plugin icon.
28
   */
29
  const ICON = '';
30
31
  /**
32
   * Browser plugin.
33
   *
34
   * @param array $configuration
35
   *   The plugin configuration.
36
   * @param string $plugin_id
37
   *   The plugin ID.
38
   * @param mixed $plugin_definition
39
   *   The plugin definition.
40
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
41
   *   The module handler.
42
   * @param \Symfony\Component\HttpFoundation\Request $request
43
   *   The request.
44
   */
45
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, Request $request) {
466
    parent::__construct($configuration, $plugin_id, $plugin_definition);
47
486
    $base_path = $request->getBasePath();
496
    $visitors_path = $module_handler->getModule('visitors')->getPath();
506
    $this->path = $base_path . '/' . $visitors_path;
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
576
    return new static(
586
      $configuration,
596
      $plugin_id,
606
      $plugin_definition,
616
      $container->get('module_handler'),
626
      $container->get('request_stack')->getCurrentRequest(),
636
    );
64
  }
65
66
  /**
67
   * Define the available options.
68
   *
69
   * @return array
70
   *   The available options.
71
   */
72
  protected function defineOptions() {
731
    $options = parent::defineOptions();
741
    $options['icon'] = ['default' => TRUE];
75
761
    return $options;
77
  }
78
79
  /**
80
   * Provide the options form.
81
   */
82
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
831
    parent::buildOptionsForm($form, $form_state);
84
851
    $form['icon'] = [
861
      '#title' => $this->t('Show icon'),
871
      '#type' => 'checkbox',
881
      '#default_value' => $this->options['icon'],
891
    ];
90
91
  }
92
93
  /**
94
   * {@inheritdoc}
95
   */
96
  public function render(ResultRow $values) {
97
982
    $output = '';
992
    $value = $this->getValue($values);
100
1012
    if ($this->options['icon']) {
1021
      if ($value) {
1031
        $image = $this->path . '/icons/plugins/' . static::ICON;
1041
        $output = '<img src="' . $image . '" width="16" height="16" />';
105
      }
106
    }
107
    else {
1081
      $output = $value;
109
    }
110
1112
    return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output));
112
  }
113
114
}