0

I'm in Drupal 7. I would like to execute an ajax function in my front-page. I've defined the block view in my ".module" file. When I change the selected option, the log gets two errors:

1.

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'test_services_search_form' not found or invalid function name in drupal_retrieve_form() (line 842 of /var/www/html/test/html/includes/form.inc).

2.

Notice: Undefined index: test_services_search_form in drupal_retrieve_form() (line 807 of /var/www/html/test/html/includes/form.inc).

test.module

function test_services_block_info() {
  $blocks['test_services_search_form'] = [
    'info' => t('Plan your trip'),
  ];
  return $blocks;
}

function test_services_block_view($delta = '') {
  $block = [];
  switch ($delta) {
    case 'test_services_search_form':
      module_load_include('inc', 'test_services', 'includes/test');
      $form             = drupal_get_form('test_services_search_form');
      $block['content'] = render($form);
      break;
  }
  return $block;
}

test.inc

<?php

/**
 * @file
 * TEST - File search inc.
 */

/**
 * Implements hook_form().
 */
function test_services_search_form($form, &$form_state) {
  $form['search'] = [
    '#type' => 'container',
  ];

  // Transfer.
  $form['search']['transfer'] = [
    '#ajax'       => [
      'callback' => 'test_services_search_transfer_callback',
    ],
    '#attributes' => [
      'class'    => ['js-select2', 'cancel'],
      'required' => TRUE,
    ],
    '#title'      => t('Transfer'),
    '#options'    => [
      0 => t('Direct trip'),
      1 => t('Stopover trip'),
    ],
    '#type'       => 'select',
  ];

  return $form;
}

/**
 * Callback handler for the direct/transfer trip select.
 */
function test_services_search_transfer_callback($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
}

/**
 * Callback function.
 */
function test_services_search_form_callback($form, &$form_state) {
  return $form;
}

The point is disable three fields if option two is selected, It's possible execute AJAX into a block view, and if so, what is going on? Why Drupal is not going into the function of the AJAX callback?

7
  • The code from test.inc needs to be in test.module. This will fix error 1 and may also fix error 2 Nov 2, 2018 at 9:38
  • I'm gonna try it, but i'm calling test.inc with "module_load_include" in my block view hook, it should works anyways.
    – Javier Rey
    Nov 2, 2018 at 9:41
  • Ok. You are right, i don't understand how, but it works. So I can not split the form code in other file?
    – Javier Rey
    Nov 2, 2018 at 9:44
  • Yes it possible, but your module_load_include call is incorrect, I believe from you oiriginally posted it should be module_load_include('inc', 'test', 'test'), take a look at api.drupal.org/module_load_include Nov 2, 2018 at 10:02
  • Are you sure? $type (inc) | $module_name (test_services) | $path_to_file (includes/test). I mean, that's the way i was trying to execute ajax, but only works if the form is in the ".module" idk why.
    – Javier Rey
    Nov 2, 2018 at 10:12

1 Answer 1

0

I've defined the block view. When I change the selected option, the log gets two errors.

It seems the error happens after you submitting the block. So the file loads fine on view, but not when the form is processed which should have information where to look for it.

So you should define your form using hook_forms.

By default, when drupal_get_form() is called, the system will look for a function with the same name as the form ID, and use that function to build the form. If no such function is found, Drupal calls this hook.

Also, when you working with the forms, instead of drupal_get_form($form_id), you can consider using form_load_include() to make sure the include file is loaded whenever the form is processed.

Use this function instead of module_load_include() from inside a form constructor or any form processing logic as it ensures that the include file is loaded whenever the form is processed. In contrast to using module_load_include() directly, form_load_include() makes sure the include file is correctly loaded also if the form is cached.

Your Answer

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

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