Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Plugin\views\field; | |
| 4 | ||
| 5 | use Drupal\Core\Form\FormStateInterface; | |
| 6 | use Drupal\views\Attribute\ViewsField; | |
| 7 | use Drupal\views\Plugin\views\field\FieldPluginBase; | |
| 8 | use Drupal\views\ResultRow; | |
| 9 | use Drupal\visitors\VisitorsLocationInterface; | |
| 10 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 11 | ||
| 12 | /** | |
| 13 | * Field handler to display the continent of the visitors. | |
| 14 | * | |
| 15 | * @ingroup views_field_handlers | |
| 16 | */ | |
| 17 | #[ViewsField("visitors_continent")] | |
| 18 | final class VisitorsContinent extends FieldPluginBase { | |
| 19 | ||
| 20 | /** | |
| 21 | * The location service. | |
| 22 | * | |
| 23 | * @var \Drupal\visitors\VisitorsLocationInterface | |
| 24 | */ | |
| 25 | protected $location; | |
| 26 | ||
| 27 | /** | |
| 28 | * Continent field. | |
| 29 | * | |
| 30 | * @param array $configuration | |
| 31 | * The plugin configuration. | |
| 32 | * @param string $plugin_id | |
| 33 | * The plugin ID. | |
| 34 | * @param mixed $plugin_definition | |
| 35 | * The plugin definition. | |
| 36 | * @param \Drupal\visitors\VisitorsLocationInterface $location | |
| 37 | * The location service. | |
| 38 | */ | |
| 39 | public function __construct(array $configuration, $plugin_id, $plugin_definition, VisitorsLocationInterface $location) { | |
| 40 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 41 | ||
| 42 | 11 | $this->location = $location; |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * {@inheritdoc} | |
| 47 | */ | |
| 48 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
| 49 | 11 | return new self( |
| 50 | 11 | $configuration, |
| 51 | 11 | $plugin_id, |
| 52 | 11 | $plugin_definition, |
| 53 | 11 | $container->get('visitors.location'), |
| 54 | 11 | ); |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Define the available options. | |
| 59 | * | |
| 60 | * @return array | |
| 61 | * The available options. | |
| 62 | */ | |
| 63 | protected function defineOptions() { | |
| 64 | 11 | $options = parent::defineOptions(); |
| 65 | 11 | $options['abbreviation'] = ['default' => FALSE]; |
| 66 | ||
| 67 | 11 | return $options; |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * Provide the options form. | |
| 72 | */ | |
| 73 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 74 | ||
| 75 | 1 | $form['abbreviation'] = [ |
| 76 | 1 | '#title' => $this->t('Use abbreviation'), |
| 77 | 1 | '#type' => 'checkbox', |
| 78 | 1 | '#default_value' => $this->options['abbreviation'], |
| 79 | 1 | ]; |
| 80 | ||
| 81 | 1 | parent::buildOptionsForm($form, $form_state); |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * {@inheritdoc} | |
| 86 | */ | |
| 87 | public function render(ResultRow $values) { | |
| 88 | 1 | $value = $this->getValue($values); |
| 89 | 1 | if (!$this->options['abbreviation']) { |
| 90 | 1 | $value = $this->location->getContinentLabel($value); |
| 91 | } | |
| 92 | ||
| 93 | 1 | return $value; |
| 94 | } | |
| 95 | ||
| 96 | } |