You are here

function petreference_field in Previewable email templates 6

Implementation of hook_field().

File

modules/petreference/petreference.module, line 171
Defines a field type for referencing pet template to a node.

Code

function petreference_field($op, &$node, $field, &$items, $teaser, $page) {
  static $sanitized_nodes = array();
  switch ($op) {

    // When preparing a translation, load any translations of existing references.
    case 'prepare translation':
      $addition = array();
      $addition[$field['field_name']] = array();
      if (isset($node->translation_source->{$field}['field_name']) && is_array($node->translation_source->{$field}['field_name'])) {
        foreach ($node->translation_source->{$field}['field_name'] as $key => $reference) {
          $reference_node = pet_load($reference['name']);

          // Test if the referenced node type is translatable and, if so,
          // load translations if the reference is not for the current language.
          // We can assume the translation module is present because it invokes 'prepare translation'.
          if (translation_supported_type($reference_node->type) && !empty($reference_node->language) && $reference_node->language != $node->language && ($translations = translation_node_get_translations($reference_node->tnid))) {

            // If there is a translation for the current language, use it.
            $addition[$field['field_name']][] = array(
              'pid' => isset($translations[$node->language]) ? $translations[$node->language]->pid : $reference['pid'],
            );
          }
        }
      }
      return $addition;
    case 'validate':

      // Extract pids to check.
      $ids = array();
      foreach ($items as $delta => $item) {
        if (is_array($item) && !empty($item['pid'])) {
          if (is_numeric($item['pid'])) {
            $ids[] = $item['pid'];
          }
          else {
            $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
            if (is_array($item) && isset($item['_error_element'])) {
              unset($item['_error_element']);
            }
            form_set_error($error_element, t("%name: invalid input.", array(
              '%name' => t($field['widget']['label']),
            )));
          }
        }
      }

      // Prevent performance hog if there are no ids to check.
      if ($ids) {
        $refs = _petreference_potential_references($field, '', NULL, $ids);
        foreach ($items as $delta => $item) {
          if (is_array($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['pid']) && !isset($refs[$item['pid']])) {
              form_set_error($error_element, t("%name: this post can't be referenced.", array(
                '%name' => t($field['widget']['label']),
              )));
            }
          }
        }
      }
      return $items;
    case 'sanitize':

      // Extract nids to check.
      $ids = array();
      foreach ($items as $delta => $item) {
        if (is_array($item)) {

          // Default to 'non accessible'.
          $items[$delta]['safe'] = array();
          if (!empty($item['pid']) && is_numeric($item['pid'])) {
            $ids[] = $item['pid'];
          }
        }
      }
      if ($ids) {

        // Load information about nids that we haven't already loaded during
        // this page request.
        $missing_ids = array_diff($ids, array_keys($sanitized_nodes));
        if (!empty($missing_ids)) {
          $where = array(
            'p.pid in (' . db_placeholders($missing_ids) . ')',
          );
          $result = db_query(db_rewrite_sql('SELECT p.pid, p.title, p.name FROM {pets} p WHERE ' . implode(' AND ', $where)), $missing_ids);
          while ($row = db_fetch_array($result)) {
            $sanitized_nodes[$row['pid']] = $row;
          }
        }
        foreach ($items as $delta => $item) {
          if (is_array($item) && !empty($item['pid']) && isset($sanitized_nodes[$item['pid']])) {
            $items[$delta]['safe'] = $sanitized_nodes[$item['pid']];
          }
        }
      }
      return $items;
  }
}