function tripal_ajax_attach_field

3.x tripal.entity.inc tripal_ajax_attach_field($id)

Responds to an AJAX call for populating a field.

Parameters

$id: The ID of the HTML div box where the field is housed. The ID contains the entity ID and field name.

1 string reference to 'tripal_ajax_attach_field'
tripal_menu in tripal/tripal.module
Implements hook_menu(). Defines all menu items needed by Tripal Core

File

tripal/includes/tripal.entity.inc, line 524

Code

function tripal_ajax_attach_field($id) {

  $matches = array();
  if (preg_match('/^tripal-entity-(\d+)--(.+)$/', $id, $matches)) {
    $entity_id = $matches[1];
    $field_name = $matches[2];
    $field = field_info_field($field_name);
    $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, array($field['id']));
    reset($result);
    $entity = $result[$entity_id];

    // Get the element render array for this field and turn off the label
    // display. It's already on the page.  We need to get the display from the
    // instance and pass it into the field_view_field. Otherwise it uses the
    // instance default display settings. Not sure why it does this. It needs
    // more investigation.
    $instance = field_info_instance('TripalEntity', $field_name, $entity->bundle);
    $element = field_view_field('TripalEntity', $entity, $field_name, $instance['display']['default']);
    $element['#label_display'] = 'hidden';

    $content = drupal_render($element);
    return drupal_json_output(array(
      'id' => $id,
      'content' => $content
    ));
  }
}