Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Plugin\views\field; | |
| 4 | ||
| 5 | use DeviceDetector\Parser\OperatingSystem; | |
| 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 OperatingSystem of the visitor. | |
| 18 | * | |
| 19 | * @ingroup views_field_handlers | |
| 20 | */ | |
| 21 | #[ViewsField("visitors_operating_system")] | |
| 22 | final 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 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 61 | ||
| 62 | 11 | $this->moduleHandler = $module_handler; |
| 63 | 11 | $this->request = $request; |
| 64 | ||
| 65 | 11 | $base_path = $this->request->getBasePath(); |
| 66 | 11 | $visitors_path = $this->moduleHandler->getModule('visitors')->getPath(); |
| 67 | 11 | $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 | 11 | return new self( |
| 75 | 11 | $configuration, |
| 76 | 11 | $plugin_id, |
| 77 | 11 | $plugin_definition, |
| 78 | 11 | $container->get('module_handler'), |
| 79 | 11 | $container->get('request_stack')->getCurrentRequest(), |
| 80 | 11 | ); |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Define the available options. | |
| 85 | * | |
| 86 | * @return array | |
| 87 | * The available options. | |
| 88 | */ | |
| 89 | protected function defineOptions() { | |
| 90 | 11 | $options = parent::defineOptions(); |
| 91 | 11 | $options['icon'] = ['default' => TRUE]; |
| 92 | ||
| 93 | 11 | return $options; |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Provide the options form. | |
| 98 | */ | |
| 99 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 100 | ||
| 101 | 1 | $form['icon'] = [ |
| 102 | 1 | '#title' => $this->t('Show icon'), |
| 103 | 1 | '#type' => 'checkbox', |
| 104 | 1 | '#default_value' => $this->options['icon'], |
| 105 | 1 | ]; |
| 106 | ||
| 107 | 1 | parent::buildOptionsForm($form, $form_state); |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * {@inheritdoc} | |
| 112 | */ | |
| 113 | public function render(ResultRow $values) { | |
| 114 | 2 | $output = ''; |
| 115 | 2 | $short_name = $this->getValue($values); |
| 116 | 2 | $os_name = OperatingSystem::getOsFamily($short_name); |
| 117 | ||
| 118 | 2 | $image = $this->path . '/icons/os/' . $short_name . '.png'; |
| 119 | 2 | if ($this->options['icon']) { |
| 120 | 1 | $output .= '<img src="' . $image . '" width="16" height="16" /> '; |
| 121 | } | |
| 122 | ||
| 123 | 2 | $output .= $os_name; |
| 124 | ||
| 125 | 2 | return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output)); |
| 126 | } | |
| 127 | ||
| 128 | } |