'. t('The Clone Imagefield module works with the Clone Module and Imagefiled Module (a CCK field-type module) to copy attached images to be made when a node is cloned.') .'

'; return $output; } } /** * Implementation of hook_clone_node_alter() (a clone.module hook). */ function cloneimagefield_clone_node_alter(&$node, $context) { $content_type = field_info_instances('node',$node->type); $image_fields = array(); // Find all the image fields foreach ($content_type as $index => $data) { if ($data['widget']['module'] == 'image') { $image_fields[] = $index; } } $clone_imagefield = array(); // Put back the imagefields from the original node. foreach ($image_fields as $key) { if (isset($context['original_node']->$key)) { $node->$key = $context['original_node']->$key; // Make a copy of the imagefield data. $clone_imagefield[$key] = $context['original_node']->$key; } } if ($context['method'] == "prepopulate") { // The imagefield data is a flag that we can find during the form_alter. $node->node_clone_imagefield = $clone_imagefield; if (empty($_POST['op'])) { drupal_set_message(t('Each pre-existing Imagefield file will be copied and attached when you submit (unless you select its "Delete" checkbox).')); } } elseif ($context['method'] == "save-edit") { foreach ($image_fields as $key) { global $language; $image = $node->$key; foreach ($image[$language->language] as $delta => $file) { _cloneimagefield_copy_file($node, $key, $delta); } } } } /** * Copy a specific attached file to a temporary location. */ function _cloneimagefield_copy_file(&$node, $key, $delta) { global $language; $image = (array)$node->{$key}[$language->language][$delta]; $source = file_load($image['fid']); // Create temporary name/path for duplicated files. On Windows, tempnam() // requires an absolute path, so we use realpath(). $dest = tempnam(realpath(file_directory_temp()), 'tmp_'); // Copy the file to the temporary location. if ($source=file_save_upload($source)) { // $source now holds the actual filename used for the copy. $image = $source; print_r($image); $image['fid'] = 'upload'; $image['nid'] = 0; } else { // Copy failed, remove the file from the list of attachements. unset($node->{$key}[$language->language][$delta]); } } /** * Implementation of hook_nodeapi(). * * Take advantage of the fact that imagefield.module does not use the 'submit' * $op to copy files at the 'submit' phase before the node is actually saved. * Only relevant when the "prepopulate" method is being used. */ function cloneimagefield_nodeapi(&$node, $op) { if ($op == 'submit' && isset($node->node_clone_imagefield)) { // The data was serialized so it could be passed as a hidden form field. $image_fields = unserialize($node->node_clone_imagefield); foreach (array_keys($image_fields) as $key) { $image = $node->$key; foreach ($image[$language->language] as $delta => $file) { // Check whether this file was attached to the original node, and that // it is not slated for removal. if (($file['uri'] == $image_fields[$key][$delta]['uri']) && empty($file['flags']['delete'])) { _cloneimagefield_copy_file($node, $key, $delta); } } } } } /** * Implementation of hook_form_alter(). */ function cloneimagefield_form_alter($form_id, &$form) { // Look for node forms. $file = drupal_get_path('module', 'node') . '/node.pages.inc'; require_once DRUPAL_ROOT . '/' . $file; // This piece of information can let other modules know that more files // need to be included if this form is loaded from cache: //$form_state['form_load_files'] = array($file); $form['build_info']['files'] = array($file); if (isset($form['type']['#value']) && $form['type']['#value'] .'_node_form' == $form_id) { if (isset($form['#node']->node_clone_imagefield)) { // If we are using the "prepopulate" method, store information about files // attached to the original node as a hidden form field. $form['node_clone_imagefield'] = array( '#type' => 'hidden', '#value' => serialize($form['#node']->node_clone_imagefield), ); } } }