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 Drupal\visitors\VisitorsLocationInterface; | |
| 13 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 14 | use Symfony\Component\HttpFoundation\Request; | |
| 15 | ||
| 16 | /** | |
| 17 | * Field handler to display the country of the visitors. | |
| 18 | * | |
| 19 | * @ingroup views_field_handlers | |
| 20 | */ | |
| 21 | #[ViewsField("visitors_country")] | |
| 22 | final class VisitorsCountry 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 | * The location service. | |
| 47 | * | |
| 48 | * @var \Drupal\visitors\VisitorsLocationInterface | |
| 49 | */ | |
| 50 | protected $location; | |
| 51 | ||
| 52 | /** | |
| 53 | * Browser version field. | |
| 54 | * | |
| 55 | * @param array $configuration | |
| 56 | * The plugin configuration. | |
| 57 | * @param string $plugin_id | |
| 58 | * The plugin ID. | |
| 59 | * @param mixed $plugin_definition | |
| 60 | * The plugin definition. | |
| 61 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | |
| 62 | * The module handler. | |
| 63 | * @param \Symfony\Component\HttpFoundation\Request $request | |
| 64 | * The request. | |
| 65 | * @param \Drupal\visitors\VisitorsLocationInterface $location | |
| 66 | * The location service. | |
| 67 | */ | |
| 68 | public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, Request $request, VisitorsLocationInterface $location) { | |
| 69 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 70 | ||
| 71 | 11 | $this->moduleHandler = $module_handler; |
| 72 | 11 | $this->request = $request; |
| 73 | ||
| 74 | 11 | $base_path = $this->request->getBasePath(); |
| 75 | 11 | $visitors_path = $this->moduleHandler->getModule('visitors')->getPath(); |
| 76 | 11 | $this->path = $base_path . '/' . $visitors_path; |
| 77 | ||
| 78 | 11 | $this->location = $location; |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * {@inheritdoc} | |
| 83 | */ | |
| 84 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
| 85 | 11 | return new self( |
| 86 | 11 | $configuration, |
| 87 | 11 | $plugin_id, |
| 88 | 11 | $plugin_definition, |
| 89 | 11 | $container->get('module_handler'), |
| 90 | 11 | $container->get('request_stack')->getCurrentRequest(), |
| 91 | 11 | $container->get('visitors.location'), |
| 92 | 11 | ); |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Define the available options. | |
| 97 | * | |
| 98 | * @return array | |
| 99 | * The available options. | |
| 100 | */ | |
| 101 | protected function defineOptions() { | |
| 102 | 11 | $options = parent::defineOptions(); |
| 103 | 11 | $options['icon'] = ['default' => TRUE]; |
| 104 | 11 | $options['text'] = ['default' => TRUE]; |
| 105 | 11 | $options['abbreviation'] = ['default' => FALSE]; |
| 106 | ||
| 107 | 11 | return $options; |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * Provide the options form. | |
| 112 | */ | |
| 113 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 114 | ||
| 115 | 1 | $form['icon'] = [ |
| 116 | 1 | '#title' => $this->t('Show flag'), |
| 117 | 1 | '#type' => 'checkbox', |
| 118 | 1 | '#default_value' => $this->options['icon'], |
| 119 | 1 | ]; |
| 120 | 1 | $form['text'] = [ |
| 121 | 1 | '#title' => $this->t('Show name'), |
| 122 | 1 | '#type' => 'checkbox', |
| 123 | 1 | '#default_value' => $this->options['text'], |
| 124 | 1 | ]; |
| 125 | ||
| 126 | 1 | $form['abbreviation'] = [ |
| 127 | 1 | '#title' => $this->t('Use abbreviation'), |
| 128 | 1 | '#type' => 'checkbox', |
| 129 | 1 | '#default_value' => $this->options['abbreviation'], |
| 130 | 1 | ]; |
| 131 | ||
| 132 | 1 | parent::buildOptionsForm($form, $form_state); |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * {@inheritdoc} | |
| 137 | */ | |
| 138 | public function render(ResultRow $values) { | |
| 139 | ||
| 140 | 1 | $output = ''; |
| 141 | 1 | $code = $this->getValue($values); |
| 142 | 1 | $value = $code; |
| 143 | 1 | if (!$this->options['abbreviation']) { |
| 144 | 1 | $value = $this->location->getCountryLabel($code); |
| 145 | } | |
| 146 | ||
| 147 | 1 | if ($this->options['icon']) { |
| 148 | 1 | $flag = $code ? strtolower($code) : 'xx'; |
| 149 | 1 | $image = $this->path . "/icons/flags/$flag.png"; |
| 150 | ||
| 151 | 1 | $output .= '<img src="' . $image . '" width="16" height="16" /> '; |
| 152 | } | |
| 153 | 1 | if ($this->options['text'] || $this->options['abbreviation']) { |
| 154 | 1 | $output .= $value; |
| 155 | } | |
| 156 | ||
| 157 | 1 | return ViewsRenderPipelineMarkup::create(UtilityXss::filterAdmin($output)); |
| 158 | } | |
| 159 | ||
| 160 | } |