0

I'm using the following code to embed an autocomplete field in a block. However the autocomplete process does not execute when I type in input:

function cse_block_info(){
    $blocks = array();
    $blocks['cse_search'] = array(
        'info' => t('CSE Search'),
    );

    return $blocks;
}

function cse_block_view($delta=''){
    switch ($delta) {
        case 'cse_search':
            // The subject is displayed at the top of the block. Note that it
              // should be passed through t() for translation. The title configured
              // for the block using Drupal UI supercedes this one.
              $block['subject'] = t('Title CSE block');
              // The content of the block is typically generated by calling a custom
              // function.
              $block['content'] = cse_block_content($delta);
            return $block;

        break;

        default:
        break;
    }

}

function cse_block_content($which_block){
    switch ($which_block) {
        case 'cse_search':
            $form['cse_search'] = array(
                '#title' => t('CSE Autocomplete TEST!'),
                '#type' => 'textfield',
                '#autocomplete_path' => 'cse/google/autocomplete',
            );

            // Adds a simple submit button that refreshes the form and clears its
            // contents. This is the default behavior for forms.
            $form['submit'] = array(
                '#type' => 'submit',
                '#value' => 'Save',
            );

            return $form;
        break;

        default:
        break;
    }
}

I implement the same code as a stand alone system i:e not placed inside a block and the process works flawlessly.

Can anyone help with suggestions of why my attempt to place an autocomplete in a block does not work?

2
  • Have you ever give a chance to jqueryUI autocomplete solution? I could solve this on my current project using it. Commented Aug 15, 2017 at 14:44
  • That's a good suggestion. I will investigate.
    – sisko
    Commented Aug 15, 2017 at 18:38

1 Answer 1

0

Your block content should look like this:

function cse_block_content($which_block){
  switch ($which_block) {
    case 'cse_search':
      drupal_render(drupal_get_form('my_custom_form'));
    break;

    default:
    break;
  }
}

function my_custom_form($form, &$form_state) {
  $form['cse_search'] = array(
      '#title' => t('CSE Autocomplete TEST!'),
      '#type' => 'textfield',
      '#autocomplete_path' => 'cse/google/autocomplete',
  );

  // Adds a simple submit button that refreshes the form and clears its
  // contents. This is the default behavior for forms.
  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Save',
  );

  return $form;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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