We will probably need something like this:

/**
 * Implements hook_replicate_field_FIELD_TYPE().
 */
function multifield_replicate_field_multifield($entity, $entity_type, $field_name) {
  foreach ($entity->$field_name as $language => &$items) {
    foreach ($items as $delta => &$item) {
      unset($item['id']);
    }
  }
}

/**
 * Implements hook_node_clone_alter().
 */
function multifield_clone_node_alter(&$node, $context) {
  $multifields = multifield_get_fields();
  $instances = array_intersect_key(field_info_instances('node', $node->type), $multifields);
  foreach (array_keys($instances) as $field_name) {
    $machine_name = $multifields[$field_name];
    if (!empty($entity->{$field_name})) {
      foreach ($entity->{$field_name} as $langcode => &$items) {
        foreach ($items as $delta => &$items) {
          unset($item['id']);
        }
      }
    }
  }
}

Comments

Dave Reid created an issue. See original summary.

dave reid’s picture

Issue summary: View changes
dave reid’s picture

Issue summary: View changes
dave reid’s picture

Maybe it would be better to add this to hook_entity_presave(), that if the entity is new, it should not contain any ID values for the fields.

/**
 * Implements hook_node_clone_alter().
 */
function multifield_entity_presave($entity, $entity_type) {
  if ($entity_type == 'multifield') {
    return;
  }

  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  if (empty($id) || !empty($entity->is_new)) {
    $multifields = multifield_get_fields();
    $instances = array_intersect_key(field_info_instances($entity_type, $bundle), $multifields);
    foreach (array_keys($instances) as $field_name) {
      $machine_name = $multifields[$field_name];
      if (!empty($entity->{$field_name})) {
        foreach ($entity->{$field_name} as $langcode => &$items) {
          foreach ($items as $delta => &$items) {
            unset($item['id']);
          }
        }
      }
    }
  }
}
dave reid’s picture

Status: Active » Needs review
StatusFileSize
new2.18 KB

Patch should show failure when attempting to do a basic clone.

Status: Needs review » Needs work

The last submitted patch, 5: 2737769-cloning-failure.patch, failed testing.

The last submitted patch, 5: 2737769-cloning-failure.patch, failed testing.

dave reid’s picture

Status: Needs work » Needs review
StatusFileSize
new3.96 KB

Should have all the tests fixed now.

dave reid’s picture

StatusFileSize
new6.51 KB

Tests only for basic cloning, node clone, and replicate modules.

Status: Needs review » Needs work

The last submitted patch, 9: 2737769-cloning-failure.patch, failed testing.

The last submitted patch, 9: 2737769-cloning-failure.patch, failed testing.

dave reid’s picture

Status: Needs work » Needs review
StatusFileSize
new6.97 KB

Try that again.

Status: Needs review » Needs work

The last submitted patch, 12: 2737769-cloning-failure.patch, failed testing.

The last submitted patch, 12: 2737769-cloning-failure.patch, failed testing.

dave reid’s picture

Status: Needs work » Needs review
StatusFileSize
new8.75 KB

Hoory, expected failures in all the new tests. Combined patch with all new tests.

Status: Needs review » Needs work

The last submitted patch, 15: 2737769-fix-cloning.patch, failed testing.

The last submitted patch, 15: 2737769-fix-cloning.patch, failed testing.

dave reid’s picture

Status: Needs work » Needs review
StatusFileSize
new8.78 KB

Fixed the replicate calls to use the right way.

  • Dave Reid committed 3232d01 on 7.x-1.x
    Issue #2737769 by Dave Reid: Fixed Multifield values not saved with new...
dave reid’s picture

Status: Needs review » Fixed

Committed #18 to 7.x-1.x.

  • Dave Reid committed 3232d01 on 7.x-2.x
    Issue #2737769 by Dave Reid: Fixed Multifield values not saved with new...
dave reid’s picture

Title: Ensure multifield works with the replicate module » Ensure multifield works with cloned entities (Node Clone or Replicate modules)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.