LanguageSelectWidget.php

Same filename in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php

Namespace

Drupal\Core\Field\Plugin\Field\FieldWidget

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/LanguageSelectWidget.php

View source
<?php

namespace Drupal\Core\Field\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;

/**
 * Plugin implementation of the 'Language' widget.
 *
 * @FieldWidget(
 *   id = "language_select",
 *   label = @Translation("Language select"),
 *   field_types = {
 *     "language"
 *   }
 * )
 */
class LanguageSelectWidget extends WidgetBase {
    
    /**
     * {@inheritdoc}
     */
    public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
        $element['value'] = $element + [
            '#type' => 'language_select',
            '#default_value' => $items[$delta]->value,
            '#languages' => $this->getSetting('include_locked') ? LanguageInterface::STATE_ALL : LanguageInterface::STATE_CONFIGURABLE,
        ];
        return $element;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function defaultSettings() {
        $settings = parent::defaultSettings();
        $settings['include_locked'] = TRUE;
        return $settings;
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsForm(array $form, FormStateInterface $form_state) {
        $element = parent::settingsForm($form, $form_state);
        $element['include_locked'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Include locked languages such as <em>Not specified</em> and <em>Not applicable</em>'),
            '#default_value' => $this->getSetting('include_locked'),
        ];
        return $element;
    }

}

Classes

Title Deprecated Summary
LanguageSelectWidget Plugin implementation of the 'Language' widget.

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