You are here

function ref_field_field_widget_form in (Entity)Reference Field Synchronization 7

Implements hook_field_widget_form().

File

./ref_field.module, line 457

Code

function ref_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Get the ID
  $eid = isset($items[$delta]['eid']) ? $items[$delta]['eid'] : '';
  $entity_type = $field['settings']['entity'];

  // Load the entity
  $default_value = '';
  if ($eid) {
    $entity = entity_load($entity_type, array(
      $eid,
    ));

    // Format default value
    $default_value = $entity ? ref_field_encode($entity_type, $entity[$eid]) : '';
  }

  // Set a token with the timestamp with the time the user accesed the widget
  // This is used to check access to the autocomplete path
  $_SESSION['ref_field'][$instance['id']] = time();
  $element += array(
    '#type' => 'textfield',
    '#default_value' => $default_value,
    '#autocomplete_path' => 'ref_field/autocomplete/' . $instance['id'] . '/' . $instance['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'],
  );

  // Autocomplete is only compatible with entities that can have labels
  // @see hook_entity_info().
  // @see entity_label().
  $entity_info = entity_get_info($entity_type);
  if ($entity_type != 'user' && !isset($entity_info['entity keys']['label'])) {
    drupal_set_message(t('%instance: Autocomplete is not compatible with %entity_type entities, please contact your administrator.', array(
      '%instance' => $instance['label'],
      '%entity_type' => $entity_info['label'],
    )), 'error');
  }
  $element['#element_validate'][] = 'ref_field_field_widget_validate';
  return array(
    'eid' => $element,
  );
}