Drupal 7 中文教程 字段验证 ISBN验证

<?php

/**
 * @file
 * isbn validation
 */

Drupal info文件:

name = ISBN Validation
description = "Add ISBN validation to text field"
core = 7.x
dependencies[] = field
dependencies[] = field_ui

module文件:

/**
 * Implements hook_field_attach_validate().
 */
function isbn_validation_field_attach_validate($entity_type, $entity, &$errors) {
 list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
 
 if($entity_type =='node' &&  $bundle == 'book'){
   $field_isbn_length = strlen($entity->field_isbn['und'][0]['value']);
   if(($field_isbn_length != 10) && ($field_isbn_length != 13)){
    $errors['field_isbn']['und'][0][] = array(
     'error' => 'field_isbn',
     'message' => t('无效的ISBN号.'),
    );
   }  
 }
}