I have a certain content-type B with an entity reference to items of content-type A.
I'm trying to programmatically create a content-type B node when a bool field in content-type A is set to true
, whether during creation of content-type A or update/editing of content-type A.
Something like this story:
When user creates or edits a
news
content (ie, sets thepush_to_slider
field astrue
), create aslide
content if it doesn't exist. If the user sets the field asfalse
, delete theslide
content.
I've been exploring how to do this, and I found out about hook_entity_presave
. So, right now, I'm able to check what was edited in the content-type A form, and if it is being created or updated.
My questions are:
How can I find out the ID of the content-type A node? I need to know it so I can pass it along when I
getStorage('type-B')->create(array)
. This might be available when editing a A-type node, but not when inserting it, at least not inhook_entity_presave
. As far as I can tell, I might be doing this in the wrong place as this hook operates before the DB insertion, is this correct? As such I think I wouldn't have access to an ID at this point. What could be the alternative?Also, how can I find out if a B-type content already exists referencing the A-type node I'm working with? I intend to use that to find out if what I need to delete in case the option in the form is set to
false
.
Hope this makes sense. If not, let me know and I'll try to clarify. Any help would be very appreciated.