You are here

function getlocations_fields_field_validate in Get Locations 7.2

Same name and namespace in other branches
  1. 7 modules/getlocations_fields/getlocations_fields.module \getlocations_fields_field_validate()

Implements hook_field_validate(). Validate this module's field data.

This hook gives us a chance to validate content that's in our field. We're really only interested in the $items parameter, since it holds arrays representing content in the field we've defined.

Parameters

$entity_type: The type of $entity.

$entity: The entity for the operation.

$field: The field structure for the operation.

$instance: The instance structure for $field on $entity's bundle.

$langcode: The language associated with $items.

$items: $entity->{$field['field_name']}[$langcode], or an empty array if unset.

$errors: The array of errors (keyed by field name, language code, and delta) that have already been reported for the entity.

File

modules/getlocations_fields/getlocations_fields.module, line 138
getlocations_fields.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_fields_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  if (!empty($items)) {
    $required = $instance['required'];
    $cardinality = $field['cardinality'];
    $count_items = count($items);
    $fieldnames = array(
      'name' => t('Name'),
      'street' => t('Street'),
      'additional' => t('Additional'),
      'city' => t('City/Town'),
      'province' => t('Province/State/County'),
      'postal_code' => t('Post code/Zip code'),
      'country' => t('Country'),
      'phone' => t('Phone'),
      'mobile' => t('Mobile'),
      'fax' => t('Fax'),
    );
    $item_count = 0;
    foreach ($items as $delta => $item) {
      if ($item['active']) {
        if (isset($item['delete_location']) && $item['delete_location']) {
          return;
        }

        // unlimited so don't error on the last one
        if ($cardinality == -1 && $item_count == $count_items - 1) {
          return;
        }
        if ($required && empty($item['latitude']) && empty($item['longitude'])) {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'latlon_empty',
            'message' => t('Latitude and Longitude empty'),
            'field_name' => $field['field_name'],
            'lang' => $langcode,
            'delta' => $delta,
          );
        }
        if (isset($item['getlocations_required']) && $item['getlocations_required']) {
          $requireds = explode(',', $item['getlocations_required']);
          foreach ($requireds as $id) {
            if (isset($item[$id]) && empty($item[$id])) {
              $errors[$field['field_name']][$langcode][$delta][] = array(
                'error' => 'field_required_' . $id,
                'message' => t('The %tt field is required', array(
                  '%tt' => $fieldnames[$id],
                )),
              );
            }
          }
        }
      }
      $item_count++;
    }
  }
}