diff --git a/field_collection.module b/field_collection.module index 645093a..21be6fe 100644 --- a/field_collection.module +++ b/field_collection.module @@ -895,6 +895,14 @@ function field_collection_field_settings_form($field, $instance) { * creation or to save changes to the host entity and its collections at once. */ function field_collection_field_presave($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) { + $force_new_entity = !empty($host_entity->is_new); + // Some special casing for nodes since is_new property is being set after this + // hook fires. Usually the is_new property/flag should be set before calling + // field_attach_presave(). + if (!$force_new_entity && $host_entity_type == 'node') { + $force_new_entity = empty($host_entity->nid); + } + foreach ($items as &$item) { // In case the entity has been changed / created, save it and set the id. // If the host entity creates a new revision, save new item-revisions as @@ -903,6 +911,12 @@ function field_collection_field_presave($host_entity_type, $host_entity, $field, if ($entity = field_collection_field_get_entity($item)) { + if ($force_new_entity) { + $entity->item_id = NULL; + $entity->revision_id = NULL; + $entity->is_new = TRUE; + } + if (!empty($entity->is_new)) { $entity->setHostEntity($host_entity_type, $host_entity, LANGUAGE_NONE, FALSE); } diff --git a/field_collection.test b/field_collection.test index b62d048..fd150f4 100644 --- a/field_collection.test +++ b/field_collection.test @@ -299,6 +299,33 @@ class FieldCollectionBasicTestCase extends DrupalWebTestCase { $this->drupalGet("node/$node->nid/revisions"); } + + /** + * If host-entities are copied field_collection-entities should be copied to. + * + * This fixes #1316162 + */ + function testCopyingEntities() { + list($node, $entity) = $this->createNodeWithFieldCollection(); + + // create a copy of that node. + $node->nid = NULL; + $node->vid = NULL; + $node->is_new = TRUE; + + node_save($node); + $item = $node->{$this->field_name}[LANGUAGE_NONE][0]; + $this->assertNotEqual($entity->item_id, $item['value']); + + // create another copy this time (needlessly) forcing a new revision. + $node->nid = NULL; + $node->vid = NULL; + $node->is_new = TRUE; + $node->revision = TRUE; + node_save($node); + $item = $node->{$this->field_name}[LANGUAGE_NONE][0]; + $this->assertNotEqual($entity->item_id, $item['value']); + } } @@ -420,3 +447,101 @@ class FieldCollectionRulesIntegrationTestCase extends DrupalWebTestCase { RulesLog::logger()->checkLog(); } } + +/** + * Test using field collection with content that gets translated. + */ +class FieldCollectionContentTranslationTestCase extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Field collection content translation', + 'description' => 'Tests using content under translation.', + 'group' => 'Field types', + 'dependencies' => array('locale'), + ); + } + + public function setUp() { + parent::setUp(array('field_collection', 'translation')); + // Create a field_collection field to use for the tests. + $this->field_name = 'field_test_collection'; + $this->field = array('field_name' => $this->field_name, 'type' => 'field_collection', 'cardinality' => 4); + $this->field = field_create_field($this->field); + $this->field_id = $this->field['id']; + + $this->instance = array( + 'field_name' => $this->field_name, + 'entity_type' => 'node', + 'bundle' => 'article', + 'label' => $this->randomName() . '_label', + 'description' => $this->randomName() . '_description', + 'weight' => mt_rand(0, 127), + 'settings' => array(), + 'widget' => array( + 'type' => 'field_collection_embed', + 'label' => 'Test', + 'settings' => array(), + ), + ); + $this->instance = field_create_instance($this->instance); + + // Add a field to the collection. + $field = array( + 'field_name' => 'field_text', + 'type' => 'text', + 'cardinality' => 1, + 'translatable' => FALSE, + ); + field_create_field($field); + $instance = array( + 'entity_type' => 'field_collection_item', + 'field_name' => 'field_text', + 'bundle' => $this->field_name, + 'label' => 'Test text field', + 'widget' => array( + 'type' => 'text_textfield', + ), + ); + field_create_instance($instance); + + $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages', 'create article content', 'edit any article content', 'translate content')); + + $this->drupalLogin($admin_user); + // Add German language. + locale_add_language('de'); + + // Set "Article" content type to use multilingual support. + variable_set('language_content_type_article', TRANSLATION_ENABLED); + } + + public function testContentTranslation() { + // Create "Article" content. + $edit['title'] = $this->randomName(); + $edit['body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName(); + $edit['language'] = 'en'; + $field_collection_name = 'field_test_collection[' . LANGUAGE_NONE . '][0][field_text][' . LANGUAGE_NONE . '][0][value]'; + $edit[$field_collection_name] = $this->randomName(); + + $this->drupalPost('node/add/article', $edit, t('Save')); + $this->assertRaw(t('Article %title has been created.', array('%title' => $edit['title'])), 'Article created.'); + $node1 = $this->drupalGetNodeByTitle($edit['title']); + + $this->drupalGet('node/' . $node1->nid . '/edit'); + $this->drupalGet('node/' . $node1->nid . '/translate'); + $this->drupalGet('node/add/article', array('query' => array('translation' => $node1->nid, 'target' => 'de'))); + + // Suffix translations with the langcode. + unset($edit['language']); + $edit['title'] .= 'DE'; + $edit[$field_collection_name] .= 'DE'; + $this->drupalPost('node/add/article', $edit, t('Save'), array('query' => array('translation' => $node1->nid, 'target' => 'de'))); + $node2 = $this->drupalGetNodeByTitle($edit['title']); + + // Ensure that our new node is the translation of the first one. + $this->assertEqual($node1->nid, $node2->tnid, 'Succesfully created translation.'); + // And check to see that their field collections are different. + $this->assertNotEqual($node1->field_test_collection, $node2->field_test_collection, 'Field collections between translation source and translation differ.'); + } + +}