Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm working on a site that has an existing eck entity type and I want to create an instance of that entity. For non-ECK things, I'm using Node::create()
, etc. When I run the same code with EckEntity::create()
, I get Drupal\Core\Entity\Exception\AmbiguousEntityClassException: Multiple entity types found for Drupal\eck\Entity\EckEntity
.
How can I create a specific EckEntity instance programmatically? As an example, there's an entity type example
with a bundle of example_day
. I haven't been able to nail down any way around this exception.
Thanks!
Comments
Comment #2
rballou CreditAttribution: rballou at Aten Design Group commentedComment #3
legolasboYou could try the following:
Where 'example' is replaced with the entity type.
Comment #4
rballou CreditAttribution: rballou at Aten Design Group commentedI should have updated this: I did have luck with entity_create(). Using the entity class doesn't work because of the error above. I'm just not sure if there is a way around it.
FWIW, Drupal::entityManager() is deprecated.
Comment #5
joecrespo CreditAttribution: joecrespo commentedComment #6
rballou CreditAttribution: rballou at Aten Design Group commentedI see what you did there, Joe. :)
The storage class does seem to work. I guess that might be the more "unified" way to get that built with everything else.
Comment #7
legolasboI'm marking this issue fixed since it answers the question how to create an ECK entity. I do agree there is a need to refactor the module to better comply with the way other entities are created troughout core, but that is something for another issue.
Comment #9
drupalninja99 CreditAttribution: drupalninja99 commentedCould we add this to the documentation?
Comment #10
legolasboFeel free to submit a patch to add this to eck.api.php
Comment #11
nnevillHere is working code:
Comment #12
man-1982 CreditAttribution: man-1982 as a volunteer commentedPlease, tell me. How create ECK (programmatically) with some fields, ex .field_description, field_amount?
Comment #13
paul_leclerc CreditAttribution: paul_leclerc commentedFew years after @man-1982, that's how I did it.
http://xxxxx:8020/admin/structure/eck/ENTITY_TYPE/bundles/ENTITY_BUNDLE/...
You'll need this patch : https://www.drupal.org/files/issues/2019-05-02/eck-create_method_fixes_m...
Allowing ECK Entity to create an entity when you have several entity types created.
Comment #14
avinashm CreditAttribution: avinashm as a volunteer and commentedThank you @paul_leclerc for the reference code.