YamlFormEntityReferenceSelectWidget.php in YAML Form 8
File
src/Plugin/Field/FieldWidget/YamlFormEntityReferenceSelectWidget.php
View source
<?php
namespace Drupal\yamlform\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\yamlform\YamlFormInterface;
class YamlFormEntityReferenceSelectWidget extends YamlFormEntityReferenceAutocompleteWidget {
public static function defaultSettings() {
return [];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
return [];
}
public function settingsSummary() {
return [];
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['target_id']['#type'] = 'yamlform_entity_select';
if (empty($element['#required'])) {
$element['target_id']['#empty_option'] = $this
->t('- Select -');
$element['target_id']['#empty_value'] = '';
}
if (!empty($element['target_id']['#default_value']) && $element['target_id']['#default_value'] instanceof YamlFormInterface) {
$element['target_id']['#default_value'] = $element['target_id']['#default_value']
->id();
}
unset($element['target_id']['#size']);
unset($element['target_id']['#maxlength']);
unset($element['target_id']['#placeholder']);
$element['#element_validate'] = [
[
get_class($this),
'validateYamlFormEntityReferenceSelectWidget',
],
];
return $element;
}
public static function validateYamlFormEntityReferenceSelectWidget(array &$element, FormStateInterface $form_state, array &$complete_form) {
$value = !empty($element['target_id']['#value']) ? $element['target_id']['#value'] : NULL;
$form_state
->setValueForElement($element['target_id'], $value);
}
}