Lines: 94.12%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Plugin\views\field; | |
| 6 | ||
| 7 | use Drupal\Core\Link; | |
| 8 | use Drupal\Core\Url; | |
| 9 | use Drupal\views\Attribute\ViewsField; | |
| 10 | use Drupal\views\Plugin\views\field\FieldPluginBase; | |
| 11 | use Drupal\views\ResultRow; | |
| 12 | ||
| 13 | /** | |
| 14 | * Links a referrer name or keyword to its drill-down report. | |
| 15 | * | |
| 16 | * An empty value uses empty_label and the argument "not-defined". | |
| 17 | */ | |
| 18 | #[ViewsField('visitors_referer_name_link')] | |
| 19 | final class RefererNameLink extends FieldPluginBase { | |
| 20 | ||
| 21 | /** | |
| 22 | * {@inheritdoc} | |
| 23 | */ | |
| 24 | protected function defineOptions() { | |
| 25 | 11 | $options = parent::defineOptions(); |
| 26 | 11 | $options['route'] = ['default' => '']; |
| 27 | 11 | $options['parameter'] = ['default' => 'referer_name']; |
| 28 | 11 | $options['empty_label'] = ['default' => '']; |
| 29 | 11 | return $options; |
| 30 | } | |
| 31 | ||
| 32 | /** | |
| 33 | * {@inheritdoc} | |
| 34 | */ | |
| 35 | public function render(ResultRow $values) { | |
| 36 | 1 | $raw = $this->getValue($values); |
| 37 | 1 | $value = is_scalar($raw) ? trim((string) $raw) : ''; |
| 38 | 1 | $label = $value; |
| 39 | 1 | if ($value === '' && $this->options['empty_label'] !== '') { |
| 40 | 1 | $label = $this->options['empty_label']; |
| 41 | 1 | $value = 'not-defined'; |
| 42 | } | |
| 43 | ||
| 44 | 1 | $route = (string) ($this->options['route'] ?? ''); |
| 45 | 1 | if ($route === '' || $value === '') { |
| 46 | 0 | return $label; |
| 47 | } | |
| 48 | ||
| 49 | 1 | $parameter = (string) ($this->options['parameter'] ?? 'referer_name'); |
| 50 | 1 | $url = Url::fromRoute($route, [$parameter => $value]); |
| 51 | 1 | return Link::fromTextAndUrl($label, $url)->toString(); |
| 52 | } | |
| 53 | ||
| 54 | } |