--- image/contrib/image_attach/image_attach.module 2010-10-03 09:35:51.000000000 +0100 +++ image/contrib/image_attach/image_attach.module 2011-01-24 21:30:17.531250000 +0000 @@ -224,13 +224,34 @@ function image_attach_form_alter(&$form, } if ($has_existing_images) { - foreach ($node->iids as $iid) { + $form['image_attach']['image_thumbnail']['#theme'] = 'image_attach_image_attach'; + $form['image_attach']['image_thumbnail']['#tree'] = TRUE; + + // A database query is needed in order to display the attached images according to their true weights (otherwise they are simply displayed in the order they are 'node-loaded' from the database + $result = db_query("SELECT iid, weight FROM {image_attach} WHERE nid = %d ORDER BY weight", $node->nid); + while ($data = db_fetch_object($result)) { + // Store all the attached images' weights keyed by attaching iid. + $attached_images_weights[$data->iid] = $data->weight; + } + + foreach ($attached_images_weights as $iid => $imageweight) { $image = node_load($iid); $form['image_attach']['image_thumbnail'][$iid] = array( - '#type' => 'item', - '#title' => t('Thumbnail'), - '#value' => image_display($image, 'thumbnail'), - ); + 'title' => array( + '#type' => 'markup', + '#value' => image_display($image, 'thumbnail'), + ), + 'weight' => array( + '#type' => 'weight', + '#title' => t('position'), + '#delta' => count($node->iids), + '#default_value' => isset($imageweight) ? $imageweight : 0, + ), + 'id' => array( + '#type' => 'hidden', + '#value' => $iid, + ), + ); } } @@ -302,9 +329,9 @@ function image_attach_image_add_submit(& if (isset($form_state['values']['iids'])) { db_query("DELETE FROM {image_attach} WHERE nid = %d", $form['nid']['#value']); if (count($form_state['values']['iids'])) { - $weight = 0; foreach ($form_state['values']['iids'] as $iid) { - db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $form['nid']['#value'], $iid, $weight++); + $weight = $form_state['values']['image_thumbnail'][$iid]['weight']; + db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $form['nid']['#value'], $iid, $weight); } } } @@ -417,10 +445,10 @@ function image_attach_nodeapi(&$node, $o case 'update': db_query("DELETE FROM {image_attach} WHERE nid = %d", $node->nid); if (!empty($node->iids)) { - // Populate weight column with placeholder values. - $weight = 0; + // Get weights from node->image_thumbnail property foreach ($node->iids as $iid) { - db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $node->nid, $iid, $weight++); + $weight = (int)$node->image_thumbnail[$iid]['weight']; + db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $node->nid, $iid, $weight); } } break; @@ -573,7 +601,28 @@ function image_attach_theme() { 'image_nodes' => array(), ), ), - ); + 'image_attach_image_attach' => array( + 'arguments' => array( + 'form' => NULL + ), + ), + ); +} + +function theme_image_attach_image_attach($form) { +drupal_add_tabledrag('draggable-table', 'order', 'sibling', 'weight-group'); +$header = array('Attached Images', 'Weight'); +foreach (element_children($form) as $key) { +$element = &$form[$key]; +$element['weight']['#attributes']['class'] = 'weight-group'; +$row = array(); +$row[] = drupal_render($element['title']); +$row[] = drupal_render($element['weight']) . drupal_render($element['id']); +$rows[] = array('data' => $row, 'class' => 'draggable'); +} +$output = theme('table', $header, $rows, array('id' => 'draggable-table')); +$output .= drupal_render($form); +return $output; } /**