Created
September 24, 2018 09:58
-
-
Save init90/7bee7d16d26c59954a994628827ff07d to your computer and use it in GitHub Desktop.
Drupal 8, attach field collection to some entity. In this example to commerce product entity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use Drupal\commerce_product\Entity\Product; | |
| use Drupal\field_collection\Entity\FieldCollectionItem; | |
| $product = Product::load(34); | |
| $fc_item = FieldCollectionItem::create([ | |
| 'field_name' => 'ENTITY_FIELD_COLLECTION_FIELD', | |
| ]); | |
| $fc_item->set('field_horizontal_position', $horizontal_pos); | |
| $fc_item->set('field_vertical_position', $vertical_pos); | |
| $fc_item->setHostEntity($product, FALSE); | |
| $product->ENTITY_FIELD_COLLECTION_FIELD[] = [ | |
| 'field_collection_item' => $fc_item, | |
| ]; | |
| $product->save(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment