public function DynamicEntityReferenceWidget::elementValidate in Dynamic Entity Reference 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldWidget/DynamicEntityReferenceWidget.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldWidget\DynamicEntityReferenceWidget::elementValidate()
File
- src/
Plugin/ Field/ FieldWidget/ DynamicEntityReferenceWidget.php, line 164
Class
- DynamicEntityReferenceWidget
- Plugin implementation of the 'dynamic_entity_reference autocomplete' widget.
Namespace
Drupal\dynamic_entity_reference\Plugin\Field\FieldWidgetCode
public function elementValidate(&$element, FormStateInterface $form_state, &$form) {
if (!empty($element['#value'])) {
// If this is the default value of the field.
if ($form_state
->hasValue('default_value_input')) {
$values = $form_state
->getValue([
'default_value_input',
$element['#field_name'],
$element['#delta'],
]);
}
else {
$parents = $element['#parents'];
// Remove the 'target_id' key.
array_pop($parents);
$values = $form_state
->getValue($parents);
}
$settings = $this
->getFieldSettings();
$element['#target_type'] = $values['target_type'];
$element['#selection_handler'] = $settings[$values['target_type']]['handler'];
$element['#selection_settings'] = $settings[$values['target_type']]['handler_settings'];
if ($this
->getSelectionHandlerSetting('auto_create', $values['target_type'])) {
$form_object = $form_state
->getFormObject();
$entity = $form_object instanceof EntityFormInterface ? $form_object
->getEntity() : '';
$element['#autocreate'] = [
'bundle' => $this
->getAutocreateBundle($values['target_type']),
'uid' => $entity instanceof EntityOwnerInterface ? $entity
->getOwnerId() : \Drupal::currentUser()
->id(),
];
}
else {
$element['#autocreate'] = NULL;
}
}
}