Drupal 7 theming a node form

我慢慢發現 content type node form 是繼 exposed views form 之後最常要修改的 form
(我明明有寫過 alter exposed views form, 但找不到...)

特定 content type 本身是沒有 theme function 的
先定義一個,順便定義使用一個 tpl 檔輸出

<?php
/**
 * Implements hook_theme().
 */
function MYMODULE_theme($existing, $type, $theme, $path) {

  return array(
   
'article_node_form' => array(
     
'render element' => 'form',
     
'template' => 'article-node-form',
     
// this will set to module/theme path by default:
     
'path' => drupal_get_path('module', 'MYMODULE'),
    ),
  );
}
?>

接著便可以使用 preprocess 加入其他需要的 variables

<?php
/**
 * Preprocessor for theme('article_node_form').
 */
function template_preprocess_article_node_form(&$variables) {

 
// nodeformcols is an alternative for this solution.
 
if (!module_exists('nodeformcols')) {

   
$variables['sidebar'] = array();   // Put taxonomy fields in sidebar.

   
$variables['sidebar'][] = $variables['form']['field_tags'];
   
hide($variables['form']['field_tags']);

   
// Extract the form buttons, and put them in independent variable.
   
$variables['buttons'] = $variables['form']['actions'];
   
hide($variables['form']['actions']);
  }
}
?>

模組資料夾內的 article.tpl.php: (當然可以修改成你需要的樣子)

<?php echo drupal_render_children($form)?>

參考 http://drupal.org/node/1092122

Google