summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Haug2009-03-11 02:58:55 (GMT)
committer Nathan Haug2009-03-11 02:58:55 (GMT)
commit4b58ccdebfbadb15ee53c69fcf94dc22cc5b442d (patch)
tree8908310e5e58d8c68f0e878c9efe655875ac058d
parent5b3aa8222cfdada404443dbf34808ffb4249f48a (diff)
#367267: New mechanism for AHAH uploads, support for CCK Multigroup.
-rw-r--r--filefield.module51
-rw-r--r--filefield_field.inc8
-rw-r--r--filefield_widget.inc86
3 files changed, 67 insertions, 78 deletions
diff --git a/filefield.module b/filefield.module
index 4c0b9ae..d27c21f 100644
--- a/filefield.module
+++ b/filefield.module
@@ -438,47 +438,28 @@ function filefield_js($type_name, $field_name, $delta) {
exit;
}
- // form_get_cache() doesn't yield the original $form_state,
- // but form_builder() does. Needed for retrieving the file array.
- $built_form = $form;
- $built_form_state = $form_state;
-
- $built_form += array('#post' => $_POST);
- $built_form = form_builder($_POST['form_id'], $built_form, $built_form_state);
-
- // Clean ids, so that the same element doesn't get a different element id
- // when rendered once more further down.
- form_clean_id(NULL, TRUE);
-
- // Ask CCK for the replacement form element. Going through CCK gets us
- // the benefit of nice stuff like '#required' merged in correctly.
- module_load_include('inc', 'content', 'includes/content.node_form');
- $field_element = content_field_form($form, $built_form_state, $field, $delta);
- $delta_element = $field_element[$field_name][0]; // there's only one element in there
-
- // Add the new element at the right place in the form.
+ // Build the form. This calls the file field's #value_callback function and
+ // saves the uploaded file. Since this form is already marked as cached
+ // (the #cache property is TRUE), the cache is updated automatically and we
+ // don't need to call form_set_cache().
+ $args = $form['#parameters'];
+ $form_id = array_shift($args);
+ $form['#post'] = $_POST;
+ $form = form_builder($form_id, $form, $form_state);
+
+ // Update the cached form with the new element at the right place in the form.
if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type_name, $field_name))) {
- $form[$group_name][$field_name][$delta] = $delta_element;
+ $form_element = $form[$group_name][$delta][$field_name];
}
else {
- $form[$field_name][$delta] = $delta_element;
+ $form_element = $form[$field_name][$delta];
}
- // Render the form for output.
- $form += array(
- '#post' => $_POST,
- '#programmed' => FALSE,
- );
- drupal_alter('form', $form, array(), 'filefield_js');
- $form_state = array('submitted' => FALSE);
- $form = form_builder('filefield_js', $form, $built_form_state);
- $field_form = empty($group_name) ? $form[$field_name] : $form[$group_name][$field_name];
-
- // We add a div around the new content to tell AHAH to let this fade in.
- $field_form[$delta]['#prefix'] = '<div class="ahah-new-content">';
- $field_form[$delta]['#suffix'] = '</div>';
+ if (isset($form_element['_weight'])) {
+ unset($form_element['_weight']);
+ }
- $output = drupal_render($field_form[$delta]);
+ $output = drupal_render($form_element);
// AHAH is not being nice to us and doesn't know the "other" button (that is,
// either "Upload" or "Delete") yet. Which in turn causes it not to attach
diff --git a/filefield_field.inc b/filefield_field.inc
index 3b3d736..5f7fb2b 100644
--- a/filefield_field.inc
+++ b/filefield_field.inc
@@ -94,7 +94,6 @@ function filefield_field_load($node, $field, &$items, $teaser, $page) {
$items[$delta] = array_merge($item, $file);
}
}
- $items = array_values($items); // compact deltas
return array($field['field_name'] => $items);
}
@@ -113,8 +112,6 @@ function filefield_field_update($node, $field, &$items, $teaser, $page) {
if (empty($items[$delta])) unset($items[$delta]);
$curfids[] = $item['fid'];
}
- $items = array_values($items); // compact deltas
-
// if this is a new node... there are no
// old items to worry about.
@@ -140,8 +137,9 @@ function filefield_field_delete_revision($node, $field, &$items, $teaser, $page)
foreach ($items as $delta => $item) {
// For hook_file_references, remember that this is being deleted.
$item['field_name'] = $field['field_name'];
- if (field_file_delete($item)) unset($items[$delta]);
- $items = array_values($items); // compact deltas
+ if (field_file_delete($item)) {
+ unset($items[$delta]);
+ }
}
}
diff --git a/filefield_widget.inc b/filefield_widget.inc
index d175528..505af86 100644
--- a/filefield_widget.inc
+++ b/filefield_widget.inc
@@ -187,13 +187,57 @@ function filefield_widget_process($element, $edit, &$form_state, $form) {
unset($element['#title']);
}
- // Check if a remove button was clicked.
- $remove_name = $element['#field_name'] .'_'. $element['#delta'] .'_filefield_remove';
- if (isset($form_state['clicked_button']) && $form_state['clicked_button']['#name'] == $remove_name) {
+ // Set up the buttons first since we need to check if they were clicked.
+ $element['filefield_upload'] = array(
+ '#type' => 'submit',
+ '#value' => t('Upload'),
+ '#process' => array('form_expand_ahah'),
+ '#submit' => array('node_form_submit_build_node'),
+ '#ahah' => array( // with JavaScript
+ 'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
+ 'wrapper' => $element['#id'] .'-ahah-wrapper',
+ 'method' => 'replace',
+ 'effect' => 'fade',
+ ),
+ '#field_name' => $element['#field_name'],
+ '#delta' => $element['#delta'],
+ '#type_name' => $element['#type_name'],
+ '#upload_validators' => $element['#upload_validators'],
+ '#weight' => 100,
+ '#post' => $element['#post'],
+ );
+ $element['filefield_remove'] = array(
+ '#name' => $element['#field_name'] .'_'. $element['#delta'] .'_filefield_remove',
+ '#type' => 'submit',
+ '#value' => t('Remove'),
+ '#process' => array('form_expand_ahah'),
+ '#submit' => array('node_form_submit_build_node'),
+ '#ahah' => array( // with JavaScript
+ 'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
+ 'wrapper' => $element['#id'] .'-ahah-wrapper',
+ 'method' => 'replace',
+ 'effect' => 'fade',
+ ),
+ '#field_name' => $element['#field_name'],
+ '#delta' => $element['#delta'],
+ '#weight' => 101,
+ '#post' => $element['#post'],
+ );
+
+ // Because the output of this field changes depending on the button clicked,
+ // we need to ask FAPI immediately if the remove button was clicked.
+ // It's not good that we call this private function, but
+ // $form_state['clicked_button'] is only available after this #process
+ // callback is finished.
+ if (_form_button_was_clicked($element['filefield_remove'])) {
$item = array('fid' => 0, 'list' => $field['list_default'], 'data' => array('description' => ''));
}
- // figute out our fid...
+ // Set access on the buttons.
+ $element['filefield_upload']['#access'] = empty($item['fid']);
+ $element['filefield_remove']['#access'] = !empty($item['fid']);
+
+ // Figure out our fid...
$element['fid'] = array('#type' => 'hidden', '#value' => $item['fid']);
if ($item['fid'] != 0) {
@@ -251,40 +295,6 @@ function filefield_widget_process($element, $edit, &$form_state, $form) {
$element['#attributes']['id'] = $element['#id'] .'-ahah-wrapper';
$element['#prefix'] = '<div '. drupal_attributes($element['#attributes']) .'>';
$element['#suffix'] = '</div>';
- $element['filefield_upload'] = array(
- '#type' => 'submit',
- '#value' => t('Upload'),
- '#process' => array('form_expand_ahah'),
- '#submit' => array('node_form_submit_build_node'),
- '#ahah' => array( // with JavaScript
- 'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
- 'wrapper' => $element['#id'] .'-ahah-wrapper',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- '#field_name' => $element['#field_name'],
- '#delta' => $element['#delta'],
- '#type_name' => $element['#type_name'],
- '#upload_validators' => $element['#upload_validators'],
- '#access' => empty($item['fid']),
- );
-
- $element['filefield_remove'] = array(
- '#name' => $element['#field_name'] .'_'. $element['#delta'] .'_filefield_remove',
- '#type' => 'submit',
- '#value' => t('Remove'),
- '#process' => array('form_expand_ahah'),
- '#submit' => array('node_form_submit_build_node'),
- '#ahah' => array( // with JavaScript
- 'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
- 'wrapper' => $element['#id'] .'-ahah-wrapper',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- '#field_name' => $element['#field_name'],
- '#delta' => $element['#delta'],
- '#access' => !empty($item['fid']),
- );
return $element;
}