LanguageSelectWidget.php in Custom Language field 8
File
src/Plugin/Field/FieldWidget/LanguageSelectWidget.php
View source
<?php
namespace Drupal\languagefield\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\languagefield\Plugin\Field\FieldType\LanguageItem;
class LanguageSelectWidget extends WidgetBase {
public static function defaultSettings() {
$settings = [
'format' => [
'name' => 'name',
],
] + parent::defaultSettings();
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$settings = $this
->getSettings();
$element = parent::settingsForm($form, $form_state);
$element['format'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Display in widget'),
'#description' => $this
->t('Select the elements you want to show. The elements will be concatenated when showing the field.'),
'#default_value' => $settings['format'],
'#options' => LanguageItem::settingsOptions('widget'),
'#required' => TRUE,
];
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$settings = $this
->getSettings()['format'];
$options = LanguageItem::settingsOptions('widget');
if (empty($settings)) {
$summary[] = $this
->t('** Not set **');
return $summary;
}
foreach ($settings as $value) {
switch ($value) {
case '0':
break;
default:
$summary[] = isset($options[$value]) ? $options[$value] : '...';
break;
}
}
return $summary;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$values = parent::massageFormValues($values, $form, $form_state);
$build_info = $form_state
->getBuildInfo();
if (isset($build_info['form_id']) && $build_info['form_id'] !== 'field_config_edit_form') {
foreach ($values as &$value) {
$value['value'] = LanguageItem::getLanguageConfigurationValues($value['value']);
}
}
return $values;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$item = $items[$delta];
$value = isset($item->value) ? $item->value : NULL;
$languages = $item
->getSettableOptions();
$element['value'] = [
'#title' => $element['#title_display'] == 'invisible' ? NULL : $element['#title'],
'#description' => $element['#description'],
'#type' => 'select',
'#required' => $element['#required'],
'#options' => $languages,
'#empty_value' => '',
'#default_value' => isset($languages[$value]) ? $value : '',
];
return $element;
}
}