Problem/Motivation
I cannot reference a entity when creating another entity which will reference the fresh created first entity.
I do Drupal for 15+ years and never had that problem. :-/ I call an endpoint in Drupal to create a bunch of entities inside a transaction, were a lot of entities will get a reference to the first entity I create. I create all entities as a normal user, BUT with uid of an user with Administrator permissions.
The creation of the first entity works without problems. I get an ID back and also ->validate() works without problems. When I create the next entity, which has a reference field to the first entity. The ValidReferenceConstraintValidator
will always come to the conclusion, that the ID of the first object cannot be referenced.
I tracked down the problem into this method: $valid_target_ids = $handler->validateReferenceableEntities($target_ids);
which will create a fairly simple query to find entities in the db, to check if the to reference entity truely exists. The query is something like this:
SELECT base_table.vid AS vid, base_table.nid AS nid
FROM node base_table
INNER JOIN node_field_data node_field_data ON node_field_data.nid = base_table.nid
WHERE (node_field_data.type IN ('case')) AND (node_field_data.nid IN ('8869'))
Bit every time, the internal Drupal Processing, probably the accessChecks, will come to the conclusion, that I cannot reference it.
I did removed the Transaction to see if the entity will be written to the node table, and yes it did and also the Query will return the ID inside an SQL Manager program.
Which brings me to the conclusion, that there is some right, which is currently unknown to me, which prevents the referencing of entities.
I do not habe permissions set on any reference field. There is also no permission known to me which allows referencing of entities. The strange thing is, that when I omit the reference, save the entity and after that set the reference and save again it works without problems. But because I do require the reference of to the first entity, I cannot use that workflow.
I tried to step through the sqlQuery build and execution, but the internals of that code is to advanced and to abstract for me to easily understand.
Has anybody a further idea, what could prevent me from referencing entities together in a transaction?