You are here

function entityreference_autofill_field_widget_form_alter in Entity reference autofill 7

Implements hook_field_widget_form_alter().

File

./entityreference_autofill.module, line 29
Entity reference autofill module.

Code

function entityreference_autofill_field_widget_form_alter(&$element, &$form_state, $context) {

  // Check that autofill is enabled and cardinality is 1 for field
  // (Module only supports 1 value fields).
  $autofill_is_enabled = !empty($context['instance']['settings']['behaviors']['autofill']['status']);
  $add_ajax_callback = $autofill_is_enabled && $context['field']['cardinality'] == 1;

  // Add AJAX callback to entity reference fields.
  if ($add_ajax_callback) {
    $widget_type = $context['instance']['widget']['type'];

    // Get path to where in the element to put the AJAX callback.
    $parents = _entityreference_autofill_supported_widgets($widget_type);
    if (is_array($parents)) {

      // Other modules can disable ajax for fields at their own peril.
      $is_allowed = module_invoke_all('entityreference_autofill_detach_ajax', $context['field']['field_name'], $element, $context);

      // Skip field if (strict) FALSE is returned by any module.
      if (in_array(FALSE, $is_allowed, TRUE)) {
        return;
      }

      // Add ajax callback to element.
      $ajax_parents =& entityreference_autofill_get_ajax_parents($parents, $element);
      foreach ($ajax_parents as &$ajax_parent) {
        $ajax_parent['#ajax'] = array(
          'callback' => 'entityreference_autofill_form_autofill',
        );
      }
    }
    return;
  }
}