diff --git a/computed_field.module b/computed_field.module index c7bff05..8f45216 100644 --- a/computed_field.module +++ b/computed_field.module @@ -11,6 +11,7 @@ function computed_field_field_info() { 'settings' => array( 'code' => '$entity_field[0][\'value\'] = "";', 'display_format' => '$display_output = $entity_field_item[\'value\'];', + 'weight' => 0, 'store' => 1, 'database' => array( 'data_type' => 'varchar', @@ -102,6 +103,12 @@ function computed_field_field_settings_form($field, $instance, $has_data) { '#markup' => t('This field is DISPLAYED using @display_func().', array('@display_func' => $display_func)), ); } + $form['weight'] = array( + '#type' => 'weight', + '#title' => t('Weight'), + '#description' => t('Defines the order in which to calculate computed fields'), + '#default_value' => !empty($settings['weight']) ? $settings['weight'] : 0, + ); $form['store'] = array( '#type' => 'checkbox', '#title' => t('Store value in the database'), @@ -396,3 +403,34 @@ function _computed_field_compute_value($entity_type, $entity, $field, $instance, } } +/** + * Order fields according to their weight + */ +function _computed_field_order($a, $b) { + $field_a = field_info_field_by_id($a['field_id']); + $field_b = field_info_field_by_id($b['field_id']); + if ($field_a['type'] == 'computed' && $field_a['type'] == 'computed') { + return ($field_a['settings']['weight'] < $field_b['settings']['weight']) ? -1 : 1; + } + + return 0; +} + +/** + * Implements hook_field_attach_load(). + */ +function computed_field_field_attach_load($entity_type, $entities, $age, $options) { + $info = _field_info_collate_fields(); + if (empty($info['sorted'])) { + foreach($info['instances'] as $entity_type => &$bundles) { + foreach ($bundles as $bundle => &$instances) { + uasort($instances, '_computed_field_order'); + } + } + + _field_info_collate_fields(TRUE); + + $info['sorted'] = TRUE; + cache_set('field_info_fields', $info, 'cache_field'); + } +}