Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.

Commit d1ccd19

Browse files
committed
Fix the field filler in-er to just do the field update.
1 parent 6064768 commit d1ccd19

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

cm_tools.module

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,3 +608,4 @@ function cm_tools_add_async_js($script = '', $id = '') {
608608
array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
609609
);
610610
}
611+

includes/profile.inc

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,48 +263,51 @@ function cm_tools_update_node(&$sandbox, $node_type = NULL, $nodes_per_pass = 10
263263
* The results of the query, and the state of the iterations are stored in the
264264
* sandbox.
265265
*
266-
* This function requires the Entity API module to be enabled.
267-
*
268266
* @param $sandbox
269267
* The sandbox passed to the drupal hook hook that this function is called from.
270268
* @param string $entity_type
271269
* The entity type to iterate over.
272-
* @param string $bundle
273-
* The bundle to iterate over.
270+
* @param NULL|string $bundle
271+
* The bundle to iterate over or NULL to iterate over all bundles for the
272+
* given entity type.
274273
* @param $fields
275-
* An associative array of fields to set the default value for. Keys are the field names, and values are either scalar (and then ignored) or an array of language codes to check and set the default values for.
274+
* An associative array of fields to set the default value for. Keys are the
275+
* field names, and values are either scalar (and then ignored) or an array
276+
* of language codes to check and set the default values for.
276277
* @param int $entities_per_pass
277278
* The number of entities to load and process in each batch iteration.
278-
*
279-
* @throws \DrupalUpdateException
280279
*/
281-
function cm_tools_update_missing_default_field_values_insert(&$sandbox, $entity_type, $bundle, $fields, $entites_per_pass = 10) {
282-
283-
if (!module_exists('entity')) {
284-
throw new DrupalUpdateException('Entity API module is required by ' . __FUNCTION__);
285-
}
280+
function cm_tools_update_missing_default_field_values_insert(&$sandbox, $entity_type, $bundle, $fields, $entities_per_pass = 10) {
286281

287282
cm_tools_update_entity_walk($sandbox, $entity_type, function($entity) use ($entity_type, $fields) {
283+
$needs_save = FALSE;
284+
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
288285
foreach ($fields as $field_name => $languages) {
289286
if (!is_array($languages)) {
290287
$languages = array(NULL);
291288
}
292-
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
293289
$field = field_info_field($field_name);
294290
if ($instance = field_info_instance($entity_type, $field_name, $bundle)) {
295-
$needs_save = FALSE;
291+
296292
foreach ($languages as $language) {
297293
$langcode = field_language($entity_type, $entity, $field_name, $language);
298294
if (field_get_items($entity_type, $entity, $field_name, $langcode) === FALSE) {
299295
$entity->{$field_name}[$langcode] = field_get_default_value($entity_type, $entity, $field, $instance, $langcode);
300296
$needs_save = TRUE;
301297
}
302298
}
303-
if ($needs_save) {
304-
// @TODO: Determine if the field SQL storage module is in for this field, and do a direct insert.
305-
entity_save($entity_type, $entity);
306-
}
307299
}
308300
}
309-
}, $bundle, $entites_per_pass);
301+
if ($needs_save) {
302+
// We don't try and save the full entity, instead we
303+
// Remove all other fields from the entity, then they'll skip being saved.
304+
$instances = field_info_instances($entity_type, $bundle);
305+
foreach (array_diff_key($instances, $fields) as $field_name => $instance) {
306+
unset($entity->$field_name);
307+
}
308+
field_attach_presave($entity_type, $entity);
309+
field_attach_update($entity_type, $entity);
310+
entity_get_controller($entity_type)->resetCache(array($id));
311+
}
312+
}, $bundle, $entities_per_pass);
310313
}

0 commit comments

Comments
 (0)