DrupalでMENU_LOCAL_TASKタブメニューを変更する方法
質問
カスタムDrupal 6モジュールにタブ付きメニューを作成しました。モジュールページの上部にあるタブ付きメニューの右側にhtmlドロップダウンリストを配置します。リストは、変更時にajaxイベントを発生させます。 10,20,50,100の結果を指定して、SQLクエリのLIMIT句を変更します。テンプレートをハッキングせずにDrupalでこれを実現するにはどうすればよいですか?
ありがとう、
解決
theme_menu_local_tasks()
をオーバーライドすることでこれを行うことができますa>テーマ内:
function yourTheme_menu_local_tasks() {
// Prepare empty dropdown to allow for unconditional addition to output below
$dropdown = '';
// Check if the dropdown should be added to this menu
$inject_dropdown = TRUE; // TODO: Add checking logic according to your needs, e.g. by inspecting the path via arg()
// Injection wanted?
if ($inject_dropdown) {
// Yes, build the dropdown using Forms API
$select = array(
'#type' => 'select',
'#title' => t('Number of results:'),
'#options' => array('10', '20', '50', '100'),
);
// Wrap rendered select in <li> tag to fit within the rest of the tabs list
$dropdown = '<li>' . drupal_render($select) . '</li>';
}
// NOTE: The following is just a copy of the default theme_menu_local_tasks(),
// with the addition of the (possibly empty) $dropdown variable output
$output = '';
if ($primary = menu_primary_local_tasks()) {
$output .= "<ul class=\"tabs primary\">\n". $primary . $dropdown . "</ul>\n";
}
if ($secondary = menu_secondary_local_tasks()) {
$output .= "<ul class=\"tabs secondary\">\n". $secondary ."</ul>\n";
}
return $output;
}
(注:テストされていないコード-潜在的なタイプミス)
他のヒント
モジュールに入れるコードを参照している場合、モジュールは hook_theme_registry_alter()
。これにより、モジュールが関数 theme_menu_local_tasks()
をオーバーライドできます。モジュールは、以前のコールバックの値を保存する必要があります。これにより、ページが変更すべきものではない場合でも呼び出すことができます。
モジュールにフックを実装すると、モジュールが無効になった後、通常のメニュータブを使用できます。現在のテーマを変更するには、機能が必要になったときに元に戻す必要があります。別の人が作成したテーマを使用している場合は、新しいバージョンをダウンロードするたびにテーマを変更する必要があります。複数のテーマを使用している場合は、使用する各テーマに変更を加える必要があります。
一般に、モジュールに必要なテーマの変更は、モジュール内で行う必要があります。
所属していません StackOverflow