You are here

class LanguageField in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/field/LanguageField.php \Drupal\views\Plugin\views\field\LanguageField
  2. 9 core/modules/views/src/Plugin/views/field/LanguageField.php \Drupal\views\Plugin\views\field\LanguageField

Defines a field handler to translate a language into its readable form.

Plugin annotation

@ViewsField("language");

Hierarchy

  • class \Drupal\views\Plugin\views\field\LanguageField extends \Drupal\views\Plugin\views\field\FieldPluginBase

Expanded class hierarchy of LanguageField

File

core/modules/views/src/Plugin/views/field/LanguageField.php, line 15

Namespace

Drupal\views\Plugin\views\field
View source
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() : '';
  }

}

Members