/** * Ensures field_collection_field_storage_config_insert() works correctly. */ public function testDuplicateFieldCollection() { $values = ['type' => 'field_collection', 'field_name' => 'my_collection', 'label' => 'My Collection']; FieldStorageConfig::create($values + ['entity_type' => 'user'])->save(); // Change the label of the field collection. $field_collection_first = FieldCollection::load('my_collection'); $field_collection_first->set('label', 'A new label'); $field_collection_first->save(); FieldStorageConfig::create($values + ['entity_type' => 'node'])->save(); // If field_collection_field_storage_config_insert() had created a new field // collection, it would not have the custom name. $field_collection_second = FieldCollection::load('my_collection'); $this->assertSame('A new label', $field_collection_second->label()); }
/** * The _title_callback for the field_collection_item.add route. * * @param \Drupal\field_collection\Entity\FieldCollection $field_collection * The current field collection. * * @return string * The page title. */ public function addPageTitle(FieldCollection $field_collection) { return $this->t('Create @label', array('@label' => $field_collection->label())); }
/** * Test deleting the field corresponding to a field collection. */ public function testFieldDeletion() { // Create a separate content type with the field collection field. $this->drupalCreateContentType(array('type' => 'test_content_type', 'name' => 'Test content type')); $field_collection_field_1 = $this->field_collection_field; $field_collection_field_2 = $this->addFieldCollectionFieldToContentType('test_content_type'); list(, $field_collection_item_1) = $this->createNodeWithFieldCollection('article'); list(, $field_collection_item_2) = $this->createNodeWithFieldCollection('test_content_type'); /** @var \Drupal\field_collection\FieldCollectionItemInterface $field_collection_item_1 */ $field_collection_item_id_1 = $field_collection_item_1->id(); /** @var \Drupal\field_collection\FieldCollectionItemInterface $field_collection_item_2 */ $field_collection_item_id_2 = $field_collection_item_2->id(); $field_collection_field_1->delete(); $this->assertNull(FieldCollectionItem::load($field_collection_item_id_1), 'field_collection_item deleted with the field_collection field.'); $this->assertNotNull(FieldCollectionItem::load($field_collection_item_id_2), 'Other field_collection_item still exists.'); $this->assertNotNull(FieldCollection::load($this->field_collection_name), 'field_collection config entity still exists.'); $field_collection_field_2->delete(); $this->assertNull(FieldCollectionItem::load($field_collection_item_id_2), 'Other field_collection_item deleted with it\'s field.'); $this->assertNull(FieldCollection::load($this->field_collection_name), 'field_collection config entity deleted.'); }