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\VisitorsLanguageInterface; | |
| 10 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 11 | ||
| 12 | /** | |
| 13 | * Field handler to display the language of the visitors. | |
| 14 | * | |
| 15 | * @ingroup views_field_handlers | |
| 16 | */ | |
| 17 | #[ViewsField("visitors_language")] | |
| 18 | final 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 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 41 | ||
| 42 | 11 | $this->language = $language; |
| 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.language'), |
| 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['code'] = ['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['code'] = [ |
| 76 | 1 | '#title' => $this->t('Show language code'), |
| 77 | 1 | '#type' => 'checkbox', |
| 78 | 1 | '#default_value' => $this->options['code'], |
| 79 | 1 | ]; |
| 80 | ||
| 81 | 1 | parent::buildOptionsForm($form, $form_state); |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * {@inheritdoc} | |
| 86 | */ | |
| 87 | public function render(ResultRow $values) { | |
| 88 | ||
| 89 | 2 | $language_code = $this->getValue($values); |
| 90 | ||
| 91 | 2 | $language = $this->language->getLanguageLabel($language_code); |
| 92 | ||
| 93 | 2 | if ($this->options['code']) { |
| 94 | 1 | $language = $language . ' (' . $language_code . ')'; |
| 95 | } | |
| 96 | ||
| 97 | 2 | return $language; |
| 98 | } | |
| 99 | ||
| 100 | } |