You are here

function recipe_ingredient_id_from_name in Recipe 7.2

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 recipe.module \recipe_ingredient_id_from_name()

Converts a recipe ingredient name to an ID.

5 calls to recipe_ingredient_id_from_name()
RecipeNodeTest::testRecipeMultipleExport in src/Tests/RecipeNodeTest.php
Tests exporting recipes with the multiple export page.
RecipeYieldFormTest::testRecipeYieldForm in src/Tests/RecipeYieldFormTest.php
Tests the custom yield form functionality.
recipe_import_get_node in ./recipe.admin.inc
Returns a node-like stdClass object suitable for node_save and preview.
recipe_ingredient_autocomplete_validate in ./recipe.module
Form element validate handler for recipe ingredient autocomplete element.
recipe_metadata_field_ingredient_name_set in ./recipe.module
Callback for setting the ingredient iid of ingredient fields.

File

./recipe.module, line 998
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;
      }
      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];
}