Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| VisitorsContinent | |
100.00% |
21 / 21 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
6 / 6 |
|
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% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 41 | |
| 42 | $this->location = $location; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritdoc} |
| 47 | */ |
| 48 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 49 | return new self( |
| 50 | $configuration, |
| 51 | $plugin_id, |
| 52 | $plugin_definition, |
| 53 | $container->get('visitors.location'), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Define the available options. |
| 59 | * |
| 60 | * @return array |
| 61 | * The available options. |
| 62 | */ |
| 63 | protected function defineOptions() { |
| 64 | $options = parent::defineOptions(); |
| 65 | $options['abbreviation'] = ['default' => FALSE]; |
| 66 | |
| 67 | return $options; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Provide the options form. |
| 72 | */ |
| 73 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { |
| 74 | |
| 75 | $form['abbreviation'] = [ |
| 76 | '#title' => $this->t('Use abbreviation'), |
| 77 | '#type' => 'checkbox', |
| 78 | '#default_value' => $this->options['abbreviation'], |
| 79 | ]; |
| 80 | |
| 81 | parent::buildOptionsForm($form, $form_state); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * {@inheritdoc} |
| 86 | */ |
| 87 | public function render(ResultRow $values) { |
| 88 | $value = $this->getValue($values); |
| 89 | if (!$this->options['abbreviation']) { |
| 90 | $value = $this->location->getContinentLabel($value); |
| 91 | } |
| 92 | |
| 93 | return $value; |
| 94 | } |
| 95 | |
| 96 | } |