2010-05-10 72 views
3

我创建了我自己的模块,名为“cssswitch”。我试图创建自己的HTML布局来显示模块的管理员表单部分。我明白如何使用hook_form_alter()来修改表单元素,但我需要创建整个表单布局来使一些字段彼此相邻。 看来我需要像使用theme_cssswitch_display()显示节点的前端视图一样,但是需要节点的管理部分。从模块的Drupal表格布局

function cssswitch_form_alter(&$form, $form_state, $form_id) { 

    switch($form_id) { 

     case 'cssswitch_node_form': 
      $form['hour_start']['#prefix'] = '<div class="start-box">'; 
      $form['ampm_start']['#suffix'] = '</div>'; 
      $form['ampm_start']['#attributes'] = array("style" => "border-width:2px;"); 
      break; 
    } 

} 

function cssswitch_theme() { 

    return array(
     'cssswitch_display' => array(
     'arguments' => array('node'), 
    ), 

    ); 

} 

// to display the view of the node 
function theme_cssswitch_display($node) { 

    $output = '<div class="cssswitch-cssfilename">Filename: '. 
    check_plain($node->cssfilename). '</div>'; 
    $output .= '<div class="cssswitch-time_start">Start: '. 
    check_plain($node->hour_start). ':'.check_plain($node->minute_start).' '.check_plain($node->ampm_start).'</div>'; 

    $output .= '<div class="cssswitch-time_end">End: '. 
    check_plain($node->hour_end). ':'.check_plain($node->minute_end).' '.check_plain($node->ampm_end).'</div>';  

    return $output; 
} 

感谢您的帮助

回答

2

我把它全部整理出来了。 我hook_theme是这样的:

function cssswitch_theme() { 

    return array(
     'cssswitch_display' => array(
     'arguments' => array('node'), 
    ), 

     'cssswitch_node_form' => array(
     'arguments' => array('form' => NULL), 
     'template' => 'cssswitch-node-form.tpl.php', 
    ), 

    ); 

} 

我创建的文件主题/花环/ cssswitch节点,form.tpl.php 然后我可以有超过往哪里放任何形式的元素这样的控制:

<style> 
#edit-hour-start-wrapper, #edit-minute-start-wrapper, #edit-ampm-start-wrapper, #edit-hour-end-wrapper, #edit-minute-end-wrapper, #edit-ampm-end-wrapper { 
    float:left; 
    margin-right:25px; 
} 
</style> 
<div id="regform1"> 

<?php print drupal_render($form['title']); ?> 
<?php print drupal_render($form['cssfilename']); ?> 

    <fieldset class="group-account-basics collapsible"> 
     <legend>Time Start</legend> 
     <?php print drupal_render($form['hour_start']); ?> 
     <?php print drupal_render($form['minute_start']); ?> 
     <?php print drupal_render($form['ampm_start']); ?> 
    </fieldset> 

    <fieldset class="group-account-basics collapsible"><legend>Time end</legend> 
     <?php print drupal_render($form['hour_end']); ?> 
     <?php print drupal_render($form['minute_end']); ?> 
     <?php print drupal_render($form['ampm_end']); ?>  
    </fieldset> 
</div> 

<?php print drupal_render($form['options']); ?> 
<?php print drupal_render($form['author']); ?> 
<?php print drupal_render($form['revision_information']); ?> 
<?php print drupal_render($form['path']); ?> 
<?php print drupal_render($form['attachments']); ?> 
<?php print drupal_render($form['comment_settings']); ?> 
<?php print drupal_render($form); ?> 
1

hook_form_alter时才需要,如果你想改变另一模块定义了一个形式。既然你正在定义表单,你不需要改变它,而是你可以像你想要的那样定义它。

你需要使用什么来改变表单的标记,是表单本身和/或它的元素的#theme属性。有了它,您可以使用自己的主题功能来呈现表单和元素。

Drupal's FAPI reference寻找帮助。

+0

谢谢你指点我在正确的方向。我觉得我现在差不多已经有了,但是我的表单并没有以模块中的函数为主题。我在表单函数中添加了主题函数名称,如下所示:$ form ['#theme'] ='cssswitch_form'; 功能cssswitch_theme(){ \t返回阵列( \t 'cssswitch_display'=>数组( \t '参数'=>阵列( '节点'), \t) \t 'cssswitch_form'=>数组( \t 'arguments'=> array(), \t),\t \t); } function theme_cssswitch_form($ form){ $ output =' testing 123'; $ output。= drupal_render($ form); return $ output; } – EricP 2010-05-11 14:29:39

+0

@EricP记得清除缓存,这是主题功能最常见的问题。 – googletorp 2010-05-11 15:12:06

0

我发现了我现在最需要的东西。我不得不使用“建议”的表单ID,Themer Info给我的是“cssswitch_node_form”,我在表单中使用了名为“cssswitch_form()”的函数,如下所示: $ form ['#theme'] =' cssswitch_node_form“;

我还需要在此函数中使用该名称:

function cssswitch_theme() { 

    return array(
     'cssswitch_display' => array(
     'arguments' => array('node'), 
    ), 

     'cssswitch_node_form' => array(
     'arguments' => array('form' => NULL), 
    ),  


    ); 

这里是“theme_”前面加上它的同名但功能:

function theme_cssswitch_node_form($form) { 

    $output=''; 
    $output.='<span>testing 123</span>'; 
    $output.=drupal_render($form['hour_start']['name']); 
    $output.=drupal_render($form['minute_start']); 
     $output .= drupal_render($form); 

    return $output; 

} 

我仍然有自动divs的表单元素周围的HTML代码的问题,这不是我想要的。在这种情况下,我想并排放置一些表单元素。我想这就是函数drupal_render()所做的。有没有一个函数调用,只会给我的表单元素没有所有的HTML标记?

谢谢