0

I have drupal 7 and created a custom "content type" with several fields. just imagine i have:

  1. title field
  2. subtitle field

I need to search the data. I want to search titles and when i found my certain title I want to get corresponding subtitle (in the same row of that title)! I know I can do it by going to directly to my database and do it by db_query(and i have to know the structure of my database as well). but is there any API in this regard?
i need to get them inside my custom module

2

In this instance using db_query is appropriate...

$nids = db_query("SELECT nid FROM node WHERE title LIKE :title", array(':title' => '%'.$title.'%'))->fetchCol();

$nodes = node_load_multiple($nids);

foreach($nodes as $node) {
    $subtitle = $node->field_subtitle[LANGUAGE_NONE][0]['value'];
    // do things with the subtitle....
}

What has happened here is we are using db_query to search the node table for nodes with matching titles. Once we have the nids we call node_load_multiple and that loads the whole node(s). Drupal uses caching here so we don't have to necessarily connect to the DB at all to do this.

The resulting $node object has all of the fields already attached to it so knowing where they are actually stored in the DB at this point is irrelevant.

As Clive has pointed out we shouldn't really access field items directly as using field_get_times or EntityMetadataWrapper will ensure that we always load the field items in the correct language (the language of the current user).

To use field_get_items you could:

foreach($nodes as $node) {

    // using field_get_items here automatically loads the field in the correct language
    $field_items = field_get_items('node', $node, 'field_subtitle');
    $subtitle = $field_items[0]['safe_value'];

    // do things with the subtitle....
}
| improve this answer | |
  • thank you man! but i think subtitled is another table. dose it matter? – PersianMan Apr 3 '14 at 8:09
  • 1
    You shouldn't access field data directly, always use field_get_items or an EntityMetadataWrapper – Clive Apr 3 '14 at 8:17
  • @Clive - are there any resources that explain the risks of accessing field items directly? Is it actually always that bad? I'm just wondering if I should change my habits surrounding this... – Felix Eve Apr 3 '14 at 8:27
  • 2
    Both the alternatives are multi-language safe, that's usually the main argument (values come in the current language without having to specify). I don't know where that's officially written down, but it's definitely true :) 'Risks' and 'bad' are a bit strong, the alternatives are just better practice, that's all – Clive Apr 3 '14 at 8:32
  • Right, well I'm glad I haven't been writing insecure code all this time! Having only ever been involved in single language sites my language has always been LANGUAGE_NONE so not really had to address this issue before. – Felix Eve Apr 3 '14 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.