I'm trying to add some custom autocomplete functionality to a couple text fields on my user registration page, along the lines of this tutorial; http://drupal.org/node/854216.
I've been able to successfully accomplish that, but now whenever I submit the registration form I get a blank page, and this error shows up in the log;
PHP Fatal error: Cannot create references to/from string offsets nor overloaded objects in /var/www/html/drupal/includes/common.inc on line 6448, referer: http://[...]/drupal/?q=user/register
I can't find the topic now, but when I was initially googling this problem I read somewhere that this issue is typically that the '#' symbol has been added or missed from a property key. Because without a # symbol it treats the value of that property as a child, and thus an array. Or something along those lines, but after double checking it seems like all the properties I'm using should be as I've put them.
Here is the code, does anyone know what I'm doing wrong?
function gtx_alterations_menu() {
$items = array();
$items['city/autocomplete'] = array(
'page callback' => 'city_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
function gtx_alterations_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_register_form') {
$form['field_city_of_residence']['#type'] = 'textfield';
$form['field_city_of_residence']['#title'] = t('City of Residence');
$form['field_city_of_residence']['#autocomplete_path'] = 'city/autocomplete';
$form['field_headquarters_location']['#type'] = 'textfield';
$form['field_headquarters_location']['#title'] = t('Headquarters Location');
$form['field_headquarters_location']['#autocomplete_path'] = 'city/autocomplete';
}
}
function city_autocomplete($string = '') {
$cities = array();
$locations = array();
$results = file_get_contents('http://graph.facebook.com/search?q='.urlencode($string).'&type=adcity');
$results = preg_replace('/\\\\u0*([0-9a-fA-F]{1,5})/', '&#x\1;', $results);
preg_match_all('/"name":"[^,]+/', $results, $cities);
preg_match_all('/"subtext":".+?,[^"]+/', $results, $locations);
$final = array();
foreach ($cities[0] as $key => $value) {
$value = substr($value, 8);
$subtext = substr($locations[0][$key], 11);
$result = $value . ', ' . $subtext;
$final[$result] = $result;
}
drupal_json_output($final);
}
Some things I tried
When trying to narrow down the problem I determined that commenting out these two lines;
$form['field_city_of_residence']['#autocomplete_path'] = 'city/autocomplete';
$form['field_headquarters_location']['#autocomplete_path'] = 'city/autocomplete';
removes the error, however that naturally means the autocomplete is also disabled.
Additionally replacing the contents of city_autocomplete($string) with
drupal_json_output(array('test' => 'test'));
does not resolve the error, meaning that the problem isn't something in the that function.
Removing the # symbol from these two lines
$form['field_city_of_residence']['#autocomplete_path'] = 'city/autocomplete';
$form['field_headquarters_location']['#autocomplete_path'] = 'city/autocomplete';
causes the previous error to be replaced with this one,
Unsupported operand types in /var/www/html/drupal/includes/form.inc on line 1755
Field forms are bit confusing in Drupal 7, have a look at Why is hook_form_alter so messy in d7? for a bit of a breakdown.
Suffice to say the actual HTML elements (the ones you want to change the values for) are found deeper in the array, e.g.
$form['field_city_of_residence'][LANGUAGE_NONE][0]['value']['#type'] = 'textfield';
$form['field_city_of_residence'][LANGUAGE_NONE][0]['value']['#title'] = t('City of Residence');
$form['field_city_of_residence'][LANGUAGE_NONE][0]['value']['#autocomplete_path'] = 'city/autocomplete';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With