You are here

function recipe_node_sanitize in Recipe 7

Same name and namespace in other branches
  1. 7.2 recipe.module \recipe_node_sanitize()

Sanitizes recipe data for display.

All recipe fields should be run through one of the Drupal data checks.

1 call to recipe_node_sanitize()
recipe_view in ./recipe.module
Implements hook_view().

File

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

Code

function recipe_node_sanitize(&$node) {

  // NOTE: This is a kludge because we should be saving each field's format is a table.
  // Drupal7 no longer does this for us.
  $filter_formats = filter_formats();
  $filter_format_id = NULL;
  if (isset($filter_formats['filtered_html'])) {
    $filter_format_id = 'filtered_html';
  }
  elseif (isset($filter_formats['1'])) {

    // Sites converted from Drupal6 will have a '1' instead of 'filtered_html'.
    $filter_format_id = '1';
  }

  // Preview uses a render array for WYSIWYG fields.
  if (isset($node->in_preview) && $node->in_preview == 1) {
    $node->recipe_description = $node->recipe_description['value'];
    $node->recipe_instructions = $node->recipe_instructions['value'];
    $node->recipe_notes = $node->recipe_notes['value'];
  }

  // Handle WYSIWYG fields first.
  if (!empty($node->recipe_description)) {
    $node->recipe_description = check_markup($node->recipe_description, $filter_format_id);
  }
  if (!empty($node->recipe_instructions)) {
    $node->recipe_instructions = check_markup($node->recipe_instructions, $filter_format_id);
  }
  if (!empty($node->recipe_notes)) {
    $node->recipe_notes = check_markup($node->recipe_notes, $filter_format_id);
  }

  // Handle the rest of the recipe table fields.
  if (!empty($node->recipe_source)) {
    $node->recipe_source = filter_xss($node->recipe_source, array(
      'a',
    ));
  }
  if (!empty($node->recipe_yield_unit)) {
    $node->recipe_yield_unit = filter_xss($node->recipe_yield_unit, array());
  }

  // Handle the ingredient fields.
  if (isset($node->recipe_ingredients['ing'])) {
    $tmp = $node->recipe_ingredients['ing'];
    $node->recipe_ingredients['ing'] = array();
    foreach ($tmp as $ingredient) {

      // Skip the more ingredients button which shows up during the node rebuild in preview.
      if (is_scalar($ingredient)) {
        continue;
      }
      if (isset($ingredient['name'])) {
        $ingredient['name'] = filter_xss($ingredient['name'], array());
      }
      if (isset($ingredient['unit_key'])) {
        $ingredient['unit_key'] = filter_xss($ingredient['unit_key'], array());
      }
      if (isset($ingredient['note'])) {
        $ingredient['note'] = filter_xss($ingredient['note'], array());
      }
      $node->recipe_ingredients['ing'][] = $ingredient;
    }
  }
}