diff --git a/serial.inc b/serial.inc index cb3dec2..7c09c77 100644 --- a/serial.inc +++ b/serial.inc @@ -170,32 +170,50 @@ function _serial_generate_value($bundle, $field_name, $delete = TRUE) { } /** - * Initializes the value of a new serial field in existing nodes. + * Initializes the value of a new serial field in existing entities. * * @param $bundle * a containing bundle (e.g. content type) * @param $field_name * the field name + * @param $entity_type + * the entity type * @return * the number of existing nodes that have been initialized. */ -function _serial_init_old_nodes($bundle, $field_name) { - // Retrieve all the node ids of that type: - $query = "SELECT nid FROM {node} WHERE type = :type ORDER BY nid"; - // TODO: Currently works only for nodes - should support comments and users. - $result = db_query($query, array('type' => $bundle)); - - // Allocate a serial number for every old node: +function _serial_init_old_entities($bundle, $field_name, $entity_type) { + + $query = new EntityFieldQuery(); + + // Retrieve all the entity ids according to entity_type and bundle + + if($bundle!=$entity_type) + $entities = $query->entityCondition('entity_type', $entity_type) + ->entityCondition('bundle', $bundle) + ->execute(); + + // Run different query if entity doesn't have a unique bundle name + + else $entities = $query->entityCondition('entity_type', $entity_type) + ->execute(); + + // Allocate a serial number for every existing entity: + $count = 0; - foreach ($result as $node) { - $nid = $node->nid; - $node = node_load($nid); + if(!empty($entities)): + foreach($entities{$entity_type} as $entity_id => $value) { + ${$entity_type} = entity_load($entity_type,array($entity_id)); + $sid = _serial_generate_value($bundle, $field_name, FALSE); - $node->{$field_name} = array('und' => array(array('value' => $sid))); - node_save($node); + + ${$entity_type}[$entity_id]->{$field_name} = array('und' => array(array('value' => $sid))); + dsm($entity_id); + + entity_save($entity_type,${$entity_type}[$entity_id]); $count++; } - + endif; + // Return the number of existing nodes that have been initialized: return $count; } @@ -214,4 +232,3 @@ function _serial_get_all_fields() { $result = $query->execute(); return $result->fetchAll(); } - diff --git a/serial.module b/serial.module index 688bb10..5d582ee 100644 --- a/serial.module +++ b/serial.module @@ -34,7 +34,10 @@ function serial_field_create_instance($instance) { _serial_create_table($field, $instance); // Set serial values for old objects - $old_count = _serial_init_old_nodes($instance['bundle'], $field['field_name']); + // $old_count = _serial_init_old_nodes($instance['bundle'], $field['field_name']); + + $old_count = _serial_init_old_entities($instance['bundle'], $field['field_name'], $instance['entity_type']); + if ($old_count) { drupal_set_message( t('Serial values have been automatically set for %count existing nodes.', @@ -68,10 +71,14 @@ function serial_form_alter(&$form, $form_state, $form_id) { t('Serial field %field has been created.', array('%field' => $field_name)) ); - + // Go back to Managed Fields: $type = $form['#bundle']; - drupal_goto("admin/structure/types/manage/$type/fields"); + + // LW: Following line may not be needed as return to relevant page occurs anyway + // LW: .. equivalent page is in different place for FC; admin/structure/field-collections/*bundle*/fields/*field_name*/field-settings + + //drupal_goto("admin/structure/types/manage/$type/fields"); } }