diff --git a/computed_field_order/README.txt b/computed_field_order/README.txt new file mode 100644 index 0000000..b62194a --- /dev/null +++ b/computed_field_order/README.txt @@ -0,0 +1,27 @@ +The Computed Field Order module helps to deal with the situation of fields +being computed in the order in which they are found in the "field_config_instance" +database table. Since that table is largely populated in the order in which you +add fields to an entity, one may control the order of computation by adding +fields in the order desired. Occasionally, however, it may be necessary to +have a later-added field computed before an existing field, such as when a field +containing data must be deleted and re-added. The easiest solution to this is to +delete and redefine the subsequent fields, thereby preserving the database integrity. +But if there are a lot of uses of that field and re-saving the content is difficult, +then this module may help you restore the desired computation order. + +If there is any way to take the manual solution above, we reccommend doing so. +You are strongly urged to back up your database before using this module. +We also suggest testing your site after using it, particularly the fields you +are altering. + +* Install and enable the module in the usual manner. +* Back up you database (optional, but reccommended). +* Go to admin/config/content/computed_field_order + (Configuration >> Content authoring >> Computed Field Order). +* Click on the bundle type that needs to be changed. +* Drag the field handles to the order you want. +* Click on the "Change order" button. +* Test to make sure the correct effect has occurred. + +You may disable the module when it is not in use so that it +is not loaded at every page build. diff --git a/computed_field_order/computed_field_order.info b/computed_field_order/computed_field_order.info new file mode 100644 index 0000000..609fd0a --- /dev/null +++ b/computed_field_order/computed_field_order.info @@ -0,0 +1,6 @@ +name = Computed Field Order +description = Allow re-ordering the field computations. +core = 7.x +configure = 'admin/config/content/computed_field_order' +dependencies[] = computed_field +package = Fields \ No newline at end of file diff --git a/computed_field_order/computed_field_order.module b/computed_field_order/computed_field_order.module new file mode 100644 index 0000000..4ab3bae --- /dev/null +++ b/computed_field_order/computed_field_order.module @@ -0,0 +1,341 @@ +'; + $output .= t('The Computed Field Order module helps to deal with the situation of fields being + computed in the order in which they are found in the "field_config_instance" database table.'); + $output .= ' '; + $output .= t('Since that table is largely populated in the order in which you add fields to + an entity, one may control the order of computation by adding fields in the order desired.'); + $output .= ' '; + $output .= t('Occasionally, however, it may be necessary to have a later-added field computed + before an existing field, such as when a field containing data must be deleted and re-added.'); + $output .= ' '; + $output .= t('The easiest solution to this is to delete and redefine the subsequent fields, + thereby preserving the database integrity.'); + $output .= ' '; + $output .= t('But if there are a lot of uses of that field and re-saving the content is difficult, + then this module may help you restore the desired computation order.'); + $output .= '
'; + $output .= ''; + $output .= t('If there is any way to take the manual solution above, we reccommend doing so.'); + $output .= ' '; + $output .= t('You are strongly urged to back up your database before using this module.'); + $output .= ' '; + $output .= t('We also suggest testing your site after using it, particularly the fields you + are altering.'); + $output .= '
'; + break; + } + return $output; +} + +/** + * Implements hook_menu(). + */ +function computed_field_order_menu() { + $items = array(); + + $items['admin/config/content/computed_field_order'] = array( + 'title' => 'Computed Field Order', + 'description' => 'Change the order of Computed Field computations', + 'access arguments' => array('administer site configuration'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('computed_field_order_settings_form', 4, 5), + ); + + return $items; +} + +/** + * Implements hook_theme(). + */ +function computed_field_order_theme() { + return array( + 'computed_field_order_settings_form' => array( + 'render element' => 'form', + ), + ); +} + +/** + * Menu callback: Settings form. + */ +function computed_field_order_settings_form($form, &$form_state, $entity_type = NULL, $bundle = NULL) { + // Let's get a list of all the entity_type/bundles that have computed fields in them. + $query = db_select('field_config', 'fc'); + $query->innerJoin('field_config_instance', 'fci', 'fci.field_id = fc.id'); + $query->condition('fc.type', 'computed'); + $query->condition('fc.deleted', 0); + $query->fields('fci', array('entity_type', 'bundle', 'data')) + ->orderBy('fci.entity_type') + ->orderBy('fci.bundle'); + $result = $query->execute(); + + $items = array(); + foreach ($result as $row) { + // Build a multi-level list. + if (isset($items[$row->entity_type])) { + $classes = array( + 'computed_field_order', // Standard class + drupal_html_class($row->entity_type), // and entity_type + drupal_html_class($row->bundle), // and bundle. + ); + + // It was serendipitous, but using the bundle name as a key prevents duplicates. + // using Distinct in the query resulted in missed data. + $items[$row->entity_type]['children'][$row->bundle] = array( + 'data' => l($info['bundles'][$row->bundle]['label'], + "admin/config/content/computed_field_order/$row->entity_type/" . $row->bundle), + 'class' => $classes, + ); + } + else { + $info = entity_get_info($row->entity_type); + $items[$row->entity_type] = array( + 'data' => check_plain($info['label']), + 'children' => array(), + ); + } + } +// dpm($items, 'Computed Field Usage'); + + // Show the list so they can select which one to order. + $form['intro'] = array( + '#type' => 'fieldset', + '#title' => t('Computed Field Usage'), + '#collapsible' => TRUE, + '#collapsed' => !empty($entity_type), + '#description' => t('These entity_type / bundle types contain computed fields.'), + ); + + $form['intro']['types'] = array( + '#type' => 'markup', + '#markup' => theme('item_list', array('items' => $items)), + ); + + $form['intro']['description'] = array( + '#type' => 'markup', + '#markup' => '