Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 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 brand of the visitors. | |
| 17 | * | |
| 18 | * @ingroup views_field_handlers | |
| 19 | */ | |
| 20 | #[ViewsField("visitors_brand")] | |
| 21 | final class VisitorsBrand 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) { | |
| 59 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 60 | ||
| 61 | 11 | $this->moduleHandler = $module_handler; |
| 62 | 11 | $this->request = $request; |
| 63 | ||
| 64 | 11 | $base_path = $this->request->getBasePath(); |
| 65 | 11 | $visitors_path = $this->moduleHandler->getModule('visitors')->getPath(); |
| 66 | 11 | $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) { | |
| 73 | 11 | return new self( |
| 74 | 11 | $configuration, |
| 75 | 11 | $plugin_id, |
| 76 | 11 | $plugin_definition, |
| 77 | 11 | $container->get('module_handler'), |
| 78 | 11 | $container->get('request_stack')->getCurrentRequest(), |
| 79 | 11 | ); |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Define the available options. | |
| 84 | * | |
| 85 | * @return array | |
| 86 | * The available options. | |
| 87 | */ | |
| 88 | protected function defineOptions() { | |
| 89 | 11 | $options = parent::defineOptions(); |
| 90 | 11 | $options['icon'] = ['default' => TRUE]; |
| 91 | ||
| 92 | 11 | return $options; |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Provide the options form. | |
| 97 | */ | |
| 98 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 99 | ||
| 100 | 1 | $form['icon'] = [ |
| 101 | 1 | '#title' => $this->t('Show icon'), |
| 102 | 1 | '#type' => 'checkbox', |
| 103 | 1 | '#default_value' => $this->options['icon'], |
| 104 | 1 | ]; |
| 105 | ||
| 106 | 1 | parent::buildOptionsForm($form, $form_state); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * {@inheritdoc} | |
| 111 | */ | |
| 112 | public function render(ResultRow $values) { | |
| 113 | ||
| 114 | 2 | $output = ''; |
| 115 | 2 | $brand = $this->getValue($values); |
| 116 | 2 | $brand_image = $brand; |
| 117 | 2 | if (empty($brand)) { |
| 118 | 1 | $brand_image = 'unk'; |
| 119 | 1 | $brand = 'unknown'; |
| 120 | 1 | if (!$this->options['icon']) { |
| 121 | 1 | return ''; |
| 122 | } | |
| 123 | } | |
| 124 | ||
| 125 | 1 | $image = $this->path . '/icons/brand/' . $brand_image . '.png'; |
| 126 | 1 | if ($this->options['icon']) { |
| 127 | 1 | $output .= '<img src="' . $image . '" width="16" height="16" /> '; |
| 128 | } | |
| 129 | ||
| 130 | 1 | $output .= ucwords($brand); |
| 131 | ||
| 132 | 1 | return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output)); |
| 133 | } | |
| 134 | ||
| 135 | } |