LanguageField.php

Same filename in other branches
  1. 9 core/modules/views/src/Plugin/views/field/LanguageField.php
  2. 8.9.x core/modules/views/src/Plugin/views/field/LanguageField.php
  3. 10 core/modules/views/src/Plugin/views/field/LanguageField.php

Namespace

Drupal\views\Plugin\views\field

File

core/modules/views/src/Plugin/views/field/LanguageField.php

View source
<?php

namespace Drupal\views\Plugin\views\field;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Attribute\ViewsField;
use Drupal\views\ResultRow;

/**
 * Defines a field handler to translate a language into its readable form.
 *
 * @ingroup views_field_handlers
 */
class LanguageField extends FieldPluginBase {
    
    /**
     * {@inheritdoc}
     */
    protected function defineOptions() {
        $options = parent::defineOptions();
        $options['native_language'] = [
            'default' => FALSE,
        ];
        return $options;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildOptionsForm(&$form, FormStateInterface $form_state) {
        parent::buildOptionsForm($form, $form_state);
        $form['native_language'] = [
            '#title' => $this->t('Display in native language'),
            '#type' => 'checkbox',
            '#default_value' => $this->options['native_language'],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function render(ResultRow $values) {
        $value = $this->getValue($values);
        $languages = $this->options['native_language'] ? \Drupal::languageManager()->getNativeLanguages() : \Drupal::languageManager()->getLanguages();
        return isset($languages[$value]) ? $languages[$value]->getName() : '';
    }

}

Classes

Title Deprecated Summary
LanguageField Defines a field handler to translate a language into its readable form.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.