require_once DRUPAL_ROOT . '/modules/recaptcha/recaptcha-php/src/autoload.php';
function module_helper_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
if ($form_id == 'user_login_form') {
require_once DRUPAL_ROOT . '/modules/recaptcha/recaptcha-php/src/autoload.php';;
}
}
The website encountered an unexpected error. Please try again later. Error: Class 'ReCaptcha\ReCaptcha' not found in _recaptcha_v3_verify_captcha_response() (line 264 of modules/contrib/recaptcha_v3/recaptcha_v3.module).
・・・・・・・・・・・・・・・・・ ・・・・・・・・・・・・・・・・・ REQUIREMENTS ------------ This module requires the following module: * Captcha (https://www.drupal.org/project/captcha) This module requires the following library: * google/recaptacha (https://github.com/google/recaptcha) RECOMMENDED MODULES ------------------- * reCAPTCHA (https://www.drupal.org/project/recaptcha): When enabled, reCAPTCHA v2 becomes available as fallback challenge. INSTALLATION ------------ * Install as you would normally install a contributed Drupal module. Visit https://www.drupal.org/documentation/install/modules-themes/modules-8 for further information. * If not using Composer, install the google/recaptacha (https://github.com/google/recaptcha) library.
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
今回、画面でポップアップでデータ入力、ajaxでデータを更新することを試みたいですが、jQuery、Bootstrapなどのmodalライブラリで実現ができます。但しDrupalサイトでポップアップを作るとしたら、やはりctoolsのModal機能を利用してみたいです(ctools(Chaos tool suite)はDrualの開発によく利用されているAPI)。
$items['my-module/%ctools_js/add'] = array( 'page callback' => 'my_module_callback', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, );
function my_module_callback($ajax) {
if ($ajax) {
// ctoolsのajax、modalに関するjsファイルをクライアントへ
ctools_include('ajax');
ctools_include('modal');
//フォーム作成に関する上方
$form_state = array(
'ajax' => TRUE,
'title' => t('New Group Creation'),
);
//ポップアップフォームの作成
//フォームからの更新データを$form_stateにセットする
$output = ctools_modal_form_wrapper('my_module_form', $form_state);
//新規ポップアップにajaxが有効にする
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
//ポップアップの出力
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('mymodule_form');
}
}
// ポップアップにある入力フォームの定義
function my_module_form($form, $form_state) {
$form = array();
$form['new_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Group Name'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
// データ更新
function my_module_form_submit(&$form, &$form_state) {
// データの更新
// ロジックは省略
// ポップアップを閉じるコマンド
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
・・・・・・ // ここで、ajax_commandの使用方法を省略
$commands[] = ajax_command_append("head", "<script type='text/javascript' src='/your/module/path/test.js"></script>" );
return array('#type' => 'ajax', '#commands' => $commands);
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implementation of hook_menu()
* 新規フォームを表示するページのメニュー設定
*/
function my_module_name_menu() {
$items['test/form'] = array(
'title' => 'Ajaxテスト',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_sample_form'),
'access arguments' => array('access content'),
);
}
/**
* フォームの各要素の記述
*/
function my_sample_form($form, &$form_state){
// メッセージ入力フィールド
$form['message'] = array(
'#type' => 'textfield',
'#title' => t('メッセージを入力'),
);
// 入力されたメッセージをajaxで表示する場所
$form['msg_wrapper'] = array(
'#type' => 'markup',
'#markup' => "",
'#prefix' => '<div id="msg-wrapper">',
'#suffix' => '</div>',
);
// サブミットボタン
$form['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#ajax' => array(
'callback' => 'test_sample_form_ajax_callback',
'wrapper' => 'msg-wrapper',
'effect' => 'fade',
),
);
return $form;
}
/**
* ajaxに呼び出される関数
*/
function test_sample_form_ajax_callback($form, $form_state) {
// 入力されたメッセージを指定した場所にセット
$form['msg_wrapper']['#markup'] = '入力されたメッセージ:' .
check_plain($form_state['values']['message']) ;
// 置換する部品を返す
return $form['msg_wrapper'] ;
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
/**
* Implements hook_init().
*/
function YOUR_MODULE_NAME_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
<a href="text/ajax/nojs/1234" class="use-ajax">ajaxリンク</a>
/**
* Implements hook_init().
*/
function test_module_init()
{
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system','drupal.ajax');
}
/**
* Implementation of hook_menu()
*/
function test_module_menu() {
$items['test/ajax/%/%']=array( // URLの2,3番目がクライアント側で生成
'page callback' => 'ajax_node_response',
'page arguments' => array(2,3), // URLの2,3番目の引数をコールバック関数に渡す
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function ajax_node_response($type="ajax", $nid=0){
$node = node_load( $nid, NULL, false );
$vnode = node_view($node);
$output = theme('node', $vnode);
if( $type="ajax" ){
$commands=array();
$commands[]=ajax_command_replace( '#block-system-main','<section id="block-system-main">'.$output.'</section>' ) ;
$page = array(
'#type' => 'ajax',
'#commands' => $commands,
);
ajax_deliver( $page ) ; // ajax対応ブラウザの結果
} else {
$output = '<div id="content">'.$output.'</div>'; // ajax非対応ブラウザの結果
return $output;
}
}
drupal_add_js("/misc/ajax.js") ; // ajaxのjsファイルをインポート
// 例: DrupalのAPIでフォームを作成
// 送信ボタンを押して、"test message"の文字列でフォームを置換します
$form['card_last']=array(
"#type" => "markup",
"#markup" => "",
"#prefix" => "<div id='card-last-result-div'>", // ajaxで置換したいエリア
"#suffix" => "</div>",
);
$form['card_last']['submit'] = array(
'#type' => 'submit',
'#value' => '送信',
'#attributes' => array( 'id'=>'test-form-submit' ), // ajaxのIDを指定します
"#ajax" => array(
"callback" => "_ajax_callback_form", // ajaxのコールバック
'wrapper' => 'card-last-result-div',
'method' => 'replace',
'effect' => 'fade',
),
);
// カスタムJSファイルをフォームと一緒に添付
$form['#attached']['js'] = array( "file_path/custom_ajax.js" );
(function($){
Drupal.behaviors.custom_module_name={
attach:function(cotext,settings){
$("#test-form-submit").once("test-form-submit",function(){ //指定したフォームのみ
var base = $(this).attr('id'); //フォームIDの使用
var element_settings = {
url: window.location.protocol + "//"+ window.location.hostname + settings.basePath + settings.pathPrefix + "system/ajax",
event: 'click',
progress: { type: 'throbber', },
};
// Drupalのajaxオブジェクトを生成します
Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
// 送信ボタンにListenerを追加
$("#test-form-submit").on( 'click',function(){
$(this).click(); // ajaxを作動させます
});
});
}
};
})(jQuery)
function _ajax_callback_card_last_form($form, &$form_state){
$commands[] = ajax_command_replace('#card-last-result-div', "test message");
return array('#type' => 'ajax', '#commands' => $commands);
}