You are here

function __og_field_validate in Organic groups 7

Implements hook_field_validate().

FIXME: Adapt this function to OG.

Possible error codes:

  • 'invalid_nid': nid is not valid for the field (not a valid node id, or the node is not referenceable).

File

./og.field.inc, line 633
Field module functionality for the Organic groups module.

Code

function __og_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {

  // Extract nids to check.
  $ids = array();

  // First check non-numeric "nid's to avoid losing time with them.
  foreach ($items as $delta => $item) {
    if (is_array($item) && !empty($item['nid'])) {
      if (is_numeric($item['nid'])) {
        $ids[] = $item['nid'];
      }
      else {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'invalid_nid',
          'message' => t("%name: invalid input.", array(
            '%name' => $instance['label'],
          )),
        );
      }
    }
  }

  // Prevent performance hog if there are no ids to check.
  if ($ids) {
    $refs = og_field_audience_potential_groups('', NULL, $ids);
    foreach ($items as $delta => $item) {
      if (is_array($item)) {
        if (!empty($item['nid']) && !isset($refs[$item['nid']])) {
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => 'invalid_nid',
            'message' => t("%name: this post can't be referenced.", array(
              '%name' => $instance['label'],
            )),
          );
        }
      }
    }
  }
}