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\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) { | |
| 46 | 6 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 47 | ||
| 48 | 6 | $base_path = $request->getBasePath(); |
| 49 | 6 | $visitors_path = $module_handler->getModule('visitors')->getPath(); |
| 50 | 6 | $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) { | |
| 57 | 6 | return new static( |
| 58 | 6 | $configuration, |
| 59 | 6 | $plugin_id, |
| 60 | 6 | $plugin_definition, |
| 61 | 6 | $container->get('module_handler'), |
| 62 | 6 | $container->get('request_stack')->getCurrentRequest(), |
| 63 | 6 | ); |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Define the available options. | |
| 68 | * | |
| 69 | * @return array | |
| 70 | * The available options. | |
| 71 | */ | |
| 72 | protected function defineOptions() { | |
| 73 | 1 | $options = parent::defineOptions(); |
| 74 | 1 | $options['icon'] = ['default' => TRUE]; |
| 75 | ||
| 76 | 1 | return $options; |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * Provide the options form. | |
| 81 | */ | |
| 82 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 83 | 1 | parent::buildOptionsForm($form, $form_state); |
| 84 | ||
| 85 | 1 | $form['icon'] = [ |
| 86 | 1 | '#title' => $this->t('Show icon'), |
| 87 | 1 | '#type' => 'checkbox', |
| 88 | 1 | '#default_value' => $this->options['icon'], |
| 89 | 1 | ]; |
| 90 | ||
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * {@inheritdoc} | |
| 95 | */ | |
| 96 | public function render(ResultRow $values) { | |
| 97 | ||
| 98 | 2 | $output = ''; |
| 99 | 2 | $value = $this->getValue($values); |
| 100 | ||
| 101 | 2 | if ($this->options['icon']) { |
| 102 | 1 | if ($value) { |
| 103 | 1 | $image = $this->path . '/icons/plugins/' . static::ICON; |
| 104 | 1 | $output = '<img src="' . $image . '" width="16" height="16" />'; |
| 105 | } | |
| 106 | } | |
| 107 | else { | |
| 108 | 1 | $output = $value; |
| 109 | } | |
| 110 | ||
| 111 | 2 | return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output)); |
| 112 | } | |
| 113 | ||
| 114 | } |