function field_defaults_update_default in Field Defaults 8
Batch Processor.
2 string references to 'field_defaults_update_default'
- FieldDefaultsBulkCommands::fieldDefaultsBulkUpdate in src/
Commands/ FieldDefaultsBulkCommands.php - Bulk update defaults.
- _field_defaults_ui_submit in ./
field_defaults.module - Submit handler for field ui form.
File
- ./
field_defaults.module, line 156 - Allows updating existing content with default values.
Code
function field_defaults_update_default($type, $id, $field_name, $field_value, $languages, $no_overwrite, &$context) {
$has_changed = FALSE;
/** @var \Drupal\Core\Entity\ContentEntityBase $entity */
$entity = \Drupal::entityTypeManager()
->getStorage($type)
->load($id);
// First set the default on the current language.
if (!$no_overwrite || $entity
->get($field_name)
->isEmpty()) {
$entity->{$field_name} = $field_value;
$has_changed = TRUE;
}
// Now set any additional languages.
foreach ($languages as $language_id => $language_value) {
// Value is if was checked in form.
if ($language_value) {
if ($entity
->hasTranslation($language_id)) {
$entity = $entity
->getTranslation($language_id);
if (!$no_overwrite || $entity
->get($field_name)
->isEmpty()) {
$entity->{$field_name} = $field_value;
$has_changed = TRUE;
}
}
}
}
// Mark this entity as being saved through the field_defaults module.
$entity->batch_field_defaults = TRUE;
// Save the entity.
if ($has_changed) {
$context['results'][] = $entity
->save();
$context['message'] = t("Setting Default Values on entity id: @id", [
"@id" => $id,
]);
}
}