You are here

function recipe_ingredient_id_from_name in Recipe 7

Same name and namespace in other branches
  1. 5 recipe.module \recipe_ingredient_id_from_name()
  2. 6 recipe.module \recipe_ingredient_id_from_name()
  3. 7.2 recipe.module \recipe_ingredient_id_from_name()

Converts a recipe ingredient name to an ID.

1 call to recipe_ingredient_id_from_name()
recipe_save_ingredients in ./recipe.module
Saves the ingredients of a recipe node to the database.

File

./recipe.module, line 1099
Contains functions for Recipe node CRUD and display.

Code

function recipe_ingredient_id_from_name($name) {
  static $cache;
  if (!isset($cache[$name])) {
    $ingredient_id = db_query("SELECT id FROM {recipe_ingredient} WHERE LOWER(name) = :name", array(
      ':name' => trim(strtolower($name)),
    ))
      ->fetchField();
    if (!$ingredient_id) {
      global $active_db;
      $node_link = db_query("SELECT nid FROM {node} n WHERE type = 'ingredient' && title = :title;", array(
        ':title' => $name,
      ))
        ->fetchField();
      if (!$node_link) {
        $node_link = 0;
      }

      // Don't convert to lowercase if there is a ® (registered trademark symbol).
      if (variable_get('recipe_ingredient_name_normalize', 0) == 1 && !preg_match('/®/', $name)) {
        $name = trim(strtolower($name));
      }
      db_insert('recipe_ingredient')
        ->fields(array(
        'name' => $name,
        'link' => $node_link,
      ))
        ->execute();

      // Get the ID that you just added.
      $ingredient_id = db_query("SELECT id FROM {recipe_ingredient} WHERE LOWER(name) = :name", array(
        ':name' => trim(strtolower($name)),
      ))
        ->fetchField();
    }
    $cache[$name] = $ingredient_id;
  }
  return $cache[$name];
}