--- image/contrib/image_attach/image_attach.module 2010-10-03 09:35:51.000000000 +0100 +++ image_diff/contrib/image_attach/image_attach.module 2011-01-28 14:50:07.171875000 +0000 @@ -224,13 +224,34 @@ function image_attach_form_alter(&$form, } if ($has_existing_images) { - foreach ($node->iids as $iid) { - $image = node_load($iid); - $form['image_attach']['image_thumbnail'][$iid] = array( - '#type' => 'item', - '#title' => t('Thumbnail'), - '#value' => image_display($image, 'thumbnail'), - ); + $form['image_attach']['image_thumbnail']['#theme'] = 'image_attach_image_attach'; + $form['image_attach']['image_thumbnail']['#tree'] = TRUE; + $imageweight = 0; + foreach($node->iids as $iid) { + $image = node_load($iid); + $form['image_attach']['image_thumbnail'][$iid] = array( + 'image' => array( + '#type' => 'markup', + '#value' => image_display($image, 'thumbnail'), + ), + 'delete' => array ( + '#type' => 'checkbox', + ), + 'title' => array ( + '#type' => 'markup', + '#value' => $image->title, + ), + 'weight' => array( + '#type' => 'weight', + '#title' => t('position'), + '#delta' => count($node->iids), + '#default_value' => isset($form_state['values']['image_thumbnail'][$iid]) ? $form_state['values']['image_thumbnail'][$iid]['weight'] : $imageweight++, + ), + 'id' => array( + '#type' => 'hidden', + '#value' => $iid, + ), + ); } } @@ -292,6 +319,32 @@ function image_attach_form_alter(&$form, } /** + * Get image weights and return an iid array ordered by weight + * + * This private function is called by submit handlers to discover how the user has ordered their images. + * It returns an array with identical keys and values (just like the ordinary iid array) + * + * @param $rawweightdata + * An array of iids and weights from the drag-to-reorder table, derived from either the $form_state object or the $node object + * @param $selectboxarray + * The array of existing images that the user wants to include. This may be different to the iids passed in $rawweightdata + * @return + * A weight-sorted (from lightest to heaviest) array of iids, with identical keys and (integer) values + */ +function _image_attach_set_image_weight($rawweightdata, $selectboxarray) { + $weightarray = array(); + foreach($selectboxarray as $iid){ + // check the delete box has not been ticked + if($rawweightdata[$iid]['delete'] !== 1) { + // if the image exists in the drag-to-reorder array, get its weight, otherwise, assign it the heaviest weight possible + $weightarray[$iid] = isset($rawweightdata[$iid]['weight']) ? $rawweightdata[$iid]['weight'] : count($selectboxarray); + } + } + asort($weightarray); + return array_combine(array_keys($weightarray),array_keys($weightarray)); +} + +/** * Save attached image nids and rebuild form. * * This submit function adds the new images and returns to the @@ -301,6 +354,10 @@ function image_attach_image_add_submit(& // Rebuild the attached image data. if (isset($form_state['values']['iids'])) { db_query("DELETE FROM {image_attach} WHERE nid = %d", $form['nid']['#value']); + // Sort the $form_state['values']['iids'] array by weight + if (count($form_state['values']['image_thumbnail'])) { + $form_state['values']['iids'] = _image_attach_set_image_weight($form_state['values']['image_thumbnail'], $form_state['values']['iids']); + } if (count($form_state['values']['iids'])) { $weight = 0; foreach ($form_state['values']['iids'] as $iid) { @@ -417,7 +475,8 @@ 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. + // Get weights from drag-to-reorder table + $node->iids = _image_attach_set_image_weight($node->image_thumbnail, $node->iids); $weight = 0; foreach ($node->iids as $iid) { db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $node->nid, $iid, $weight++); @@ -573,10 +632,40 @@ function image_attach_theme() { 'image_nodes' => array(), ), ), + 'image_attach_image_attach' => array( + 'arguments' => array( + 'form' => NULL + ), + ), ); } /** + * Theme image thumbnails in editing ui to a drag-to-reorder table + * + * @param $form + * The $form object + * + */ +function theme_image_attach_image_attach($form) { + drupal_add_tabledrag('draggable-table', 'order', 'sibling', 'weight-group'); + $header = array('Attached Images', 'Delete', 'Title', 'Weight'); + foreach (element_children($form) as $key) { + $element = &$form[$key]; + $element['weight']['#attributes']['class'] = 'weight-group'; + $row = array(); + $row[] = drupal_render($element['image']); + $row[] = drupal_render($element['delete']); + $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; +} + +/** * Theme attached images shown in nodes. * * @param $nid