You are here

function entity_autocomplete_element_process in Entity Autocomplete 7

Form element processing handler for the #autocomplete_path property.

1 string reference to 'entity_autocomplete_element_process'
entity_autocomplete_element_info in ./entity_autocomplete.module
Implements hook_element_info().

File

./entity_autocomplete.module, line 162
Provides functionalities for entity fields autocompletion.

Code

function entity_autocomplete_element_process($element, $form_state) {
  if (user_access('use entity autocomplete')) {
    $parts = array(
      'entity-autocomplete',
      $element['#entity_type'],
    );
    if ($element['#bundles']) {
      array_splice($parts, 1, 0, 'bundle');
      $parts[] = implode('+', (array) $element['#bundles']);
    }
    $element['#autocomplete_path'] = implode('/', $parts);

    // Try to display the full entity label.
    if (!empty($element['#value']) && is_numeric($element['#value'])) {
      $element['#value'] = entity_autocomplete_element_label($element['#value'], $element['#entity_type']);
    }
  }

  // Function form_process_autocomplete() was added to core in v7.39.
  if (function_exists('form_process_autocomplete')) {
    $element = form_process_autocomplete($element);
  }

  // Keep compatible with AJAX.
  if (isset($element['#ajax']) && !isset($element['#ajax']['event'])) {

    // The ajax_pre_render_element() function does not recognize the
    // 'entity_autocomplete' element, so is does not add the expected event
    // that triggers the AJAX callback.
    $element['#ajax']['event'] = 'blur';
  }
  return $element;
}