I have a form into which the user types a node ID. In the form validation, I want to check that it is a valid ID (i.e. it is a node and of the right type).
How would I do that?
I have a form into which the user types a node ID. In the form validation, I want to check that it is a valid ID (i.e. it is a node and of the right type).
How would I do that?
Nothing wrong with loading a node. The query is just slightly more efficient. As another alternative, you can use db_query on the node table as well. Again, slightly more efficient than EntityFieldQuery.
Nvm. Figured it out. I can just node_load and if it returns false, it doesn't exist. Then I can check the node type.
If you want to avoid loading the full node, you can do
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')->propertyCondition('nid', $my_nid);
$result = $query->execute();
if (empty($result['node'])) {
// The NID doesn't exist.
}
That is good to know. I suppose it's probably good to avoid loading nodes that I won't end up needing. :)
Can I ask what does the node id represent content wise?
Could you use Entity Reference and then as the user types the NID or stock code or title etc.. they would see the node title for example... would that be a more user friendly solution?
If you want to avoid loading the full node, you can do
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')->propertyCondition('nid', $my_nid);
$result = $query->execute();
if (empty($result['node'])) {
// The NID doesn't exist.
}