5

I'm trying to implement an auto complete form with a little twist. I want its auto complete options to be limited by another field (text field to be more specific).

I understand how AJAX API works and should able to create an auto complete filter for a field based on it's own values but I have no idea how to create a an a auto complete filter based on another field in the form.

From what I have found only this question is similar to my problem, it is unfortunately unanswered. I don't want the code, just hint where to look would be useful.

BTW: I have came up with a solution but it's unbelievably dirty hack that requires going into database twice. I could implement AJAX callback for the field I want the filter to be based on, store the value of the field in the callback to custom database table and then in the callback of the field I want to filter get the value and make it work.

BTW2: The field I want to base the filter on will be text field, the other one custom entityreference.

BTW3: I'm almost sure it is possible somehow since the entityreference option limit module does something like that. (It does it with a help of one other field which I don't want (And I didn't understand it at all even though I tried anyway :) ))

2

Great job on the research ... here's a solution I came up with. The Drupal Form API and Ajax API should be able to do this but I stopped after I got abit confused too some time ago -- I wrote a fallback using jQuery (which comes bundled with Drupal).

In a custom module .info file I add my javascript:

scripts[] = eap_student_feedback2_ajax.js

You could add this via a form_alter() call instead and using #attached FAPI key.

Then in my javascript file I have dependant dropdowns. You have to select a Country before you select a Program available in that country. When you select a Country, those countries Program list is fetched via Ajax and inserted into that HTML select list -- the widget is then made available. The jQuery code is as follows:

jQuery(function($) {

  $('#edit-field-survey-program-und option:gt(0)').remove();
  $('#edit-field-survey-program-und').attr('disabled', 'disabled');

  $("#edit-field-survey-country-und").click(function() {
    var selected_country = $("#edit-field-survey-country-und").val();
    if (selected_country != '_none') {
      $.ajax({
        type:'GET',
        url: '/eap-ajax/eap-country-programs/' + selected_country,
        data: '',
          success:function(data){
          //console.log("got: ");
          //console.log(data);
          // Do actual work.
          $('#edit-field-survey-program-und option:gt(0)').remove();
          $.each(data, function(key, value) {
            $('#edit-field-survey-program-und').append(
              '<option value="' + key + '">' + value + '</option>'
            );
          });
          $('#edit-field-survey-program-und').removeAttr('disabled');
        }
      }); 
    }
  });
});

In my module file I implement hook_menu to supply the ajax endpoint:

// Make a frigging ajax callback.
function eap_student_feedback2_menu() {
  $items['eap-ajax/eap-country-programs/%'] = array(
    'page callback' => '_eap_student_feedback2_programs_json_endpoint',
    'page arguments' => array(2),
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
  );
  return $items;
}

And also implement the logic to return some JSON for jQuery:

function _eap_student_feedback2_programs_json_endpoint($country_term_id) {
  $matches = array();
  $result = db_query('
      SELECT tth.tid, ttd.name AS term_name
      FROM taxonomy_term_hierarchy tth
      LEFT JOIN taxonomy_term_data ttd ON tth.tid = ttd.tid
      WHERE tth.parent = :tid
      ORDER BY 2 ASC
  ', array(':tid' => $country_term_id));
  // add matches to $matches
  foreach ($result as $row) {
    $matches[$row->tid] = check_plain($row->term_name);
  }
  // return for JS
  return drupal_json_output($matches);
}
2

I know it's a bit amateur but the question has already been answered I just didn't find it.

It's here:

form auto complete how to restrict results by previous drop down

and here:

Using ajax to filter drupal autocomplete form

BTW: I would delete the question but since I didn't find the answered questions myself I thought that this one might be more discoverable.

//I can't accept my own answer now, sory for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.