You are here

function rb_cck_action_merge_multiple_values in Rules Bonus Pack 6

Action for 'rb_cck_action_merge_multiple_values'.

File

./rb_cck.module, line 867
Functions for extending CCK field management with Rules.

Code

function rb_cck_action_merge_multiple_values($source, $target, $settings) {

  // Get required metadata for the field.
  $source_info = content_fields($settings['source_field']);
  $target_info = content_fields($settings['target_field']);
  $storage_key = rb_cck_field_storage_key($settings['target_field']);

  // Verify that source and target fields are of the same type.
  if ($source_info['type'] != $target_info['type']) {

    // TODO: Write to watchdog.
    return;
  }

  // Loop through the source field content, insert into target.
  $anything_changed = FALSE;
  foreach ($source->{$settings['source_field']} as &$source_content) {

    // A simple in_array would be much quicker here, but there seems sometimes
    // to be _error_element added to the target node. Most disturbing.
    $value_present = FALSE;
    foreach ($target->{$settings['target_field']} as $target_content) {
      if ($source_content[$storage_key] == $target_content[$storage_key]) {
        $value_present = TRUE;
        continue;
      }
    }
    if (!$value_present) {
      $anything_changed = TRUE;
      $target->{$settings['target_field']}[] = $source_content;
    }
  }

  // If anything has changed, return the changed target node for saving by
  // Rules. Otherwise, just return.
  if ($anything_changed) {
    return array(
      'target' => $target,
    );
  }
  else {
    return;
  }
}