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