You are here

function font_reference_field_validate in @font-your-face 7

Same name and namespace in other branches
  1. 7.2 modules/font_reference/font_reference.module \font_reference_field_validate()

Implements hook_field_validate().

File

modules/font_reference/font_reference.module, line 69
Defines a field type for referencing a font from a node.

Code

function font_reference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  $ids = array();
  foreach ($items as $delta => $item) {
    if (is_array($item) && !empty($item['fid'])) {
      if (is_numeric($item['fid'])) {
        $ids[] = $item['fid'];
      }
      else {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'invalid_fid',
          'message' => t('%name: invalid input.', array(
            '%name' => $instance['label'],
          )),
        );
      }

      // else
    }

    // if
  }

  // foreach
  if ($ids) {
    foreach ($items as $delta => $item) {
      if (is_array($item) && !empty($item['fid'])) {
        $font_exists = db_query_range("SELECT fid FROM {fontyourface_font} WHERE fid = :fid", 0, 1, array(
          ':fid' => $item['fid'],
        ))
          ->fetchObject();
        if (!$font_exists) {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'invalid_fid',
            'message' => t("%name: this font can't be referenced.", array(
              '%name' => $instance['label'],
            )),
          );
        }

        // if
      }

      // if
    }

    // foreach
  }

  // if
}