You are here

answers.field_utils.inc in Answers 7.3

Same filename and directory in other branches
  1. 7.2 includes/answers.field_utils.inc

Field utility functions for the 'Answers' module.

File

includes/answers.field_utils.inc
View source
<?php

/**
 * @file
 * Field utility functions for the 'Answers' module.
 */

/**
 * Returns the value of a field for a node.
 *
 * @param object $node
 *   The node that contains the field.
 * @param string $field
 *   The field name.
 *
 * @return mixed
 *   The value of the field.
 */
function answers_field_get_value($node, $field) {
  $items = field_get_items('node', $node, $field);
  return $items[0]['value'];
}

/**
 * Returns a referenced node for a given field.
 *
 * @param object $source_node
 *   The node that contains the reference to another node.
 * @param string $field
 *   The reference field name.
 *
 * @return object
 *   A fully loaded node object.
 */
function answers_field_get_node_reference($source_node, $field) {
  $items = field_get_items('node', $source_node, $field);
  $target_nid = $items[0]['nid'];
  return node_load($target_nid);
}

/**
 * Implements helper for orphaned answers.
 *
 * @return array
 *   An array of answer nids.
 */
function _answers_orphan_nids() {
  $orphan_nids = array();
  $query = db_select('field_data_field_answer_question', 'q');
  $query
    ->leftJoin('node', 'n', 'q.field_answer_question_nid = n.nid');
  $query
    ->fields('q', array(
    'entity_id',
  ))
    ->fields('n', array(
    'nid',
  ))
    ->isNull('nid');
  $result = $query
    ->execute();
  foreach ($result as $record) {
    $orphan_nids[] = $record->entity_id;
  }
  $query = db_select('node', 'n');
  $query
    ->leftJoin('field_data_field_answer_question', 'q', 'n.nid = q.entity_id');
  $query
    ->fields('q', array(
    'entity_id',
  ))
    ->fields('n', array(
    'nid',
    'type',
  ))
    ->condition('type', 'answer')
    ->isNull('entity_id');
  $result = $query
    ->execute();
  foreach ($result as $record) {
    $orphan_nids[] = $record->nid;
  }
  return array_unique($orphan_nids);
}

Functions

Namesort descending Description
answers_field_get_node_reference Returns a referenced node for a given field.
answers_field_get_value Returns the value of a field for a node.
_answers_orphan_nids Implements helper for orphaned answers.