You are here

function cloneimagefield_clone_node_alter in Node clone 5

Same name and namespace in other branches
  1. 5.2 cloneimagefield.module \cloneimagefield_clone_node_alter()

Implementation of hook_clone_node_alter() (a clone.module hook).

File

./cloneimagefield.module, line 18

Code

function cloneimagefield_clone_node_alter(&$node, $original_node, $method) {
  $content_type = module_invoke('content', 'types', $node->type);
  $image_fields = array();

  // Find all the image fields
  foreach ($content_type['fields'] as $data) {
    if ($data['type'] == 'image') {
      $image_fields[] = $data['field_name'];
    }
  }
  $clone_imagefield = array();

  // Put back the imagefields from the original node.
  foreach ($image_fields as $key) {
    if (isset($original_node->{$key})) {
      $node->{$key} = $original_node->{$key};

      // Make a copy of the imagefield data.
      $clone_imagefield[$key] = $original_node->{$key};
    }
  }
  if ($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 ($method == "save-edit") {
    foreach ($image_fields as $key) {
      foreach ($node->{$key} as $delta => $file) {
        _cloneimagefield_copy_file($node, $key, $delta);
      }
    }
  }
}