I am trying to write a script that will create about a thousand instances of a custom content entity type I defined via a module from existing data that I have prepared. Right now I just need to figure out how to create an instance of my entity type programmatically.
This entity type contains multiple field collections. I don't understand how to structure the arguments of {entity_type}::create() to accommodate field collections.
Following the accepted answers to Programmatically create nodes and How to programmatically insert and update Field Collection values?, here is my function code so far (i have it inside hook_entity_view_alter()
just for easy testing):
$food = Food::create([
'type' => 'food',
'title' => 'Allspice',
'field_category' => 'Spices'
]);
$variation = FieldCollectionItem::create([
'field_ingredients' => 'allspice',
'field_safe_for' => [
'field_nut_allergies' => 'Yes',
'field_seafood_allergies' => 'Yes',
]
]);
$variation->setHostEntity($food);
$food->field_variations[] = ['field_collection_item' => $variation];
$food->save();
where food
is my entity type, field_variations
is the ID of the field collection in food
which may have unlimited values per food
instance, and field_safe_for
is a nested field collection.
No matter what I do, I always get this:
The website encountered an unexpected error. Please try again later.
Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type field_collection_item in Drupal\Core\Entity\ContentEntityStorageBase->doCreate() (line 83 of core\lib\Drupal\Core\Entity\ContentEntityStorageBase.php).
Drupal\Core\Entity\EntityStorageBase->create(Array) (Line: 509)
Drupal\Core\Entity\Entity::create(Array) (Line: 39)
food_food_view_alter(Array, Object, Object) (Line: 501)
Drupal\Core\Extension\ModuleHandler->alter(Array, Array, Object, Object) (Line: 268)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 203)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func(Array, Array) (Line: 381)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
which I do not understand in the slightest since my custom entity types don't make use of bundles.
How can I fix this?