What is the “proper” way to get the value stored in a particular field within a custom Drupal node? I’ve created a custom module, with a custom node, with a custom URL field. The following works:
$result = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(
':title' => $title,
':type' => 'custom',
))->fetchField();
$node = node_load($result);
$url = $node->url['und']['0']['value'];
…but is there a better way, maybe using the new Field API functions?
node_load()then accessing the field as a property is the proper way, although I’d do it slightly differently to avoid hard-coding the locale:The method you’re using to get the nid is a particularly kludgy way to derive it; I’d focus on refactoring that and use
EntityFieldQueryand entity_load() instead:You’d want to do this especially since title is not a unique property and if the field appears on entities other than nodes. In that case you’d remove the
entityCondition().