Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| VisitorsBrowser | |
100.00% |
31 / 31 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| defineOptions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| buildOptionsForm | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 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) { |
| 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 | $available_browsers = Browser::getAvailableBrowsers(); |
| 117 | $browser_name = $available_browsers[$short_name] ?? $short_name; |
| 118 | |
| 119 | $image = $this->path . '/icons/browsers/' . $short_name . '.png'; |
| 120 | if ($this->options['icon']) { |
| 121 | $output .= '<img src="' . $image . '" width="16" height="16" /> '; |
| 122 | } |
| 123 | |
| 124 | $output .= $browser_name; |
| 125 | |
| 126 | return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output)); |
| 127 | } |
| 128 | |
| 129 | } |