function disable_field_form_fill_value in Disable Field 8
This functions fills default value for fields which are disabled, to avoid problem with saving invalid data.
1 string reference to 'disable_field_form_fill_value'
- disable_field_add_validator in ./
disable_field.module - Adds data validator to fields, which are disabled.
File
- ./
disable_field.module, line 238
Code
function disable_field_form_fill_value(&$element, FormStateInterface $form_state, $form) {
list($form_entity_type_id, $bundle, $operation) = disable_field_get_entity_data($form_state);
// We should set default value only when we add the new content or if we edit
// an existing entity, because some field could be added when we will already
// have entity entries.
if (in_array($operation, [
'add',
'edit',
])) {
// We should fill value only if we have widget for field.
if (!empty($element['widget'])) {
$field_name = $element['widget']['#field_name'];
// If value for the field is not set.
$field_value = $form_state
->getValue($field_name);
if (isset($field_value['value']) && $field_value['value'] === FALSE) {
// We need to set default value only when user adds an entity.
if ($operation === 'add') {
if ($form_entity_type_id) {
$entity_type = \Drupal::service('entity_type.manager')
->getDefinition($form_entity_type_id);
// Check that current entity is fieldable before process it's fields.
if ($entity_type
->isSubclassOf(\Drupal\Core\Entity\FieldableEntityInterface::class)) {
$fields = \Drupal::service('entity_field.manager')
->getFieldDefinitions($form_entity_type_id, $bundle);
if ($fields[$field_name]) {
// If the field has a default value.
if ($dv = $fields[$field_name]
->get('default_value')) {
// If the default value is not FALSE.
if ($dv[0]['value'] !== FALSE) {
// Set value by default.
$form_state
->setValue($field_name, $dv);
}
}
}
}
}
}
}
}
}
}