Create content after editing other content-type 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 the push_to_slider field as true), create a slide content if it doesn't exist. If the user sets the field as false, delete the slide 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')-&gt;create(array). This might be available when editing a A-type node, but not when inserting it, at least not in hook_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.
How can I find out the ID of the content-type A node?

You should use devel module's kint function to get all available methods of an object, and you could easy find out, that it's: $node_object->id();

how can I find out if a B-type content already exists referencing the A-type node I'm working with?

You will write a Database service query (not pure sql query), where you will have two important condition:


the node type must be B 
it's reference field contains the id of the A content.


You will of course have to use table joins. To better understand the schema representation of your content types, I suggest you do use phpmyadmin parallel.