このチュートリアルに沿って、ユーザー登録ページのいくつかのテキストフィールドにカスタムオートコンプリート機能を追加しようとしています。http://drupal.org/node/854216。
私はそれをうまく達成することができましたが、今では登録フォームを送信するたびに空白のページが表示され、このエラーがログに表示されます。
PHPの致命的なエラー:6448行目の/var/www/html/drupal/includes/common.incに文字列オフセットまたはオーバーロードされたオブジェクトへの/からの参照を作成できません。リファラー:http:// [...] / drupal /?q =ユーザー/登録
現在トピックを見つけることができませんが、最初にこの問題をグーグルで調べていたときに、この問題は通常、プロパティキーに「#」記号が追加されているか欠落していることであるとどこかで読みました。#記号がないと、そのプロパティの値が子として扱われ、したがって配列として扱われるためです。またはそれらの線に沿った何か、しかし再確認した後、私が使用しているすべてのプロパティは私がそれらを置いた通りでなければならないようです。
ここにコードがあります、誰かが私が間違っていることを知っていますか?
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);
}
私が試したいくつかのこと
問題を絞り込もうとしたとき、私はこれらの2行をコメントアウトすることにしました。
$form['field_city_of_residence']['#autocomplete_path'] = 'city/autocomplete';
$form['field_headquarters_location']['#autocomplete_path'] = 'city/autocomplete';
エラーを削除しますが、これは当然、オートコンプリートも無効になっていることを意味します。
さらに、city_autocomplete($ string)の内容を次のように置き換えます
drupal_json_output(array('test' => 'test'));
エラーを解決しません。つまり、問題はその関数にあるものではありません。
これらの2行から#記号を削除する
$form['field_city_of_residence']['#autocomplete_path'] = 'city/autocomplete';
$form['field_headquarters_location']['#autocomplete_path'] = 'city/autocomplete';
以前のエラーがこのエラーに置き換えられます。
Unsupported operand types in /var/www/html/drupal/includes/form.inc on line 1755