function _field_defaults_ui_submit in Field Defaults 8
Same name and namespace in other branches
- 7.2 field_defaults.module \_field_defaults_ui_submit()
- 7 field_defaults.module \_field_defaults_ui_submit()
Submit handler for field ui form.
1 string reference to '_field_defaults_ui_submit'
File
- ./
field_defaults.module, line 86 - Allows updating existing content with default values.
Code
function _field_defaults_ui_submit(&$form, FormStateInterface $form_state) {
$values = $form_state
->getValue([
'default_value_input',
'field_defaults',
], FALSE);
if (!empty($values['update_defaults'])) {
/** @var \Drupal\field\FieldConfigInterface $entity */
$entity = $form_state
->getFormObject()
->getEntity();
$field_name = $entity
->getName();
// Only go ahead if default value field actually has value.
if ($field_value = $form_state
->getValue([
'default_value_input',
$field_name,
], 0)) {
if (isset($field_value['target_id'])) {
// Fix odd term structure.
if (is_array($field_value['target_id'])) {
$field_value = $field_value['target_id'];
}
$handler = $entity
->getSetting('handler');
if (!empty($handler)) {
$definition = explode(':', $handler);
$provider = $definition[1] ?? $definition[0];
}
// Fix media structure.
if ($provider === 'media') {
$media_values = explode(':', $field_value['target_id']);
$field_value = $media_values[1] ?? $media_values[0];
}
}
$entity_type = $entity
->getTargetEntityTypeId();
$bundle_key = \Drupal::entityTypeManager()
->getDefinition($entity_type)
->getKey('bundle');
// Get all entities of type/bundle to process.
$query = \Drupal::entityQuery($entity_type);
// Some entities don't have bundle (i.e. user)
if (!empty($bundle_key)) {
$bundle = $entity
->getTargetBundle();
$query
->condition($bundle_key, $bundle);
}
$ids = $query
->execute();
// Load languages and overwrite settings.
$languages = !empty($values['update_defaults_lang']) ? $values['update_defaults_lang'] : [];
$no_overwrite = !empty($values['no_overwrite']) ? $values['no_overwrite'] : FALSE;
$operations = [];
foreach ($ids as $id) {
$operations[] = [
'field_defaults_update_default',
[
$entity_type,
$id,
$field_name,
$field_value,
$languages,
$no_overwrite,
],
];
}
$batch = [
'title' => t('Processing default values'),
'operations' => $operations,
'finished' => 'field_defaults_batch_finished',
];
batch_set($batch);
}
}
}