cloneimagefield.module in Node clone 5
File
cloneimagefield.module
View source
<?php
function cloneimagefield_help($section) {
switch ($section) {
case 'admin/help#cloneimagefield':
$output = '<p>' . 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.') . '</p>';
return $output;
}
}
function cloneimagefield_clone_node_alter(&$node, $original_node, $method) {
$content_type = module_invoke('content', 'types', $node->type);
$image_fields = array();
foreach ($content_type['fields'] as $data) {
if ($data['type'] == 'image') {
$image_fields[] = $data['field_name'];
}
}
$clone_imagefield = array();
foreach ($image_fields as $key) {
if (isset($original_node->{$key})) {
$node->{$key} = $original_node->{$key};
$clone_imagefield[$key] = $original_node->{$key};
}
}
if ($method == "prepopulate") {
$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 ($method == "save-edit") {
foreach ($image_fields as $key) {
foreach ($node->{$key} as $delta => $file) {
_cloneimagefield_copy_file($node, $key, $delta);
}
}
}
}
function _cloneimagefield_copy_file(&$node, $key, $delta) {
$source = $node->{$key}[$delta]['filepath'];
$dest = tempnam(realpath(file_directory_temp()), 'tmp_');
if (file_copy($source, $dest)) {
$node->{$key}[$delta]['filepath'] = $source;
$node->{$key}[$delta]['fid'] = 'upload';
$node->{$key}[$delta]['nid'] = 0;
}
else {
unset($node->{$key}[$delta]);
}
}
function cloneimagefield_nodeapi(&$node, $op) {
if ($op == 'submit' && isset($node->node_clone_imagefield)) {
$image_fields = unserialize($node->node_clone_imagefield);
foreach (array_keys($image_fields) as $key) {
foreach ($node->{$key} as $delta => $file) {
if ($file['filepath'] == $image_fields[$key][$delta]['filepath'] && empty($file['flags']['delete'])) {
_cloneimagefield_copy_file($node, $key, $delta);
}
}
}
}
}
function cloneimagefield_form_alter($form_id, &$form) {
if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id) {
if (isset($form['#node']->node_clone_imagefield)) {
$form['node_clone_imagefield'] = array(
'#type' => 'hidden',
'#value' => serialize($form['#node']->node_clone_imagefield),
);
}
}
}