Drupal の Ajax API で wrapper エラーが出た場合の対処方法
エラー
Drupal の Ajax API を使った処理を実装した際に次のようなエラーが出ました。
ajax.js
Uncaught TypeError: wrapper[method] is not a function
原因と解決方法
エラーメッセージに書かれているとおり method
のパラメータが間違っています。
こちらは $form
配列に追加した #ajax
の method
キーの値が不正なことが原因です。
$form['my_field']['#ajax'] = array(
'callback' => 'mymodule_acme_callback',
'wrapper' => 'mymodule-acme-element-wrapper',
'method' => 'repace',
);
この場合は replace
が間違っているので次のとおりに修正すれば解決です。
'method' => 'replace',
method
の値として許容される値は jQuery 由来らしく doc コメントには次のように書かれています。
#ajax['method']: The jQuery method to use to place the new HTML. Defaults to 'replaceWith'. May be: 'replaceWith', 'append', 'prepend', 'before', 'after', or 'html'. See the jQuery manipulators documentation for more information on these methods.
コメントにはこの値を replace
ととしているものがあり実際にもこれで replaceWith
の挙動をするので jQuery のドキュメントに書かれているもの以外にもサポートされているものがあるようです。