0

I'm writing because I have spent the last few days investigating what seems to be a real conundrum and I'm pretty much at my wit's end.

I have a very simple Drupal 8 form_alter which creates a set of two dropdowns during user registration. user picks an option from the first one, and then the second is populated based on that first choice. both dropdowns' options are pulled from an external database.

problem is: the ajax callback runs smoothly on my webhost, but not on the client's. instead, I get the following error on Firebug:

AjaxError: 
An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /user/register?ajax_form=1
StatusText: OK
ResponseText: 

Drupal.AjaxError.prototype = new Error();
-----------------------------^

and here's the code:

databases configuration

$databases['default']['default'] = array (
  'database' => 'mydatabase',
  'username' => 'mydatabase_username',
  'password' => 'mydatabase_password',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);
$databases['external']['default'] = array (
  'database' => 'myexternaldatabase',
  'username' => 'myexternaldatabase_username',
  'password' => 'myexternaldatabase_password',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

mymodule.info.yml

name: My Module
type: module
description: 'User registration form customisation'
package: mypackage
core: 8.x

mymodule.module

<?php

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if ($form_id == 'user_register_form') {
    // Switch to external database
    \Drupal\Core\Database\Database::setActiveConnection('external');

    $db = \Drupal\Core\Database\Database::getConnection();

    $query = $db->select('table1', 't1');
    $query->fields('t1', array('field1', 'field2'));
    $query->orderBy('field1');
    $table1 = $query->execute()->fetchAllKeyed();

    // Switch back to default database
    \Drupal\Core\Database\Database::setActiveConnection();

    $options = array(
      '0' => t('Choose a Value'),
    );

    $options = $options + $table1;

    $form['field_table1'] = array(
      '#type' => 'select',
      '#title' => t('Select from Table 1'),
      '#options' => $options,
      '#ajax' => array(
        'callback' => '_mymodule_dropdown_callback',
        'wrapper' => 'mymodule-dropdown-2',
        'event' => 'change',
        'progress' => array(
          'effect' => 'fade',
          'message' => t('Loading...'),
        ),
      ),
    );

    $form['field_table2'] = array(
      '#type' => 'select',
      '#title' => t('Select from Table 2'),
      '#prefix' => '<div id="mymodule-dropdown-2">',
      '#suffix' => '</div>',
      '#options' => array(0 => 'Choose from Table 1 first'),
      '#default_value' => '0',
    );

  }
}

function _mymodule_dropdown_callback(array $form, \Drupal\Core\Form\FormStateInterface &$form_state) {

  $values = $form_state->getValues();
  $field1 = $values['field_table1'];

  // Switch to external database
  \Drupal\Core\Database\Database::setActiveConnection('external');

  $db = \Drupal\Core\Database\Database::getConnection();

  $query = $db->select('table2', 't2');
  $query->fields(
    't2',
    array(
      'field1',
      'field2',
      'field3',
    )
  );
  $query->condition('field1', $field1);
  $query->orderBy('field2');
  $table2 = $query->execute()->fetchAllKeyed(1, 2);

  // Switch back to default database
  \Drupal\Core\Database\Database::setActiveConnection();

  unset($form['field_table2']);

  $form['field_table2'] = array(
    '#type' => 'select',
    '#title' => t('Select from Table 2'),
    '#prefix' => '<div id="mymodule-dropdown-2">',
    '#suffix' => '</div>',
    '#default_value' => '0',
  );

  $default = array('0' => t('Choose a Value'));

  $form['field_table2']['#options'] = $default + $table2;
  return $form['field_table2'];

}

from what I could gather, the problem is a difference in server configuration between my webhost and the client's, but I have no idea what to look for. I scoured the web but came up empty.

if anyone would be willing to point me to the right direction, or if you need more clarification on the servers' configurations, please let me know (I felt it would be overkill to simply dump a lot of server configuration here).

any help is greatly appreciated. thank you in advance.

kind regards, Luciano

0

well, as it turns out, there was a problem with AJAX in Core prior to version 8.2. here's an example from the (awesome) support at Drupalize.me:

https://www.drupal.org/node/2619878

as for the reason why it was working on my server configuration, it's (probably) because they have upgraded PHP in a rather odd fashion (applying patches for example) and that caused some functionality to "work" where it wasn't, er, supposed to.

it also helped that I looked at the error logs and meddled with the code in core, but you know -- you're not supposed to hack core.

anyway it seems like this issue has been fixed as of Drupal 8.2, so I'm closing this ticket for now.

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.