the current implementation of entityreference_autocreate_new_entity() doesn't work for arbitrary entity api entities.

as entityreference depends on entity it should be safe to depend on entity_create() instead of entity_create_stub_entity().

Comments

dasjo’s picture

Status: Active » Needs review
StatusFileSize
new1.38 KB

the attached patch has been tested with entites based on the While module.

dman’s picture

Thanks. This has been on the roadmap, but wasn't an itch I had time to scratch myself. I'd hoped there would be a graceful way to do it ...
Mmm. $label_key. That looks like what I was hoping to see.

dasjo’s picture

hi dman, glad you like it!

i haven't tested it with nodes, but i think it should pretty much work that way :)

dman’s picture

Testing... (half notes-to-self) my drush-iq utility is broken since the d.o upgrade so..

git clone --branch 7.x-1.x dman@git.drupal.org:project/entityreference_autocreate.git
cd entityreference_autocreate
wget https://drupal.org/files/issues/2133167_entityreference_autocreate_entity_api_support_1.patch
git checkout -b 2133167_entityreference_autocreate_entity_api_support
git apply 2133167_entityreference_autocreate_entity_api_support_1.patch 

..
* using an existing site, haf-populated with entityrefs already...
* Add a new entityref field to a content type. This time to a 'user' entity, why not... with an 'autocomplete' widget.
* Target selection 'simple'
* Autocreate target if not found
* Edited an existing node, entered 'John' as the user to see if it would create a user for me = There are no entities matching "John"
* So that validation is not being skipped. :-(

Try again - maybe it will work on taxonomies (although they already support 'tag' style autocomplete natively, I should be able to double it up with an entityreference way of handling it - if we now have generic 'entity' support ...

Notice: Undefined property: stdClass::$vid in taxonomy_term_save() (line 625 of /Library/WebServer/Documents/drupal7/modules/taxonomy/taxonomy.module).
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 173 of /Library/WebServer/Documents/drupal7/includes/entity.inc).
Notice: Trying to get property of non-object in taxonomy_term_save() (line 626 of /Library/WebServer/Documents/drupal7/modules/taxonomy/taxonomy.module).
EntityMalformedException: Missing bundle property on entity of type taxonomy_term. in entity_extract_ids() (line 7693 of /Library/WebServer/Documents/drupal7/includes/common.inc).

Soo, it tried to make a term but with an unknown vid. That's not allowed so it failed. Unhappily and broke.

Looks like we need to take lots of care and special cases for each entity out there, there may not be a one-size-fits-all "make me a thing" solution here.
So far the other core entities, taxonomy term and user have both totally failed. It may be that we don't *expect* to have to use this utility on those entities, but if we can't get those right, how can we expect arbitrary contrib entities to work any more reliably?

dasjo’s picture

the problem is that core entities aren't build in a uniform, reusable way as entity api entities are. so you'll need special coding like #2053789: Add support for entity type taxonomy for those.

the above patch should still work for most custom entities that can be created without requiring any special information.

my suggestions is to define optional callbacks for the entity creation based on the entity type.

you can then provide the necessary callbacks for the core entities and leave it up to contrib modules to provide creation callbacks for themselves. in the case of the while entity this isn't needed as it doesn't need any further parameters.

fago’s picture

Patch looks good. Not necessarily part of this issue, but the module should respect all entity keys and apply bundle and language keys also.

fago’s picture

Bundles for terms are problematic yep. I guess we could provide a creation callback for terms in entity api that sets the 'vid' if at least the bundle is specified.

dman’s picture

I can't get this in while it dies so badly on just the core entities ... So I merged part of #2053789: Add support for entity type taxonomy in and added partial user support to the point where they don't break.
I tried it on another random contrib entity (wsclient) and discovered new problems, (though I don't expect there to be any sane reason you'd want to refer to a wsclient, it does seem that it demonstrates that all entities are inconsistent.

We can add a hook callback in there for the hell of it, maybe as sketched out in #2053789: Add support for entity type taxonomy - though it should be a Drupal hook callback, not a hook_alter. But I really don't expect any contribs to start doing that for us.

dman’s picture

dasjo’s picture

i think we really should use the entity_create() function instead of (object) array ....

so i suggest preparing the values within the switch statement (or ideally moving that logic to a callback) and afterwards properly create the entities.

fago’s picture

i think we really should use the entity_create() function instead of (object) array ....

yes. entity-saving stdclass objects won't work with entity types that use classes

dman’s picture

Yay. It's new core function names like that I wasn't even aware of when I first threw this together :-)

dman’s picture

Ah boo. Well this is why.
It would introduce a dependency on entity API module - which isn't yet needed.

I've got nothing *against* EntityAPI ... but I dislike including a whole module just to use one convenience function .... Needs thinking about.
If EntityAPI also provides better validation and default value management and error handling for creating stubs - then yep.
Otherwise it's currently not-broken using (object) ... unless someone can give an example where this really fails?

dasjo’s picture

Note that entityreference already depends on entity api, so that shouldn't be an issue :)

dman’s picture

Ah Righty then :-) Thanks!

bennos’s picture

Patch in #8 works for taxonomy. No problems with Taxonomy term (autocomplete widget and autocomplete tag style widget.

dman’s picture

Ages later, I got a bump on this project.
I've now merged all the above bits into dev, and some significant refactoring in too, based on some of the hints from the above issue.

Trialled with nodes, taxonomy, users (it looks like it was just them that was the major spanner in the works) as well as "while" entities (they are cute - I'd not followed up the first reference to them because it sounded like some timekeeping app I didn't really want to try out)

So far so good. I've been able to rapidly make vast networks of references, and seem to have eliminated a few rough corners also.
I've put it right into -dev because I was fixing up lots of stuff and didn;t want to lose everything again for another six months.

Thanks for everyones input above!

Yuri’s picture

The patch #9 doesn't patch any more.

dman’s picture

Status: Needs review » Fixed

The patch from 2013 won't apply, as in comment #17 most of the things it did were merged into -dev in 2014.
The system now *attempts* to do support of arbitrary entities (can do users, probably ECK things, and I think field_collections) but we also know that different entities are different.
So: 'fixed' to the point where it works, but not to where it's sure to always sure to work.

If you have a use-case to test against a specific new entity type, please open a new issue, with details how to replicate.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.