drupal 7を使用していて、非常に基本的な問題で立ち往生しています。既知のノードIDに編集ページを表示したいだけです。同様の投稿があることは知っていますが、どちらもしなかった動作するか、テーマが理解できませんでした。モジュールは次のようになります。
<?php
/*
* Show the edid page to a specific nid.
*/
function control_center_menu() {
$items = array();
module_load_include('inc', 'node', 'node.pages');
$nid = '4';
$node = node_load($nid);
$items['test/test'] = array(
'title' => 'Edit this node',
'description' => 'Edit this node.',
'page callback' => 'drupal_get_form',
'page arguments' => array('page_node_form', $node),
'access callback' => TRUE,
);
return $items;
}
しかし、テスト/テストで私はたくさんのエラーを受け取ります:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'node_form' not found or invalid function name in drupal_retrieve_form() (line 795 of /home/christian/workspace/gigs/includes/form.inc).
Notice: Undefined index: #node in menu_form_node_form_alter() (line 629 of /home/christian/workspace/gigs/modules/menu/menu.module).
Notice: Trying to get property of non-object in menu_form_node_form_alter() (line 629 of /home/christian/workspace/gigs/modules/menu/menu.module).
Notice: Undefined index: #node in menu_form_node_form_alter() (line 630 of /home/christian/workspace/gigs/modules/menu/menu.module).
Notice: Trying to get property of non-object in menu_form_node_form_alter() (line 630 of /home/christian/workspace/gigs/modules/menu/menu.module).
そして、ホールフォームの代わりに、メニュー設定のチェックボックスとURLパス設定のみを取得します。
何が悪いのですか?私は過去のnoobフレンドリーなコピーを見つけるために一生懸命努力しましたが、何も見つかりませんでした...事前にどうもありがとうございました!
hook_menu()
は、キャッシュが再構築されるときにのみ評価されるため、パスが実際にアクセスされたときにmodule_load_include()
への呼び出しは呼び出されません。
このフックは、メニュー項目を宣言することのみを目的としています。パスに追加する必要のあるロジックがある場合は、次のようにアクセスコールバック/ページコールバックで発生する必要があります。
_function control_center_menu() {
$items = array();
$items['test/test'] = array(
'title' => 'Edit this node',
'description' => 'Edit this node.',
'page callback' => 'control_center_test_callback',
'page arguments' => array('page_node_form', $node),
'access callback' => TRUE,
);
return $items;
}
function control_center_test_callback() {
module_load_include('inc', 'node', 'node.pages');
$nid = '4';
$node = node_load($nid);
return drupal_get_form('page_node_form', $node);
}
_
ただし、静的ノード編集ページに転送しているので、ページコールバックをdrupal_goto()
に設定してURLをハードコードする方が簡単な場合があります。
_function control_center_menu() {
$items = array();
$items['test/test'] = array(
'title' => 'Edit this node',
'description' => 'Edit this node.',
'page callback' => 'drupal_goto',
'page arguments' => array('node/4/edit'),
'access callback' => TRUE,
);
return $items;
}
_
次のコードのように、メニューコールバックを定義する必要があります。
_ $items['test/test'] = array(
'title' => 'Edit this node',
'description' => 'Edit this node.',
'page callback' => 'drupal_get_form',
'page arguments' => array('page_node_form', $node),
'file path' => drupal_get_path('module', 'node'),
'file' => 'node.pages.inc',
'access callback' => TRUE,
);
_
hook_menu()
の実装は、新しいページがDrupalに要求されるたびではなく、メニューが再構築されるときにのみ呼び出されることに注意してください。この結果、モジュールがインストールされている場合、またはメニューが再構築されている場合にのみ、フォームは更新されたノードオブジェクトを取得します。他の場合では、フォームは静的ノードオブジェクトのコピーを取得しますが、実際のノードオブジェクトを反映していません。
Test/testをnode/4/editのパスエイリアスにすることをお勧めします。このようにして、フォームは毎回更新されたノードオブジェクトを取得します。
私は次のようにコードを使用しました:
function mymodule_menu() {
$items = array();
$items['user/%user/firm'] = array(
'title' => 'Firm',
'page callback' => 'firm_callback',
'page arguments' => array(1),
'access callback' => 'user_check',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function firm_callback($userobj) {
....
if ( isset($node) ) { // existed node
$form_state['build_info']['args'] = array($node);
form_load_include($form_state, 'inc', 'node', 'node.pages');
$form = drupal_build_form('firm_node_form', $form_state);
return $form;
} else { // new node
module_load_include('inc', 'node', 'node.pages');
$form = node_add('firm');
return drupal_render($form);
}
}
function mymodule_form_firm_node_form_alter(&$form, &$form_state, $form_id) {
form_load_include($form_state, 'inc', 'node', 'node.pages');
}