You are here

function _sheetnode_ajax_field in Sheetnode 7.2

Same name and namespace in other branches
  1. 5 sheetnode.module \_sheetnode_ajax_field()
  2. 6 sheetnode.module \_sheetnode_ajax_field()
  3. 7 sheetnode.module \_sheetnode_ajax_field()

AJAX function to return a field value.

1 string reference to '_sheetnode_ajax_field'
sheetnode_menu in ./sheetnode.module
Implements hook_menu().

File

./sheetnode.module, line 1106
Module file for the sheetnode module.

Code

function _sheetnode_ajax_field($oid = NULL, $entity_type = NULL, $field = NULL) {
  if (!$oid) {
    $oid = $_REQUEST['oid'];
  }
  if (!$entity_type) {
    $entity_type = $_REQUEST['entity_type'];
  }
  if (!$field) {
    $field = $_REQUEST['field'];
  }
  $value = NULL;
  $entity = _sheetnode_entity_load($entity_type, $oid);
  if ($entity) {

    // Try field.
    $field_info = field_info_instance($entity_type, $field, field_extract_bundle($entity_type, $entity));
    if ($field_info) {
      if (!field_access('view', $field_info, $entity_type, $entity)) {
        drupal_json_output(array(
          'value' => '',
          'type' => 'e#NAME?',
        ));
        drupal_exit();
      }
      $value = field_view_field($entity_type, $entity, $field, array(
        'label' => 'hidden',
      ));
      if ($value) {
        $value = drupal_render($value);
      }
    }

    // Try rendered field with raw build functions.
    $build_func = $entity_type . '_build_content';
    if (!$value && function_exists($build_func)) {
      $build_func($entity);
      if (isset($entity->content[$field])) {
        $value = drupal_render($entity->content[$field]);
      }
    }

    // Try raw object field.
    if (!$value && isset($entity->{$field})) {
      if (is_object($entity->{$field}) || is_array($entity->{$field})) {
        $value = print_r($entity->{$field}, TRUE);
      }
      else {
        $value = $entity->{$field};
      }
    }

    // If found, send it back.
    if ($value) {
      drupal_json_output(array(
        'type' => is_numeric($value) ? 'n' : 'th',
        'value' => $value,
      ));
      drupal_exit();
    }
  }
  drupal_json_output(array(
    'value' => '',
    'type' => 'e#NAME?',
  ));
  drupal_exit();
}