You are here

function isbn_field in ISBN Field 6

Implementation of hook_field(). http://drupal.org/node/342996

File

./isbn.module, line 221
Defines ISBN field types.

Code

function isbn_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
          $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
          if (is_array($item) && isset($item['_error_element'])) {
            unset($item['_error_element']);
          }
          if (!empty($item['value'])) {

            //VALIDATE ISBN HERE
            if ($field['isbn_validation'] != ISBN_NO_VALIDATION) {
              $valid = isbn_validate_number($item['value'], $field['isbn_format']);

              //               if (!$valid) {
              //                 form_set_error($error_element, t('ISBN/ISSN Validation Failed'));
              if (!isset($valid) || $valid['valid'] == false) {
                switch ($valid['message']) {
                  case 'confirmSuccess':
                    form_set_error($error_element, t('Valid ISBN/ISSN number.'));
                    break;
                  case 'confirmFailure':
                    form_set_error($error_element, t('Not a valid ISBN/ISSN Number.'));
                    break;
                  case 'notEnoughDigits':
                    form_set_error($error_element, t('Not Enough Digits!  '));
                    break;
                  case 'tooManyDigits':
                    form_set_error($error_element, t('Too Many Digits!  '));
                    break;
                  case 'requiredDigits':
                    form_set_error($error_element, t(' digits are required.'));
                    break;
                  case 'missingPrefix13':
                    form_set_error($error_element, t('13 Digit ISBNs need to begin with 978 or 979.'));
                    break;
                  default:
                    form_set_error($error_element, t('ISBN/ISSN Validation Failed'));
                }
              }

              //              }
            }
          }
        }
      }
      return $items;
    case 'sanitize':
      foreach ($items as $delta => $item) {
        $isbn_formatted = isbn_clean($item['value'], $field['isbn_format']);
        $items[$delta]['clean'] = $isbn_formatted;
      }
  }
}