What is the "correct" way to check if a node exists from within a custom module?

Scenario: 

Webform results data is converted to Node via node_save. BUT we don't want to duplicate, only update, if the admin adjusts the results form.

I'm comfortable with doing db_query ala D6 method, and have searched the API docs, but couldn't see any pertinent method.

Accepted answer

I'm having difficulties understanding the reason why you want to convert a webform submission into a node. Wouldn't it be easier to let people create nodes, instead of having them create a webform submission and then convert it programmatically into a node?

In any case, if you still want to do that, you have to decide which field should be unique (title? another field?). Then use EntityFieldQuery to check if a node with that field value exists. Here is info on how to use EFQ.

Most helpful answers

One more thought: it may be easier to have your admin edit the node directly, instead of the webform. That way the node won't be duplicated.

Hi onyx,

what information do you have at the point you want to check that the node exists?

Do you rely on the title? Or is a node id (nid) available?

Maybe it helps to better understand, if you provide your current query.

Comments

I'm having difficulties understanding the reason why you want to convert a webform submission into a node. Wouldn't it be easier to let people create nodes, instead of having them create a webform submission and then convert it programmatically into a node?

In any case, if you still want to do that, you have to decide which field should be unique (title? another field?). Then use EntityFieldQuery to check if a node with that field value exists. Here is info on how to use EFQ.

One more thought: it may be easier to have your admin edit the node directly, instead of the webform. That way the node won't be duplicated.

indigoxela's picture

Hi onyx,

what information do you have at the point you want to check that the node exists?

Do you rely on the title? Or is a node id (nid) available?

Maybe it helps to better understand, if you provide your current query.

I am using Webform because it has the hand-holding required for those that are struggling, and needs to be public entry point, yet the switch to published has to be admin-based because it will be targetted by trolls.

The site is a community for the vax injured, which has become a tsunami, and the traditional social media platforms are shutting them down because it's against the narrative to tell the truth of what happened to them.

So programmatically I will use some required unique values like email address and date of jab to determine uniqueness. Yes, even those may cause a problem if they have multiple stories to tell (many do, of family) and use the same data, so I have to figure that out.

Brain fog is a very common side-effect, so it has to be uber-simple. And I have to have it easy enough for the next admin to pick up too, who will likely be from the same community.

Thanks for the help guys!