Contributions API

nodereference_field

Definition

nodereference_field($op, &$node, $field, &$items, $teaser, $page)
contributions/cck/modules/nodereference/nodereference.module, line 168

Description

Implementation of hook_field().

Code

<?php
function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
  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 = node_load($reference['nid']);
          // 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(
              'nid' => isset($translations[$node->language]) ? $translations[$node->language]->nid : $reference['nid'],
            );
          }
        }
      }
      return $addition;
      break;
    case 'validate':
      $refs = _nodereference_potential_references($field);
      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['nid'])) {
            if (!isset($refs[$item['nid']])) {
              form_set_error($error_element, t("%name: this post can't be referenced.", array('%name' => t($field['widget']['label']))));
            }
          }
        }
      }
      return $items;
  }
}
?>