Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
VisitorsLanguage
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 defineOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 buildOptionsForm
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Drupal\visitors\Plugin\views\field;
4
5use Drupal\Core\Form\FormStateInterface;
6use Drupal\views\Plugin\views\field\FieldPluginBase;
7use Drupal\views\ResultRow;
8use Drupal\visitors\VisitorsLanguageInterface;
9use Symfony\Component\DependencyInjection\ContainerInterface;
10
11/**
12 * Field handler to display the device brand of the visitors.
13 *
14 * @ingroup views_field_handlers
15 *
16 * @ViewsField("visitors_language")
17 */
18final class VisitorsLanguage extends FieldPluginBase {
19
20  /**
21   * The language service.
22   *
23   * @var \Drupal\visitors\VisitorsLanguageInterface
24   */
25  protected $language;
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\VisitorsLanguageInterface $language
37   *   The location service.
38   */
39  public function __construct(array $configuration, $plugin_id, $plugin_definition, VisitorsLanguageInterface $language) {
40    parent::__construct($configuration, $plugin_id, $plugin_definition);
41
42    $this->language = $language;
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.language'),
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['code'] = ['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['code'] = [
76      '#title' => $this->t('Show language code'),
77      '#type' => 'checkbox',
78      '#default_value' => $this->options['code'],
79    ];
80
81    parent::buildOptionsForm($form, $form_state);
82  }
83
84  /**
85   * {@inheritdoc}
86   */
87  public function render(ResultRow $values) {
88
89    $language_code = $this->getValue($values);
90
91    $language = $this->language->getLanguageLabel($language_code);
92
93    if ($this->options['code']) {
94      $language = $language . ' (' . $language_code . ')';
95    }
96
97    return $language;
98  }
99
100}